Skip to content

Instantly share code, notes, and snippets.

@aimtiaz11
Last active June 15, 2024 15:20
Show Gist options
  • Save aimtiaz11/ce73642553e6f13379d13ce28cd459a5 to your computer and use it in GitHub Desktop.
Save aimtiaz11/ce73642553e6f13379d13ce28cd459a5 to your computer and use it in GitHub Desktop.
Useful Networking Scripts
#!/bin/bash
# Define a list of hostnames
HOSTS=(
"google.com:443"
"amazon.com:443"
)
# Iterate through the list of hostnames
for hostname in "${HOSTS[@]}"; do
IFS=":"
read -a parts <<< "$hostname"
# Get the IP addresses using 'host' command
ip_addresses=$(host ${parts[0]} | awk '/has address/ {print $4}' | tr '\n' ',' | sed 's/,$//')
# Check if 'host' command succeeded and if any IPv4 addresses were found
if [ -z "$ip_addresses" ]; then
echo "Could not resolve any IPv4 addresses for $hostname"
else
internal="false"
for ip in $(echo "$ip_addresses" | tr ',' ' '); do
if [[ "$ip" == 10.* ]]; then
internal="true"
break
fi
done
if [ "$internal" == "true" ]; then
echo "INTERNAL $hostname - $ip_addresses"
else
echo "EXTERNAL $hostname - $ip_addresses"
fi
fi
done
#!/bin/bash
HOST=$1
PORT=$2
if [[ -z "$HOST" || -z "$PORT" ]]; then
echo "Usage: $0 <host> <port>"
exit 1
fi
timeout 1 bash -c "</dev/tcp/$HOST/$PORT" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Connection to $HOST on port $PORT succeeded."
else
echo "Connection to $HOST on port $PORT failed."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment