Code Monkey home page Code Monkey logo

host2ip's Introduction

Host2IP

Bash script to convert a .txt file of Domains/URL's to IP Addresses in a seperate .txt file

#!/bin/bash

This line is called the shebang and specifies the interpreter to be used for executing the script, in this case, /bin/bash.

# Check if an argument was provided
if [ $# -eq 0 ]; then
  echo "Please provide a .txt file as an argument."
  exit 1
fi

input_file="$1"
output_file="Host-IP.txt"

These lines check if an argument (the input file) was provided when running the script. If no argument is provided, it displays an error message and exits the script with a non-zero status. The provided argument is assigned to the input_file variable, and the output_file variable is set to "Host-IP.txt".


# Check if the input file exists
if [ ! -f "$input_file" ]; then
  echo "Input file not found: $input_file"
  exit 1
fi

This section checks if the input file exists. If the file does not exist, it displays an error message and exits the script.

# Create an empty output file
> "$output_file"

This line creates an empty output file or overwrites the existing file if it already exists.


# Resolve IP addresses from each line in the input file
while IFS= read -r line; do
  # Remove leading and trailing spaces from the line
  line=$(echo "$line" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')

  # Split the line into URL and netmask
  url=$(echo "$line" | awk '{print $1}')
  netmask=$(echo "$line" | awk '{print $2}')

  echo "Resolving: $url"

  # Remove "http://" or "https://" from the beginning of the URL
  url_without_prefix=$(echo "$url" | sed -e 's,http://,,;s,https://,,')

  ip=$(host "$url_without_prefix" | awk '/has address/ {print $4; exit}')
  if [ -n "$ip" ]; then
    echo "IP address: $ip"
    echo "$ip" >> "$output_file"
  else
    echo "Failed to resolve IP address for: $url"
  fi

done < "$input_file"

This is the main loop that reads each line from the input file and resolves the IP address for the corresponding URL. Here's what it does:

The IFS= read -r line command reads a line from the input file and stores it in the line variable.
The sed command removes any leading and trailing spaces from the line to ensure accurate parsing.
The awk commands extract the URL and netmask from the line. It assumes that the URL is the first field and the netmask is the second field.
The resolved IP address is obtained by removing the "http://" or "https://" prefix from the URL and passing it to the host command. The output of host is then filtered using awk to extract the IP address.
If an IP address is successfully resolved, it is echoed to the console and appended to the output file using >>.
If the IP address resolution fails, an error message is displayed.

# Remove the lines with "Failed to resolve" from the output file
sed -i '/Failed to resolve/d' "$output_file"

This line uses the sed command to remove all lines containing "Failed to resolve" from the output file. The -i flag allows in-place editing of the file, and the /Failed to resolve/d command deletes the matching lines.

That's the explanation of the script.

To use it, follow these steps:

  1. Open a text editor and copy the script into a new file.
  2. Save the file with a .sh extension, for example, resolve_ips.sh.
  3. Open a terminal or command prompt and navigate to the directory where the script file is saved.
  4. Run the script by executing the command: bash resolve_ips.sh input_file.txt.
  5. Replace input_file.txt with the path to your actual input file containing the URLs and netmasks.

The script will resolve the IP addresses for each URL in the input file and save the results in the Host-IP.txt file in the same directory. The lines with "Failed to resolve" will be excluded from the output file.

  1. After running the script, you can check the Host-IP.txt filed to view the resolved IP addresses.

Note: Make sure you have proper permissions to execute the script and that the necessary networking dependencies are installed on your system.

host2ip's People

Contributors

sic4rio avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.