added getIps.sh script #4
@@ -0,0 +1,76 @@
|
|||||||
|
# getIps.sh
|
||||||
|
|
||||||
|
A bash script that resolves hostnames to their IPv4 and IPv6 addresses using DNS lookups.
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
This script reads a list of hostnames from an input file and performs DNS lookups to find their corresponding IPv4 and IPv6 addresses. The results are written to an output file in a tab-separated format.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./getIps.sh <input_file> <output_file>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Arguments
|
||||||
|
|
||||||
|
- `input_file`: Path to a text file containing hostnames (one per line)
|
||||||
|
- `output_file`: Path where the results will be written
|
||||||
|
|
||||||
|
### Example
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./getIps.sh hosts.txt results.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
## Input Format
|
||||||
|
|
||||||
|
The input file should contain hostnames, one per line:
|
||||||
|
|
||||||
|
```
|
||||||
|
google.com
|
||||||
|
github.com
|
||||||
|
stackoverflow.com
|
||||||
|
```
|
||||||
|
|
||||||
|
## Output Format
|
||||||
|
|
||||||
|
The output file will contain tab-separated values with the following columns:
|
||||||
|
|
||||||
|
```
|
||||||
|
hostname IPv4_address IPv6_address
|
||||||
|
```
|
||||||
|
|
||||||
|
Example output:
|
||||||
|
```
|
||||||
|
google.com 142.250.191.14 2a00:1450:4001:80e::200e
|
||||||
|
github.com 140.82.112.4 N/A
|
||||||
|
stackoverflow.com 151.101.1.69 N/A
|
||||||
|
```
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Resolves both IPv4 (A records) and IPv6 (AAAA records) addresses
|
||||||
|
- Handles empty lines in input file gracefully
|
||||||
|
- Shows "N/A" for addresses that cannot be resolved
|
||||||
|
- Clears previous output file before writing new results
|
||||||
|
- Provides usage information if incorrect arguments are provided
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- `dig` command (part of dnsutils package)
|
||||||
|
- Bash shell
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Make the script executable:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
chmod +x getIps.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
## Error Handling
|
||||||
|
|
||||||
|
- The script will exit with an error if the wrong number of arguments is provided
|
||||||
|
- Empty lines in the input file are automatically skipped
|
||||||
|
- DNS resolution failures are handled gracefully (shows "N/A")
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Check if required arguments are provided
|
||||||
|
if [ $# -ne 2 ]; then
|
||||||
|
echo "Usage: $0 <input_file> <output_file>"
|
||||||
|
echo "Example: $0 hosts.txt output.txt"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Input and output files from command line arguments
|
||||||
|
INPUT_FILE="$1"
|
||||||
|
OUTPUT_FILE="$2"
|
||||||
|
|
||||||
|
# Clear previous output
|
||||||
|
> "$OUTPUT_FILE"
|
||||||
|
|
||||||
|
# Process each hostname
|
||||||
|
while read -r host; do
|
||||||
|
# Skip empty lines
|
||||||
|
[[ -z "$host" ]] && continue
|
||||||
|
|
||||||
|
ipv4=$(dig +short A "$host" | head -n 1)
|
||||||
|
ipv6=$(dig +short AAAA "$host" | head -n 1)
|
||||||
|
|
||||||
|
echo -e "$host\t${ipv4:-N/A}\t${ipv6:-N/A}" >> "$OUTPUT_FILE"
|
||||||
|
done < "$INPUT_FILE"
|
||||||
|
|
||||||
|
echo "Done. Results written to $OUTPUT_FILE"
|
||||||
Reference in New Issue
Block a user