Skip to content

Instantly share code, notes, and snippets.

@tayyebi
Last active October 11, 2024 10:29
Show Gist options
  • Save tayyebi/7422a348250923fe490f2c23e3e11c86 to your computer and use it in GitHub Desktop.
Save tayyebi/7422a348250923fe490f2c23e3e11c86 to your computer and use it in GitHub Desktop.
Troubleshoot internet
traceroute_*.*
tshark_*.*

Install dependencies

sudo apt-get install -y tshark graphviz curl net-tools dnsutils

Scripts created with the assistance of Bing AI

#!/bin/bash
# Script created with the assistance of AI
domains=("google.com" "example.com")
output_file="traceroute_results.csv"
dot_file="traceroute_graph.dot"
image_file="traceroute_graph.png"
tshark_output_file="tshark_results.csv"
# Clear the output files if they exist
>$output_file
>$tshark_output_file
# Detect the network device used for internet access
network_device=$(ip route | grep default | awk '{print $5}')
# Function to perform reverse DNS lookup using nslookup
declare -A domain_cache
get_domain() {
local ip=$1
if [[ -z "$ip" ]]; then
echo "NaN"
return 1
fi
if [[ -n "${domain_cache[$ip]}" ]]; then
echo "${domain_cache[$ip]}"
else
local domain=$(nslookup $ip | awk -F'= ' '/name =/ {print $2}' | sed 's/\.$//')
domain_cache[$ip]=$domain
echo $domain
fi
}
# Run traceroute 3 times for each domain and save results to CSV
for domain in "${domains[@]}"; do
for i in {1..3}; do
traceroute -n $domain | awk '{if(NR>1)print $2}' | awk 'NR==1{start=$0; next} {print start","$0; start=$0}' >>$output_file
done
done
# Capture network traffic with tshark for some seconds and save to CSV
tshark -i $network_device -a duration:10 -T fields -e ip.src -e ip.dst -E separator=, -E occurrence=f >$tshark_output_file
# Remove duplicated rows
sort -u $output_file -o $output_file
sort -u $tshark_output_file -o $tshark_output_file
# Update CSV file with domain names
temp_file=$(mktemp)
while IFS=, read -r src dst; do
src_domain=$(get_domain $src)
dst_domain=$(get_domain $dst)
echo "$src$src_domain,$dst$dst_domain" >>$temp_file
done <$output_file
mv $temp_file $output_file
# Create dotfile from CSV
echo "digraph G {" >$dot_file
while IFS=, read -r src dst; do
echo "\"$src\" -> \"$dst\";" >>$dot_file
done <$output_file
echo "}" >>$dot_file
# Remove duplicated rows
awk -i inplace '!seen[$0]++' $output_file
# Generate image from dotfile
dot -Tpng $dot_file -o $image_file
#!/bin/bash
# List of DNS servers to check
DNS_SERVERS=("85.15.1.15" "5.202.100.100" "10.202.10.10" "178.22.122.100" "1.1.1.1" "91.239.100.100" "8.8.8.8" "8.26.56.26" "77.88.8.8" "64.68.200.200" "208.67.222.222" "199.85.127.10" "192.135.250.69" "156.154.70.1")
# List of domains to check
DOMAINS=("bing.com" "amazon.com" "snapp.ir" "farsnews.ir")
# List of websites to check for censorship
WEBSITES=("grrg.ir" "aparat.com" "youtube.com", "instagram.com")
# List of update sources to check
UPDATE_SOURCES=("https://update.microsoft.com" "https://dl.google.com" "https://update.hicloud.com" "https://updates.cdn-apple.com" "https://update.miui.com")
# Function to print a heading with customizable formatting and background color
print_heading() {
local heading="$1"
local width="${2:-40}" # Default width is 20
local char="${3:-=}" # Default decoration character is '='
local bg_color="${4:-\e[44m}" # Default background color is blue
# Convert heading to uppercase
local upper_heading=$(echo "$heading" | tr '[:lower:]' '[:upper:]')
local line=$(printf "%${width}s" | tr ' ' "$char")
local padded_heading=$(printf "%*s" $(((${#upper_heading} + $width) / 2)) "$upper_heading")
echo -e "${bg_color}${line}\e[0m"
echo -e "${bg_color}${padded_heading}\e[0m"
echo -e "${bg_color}${line}\e[0m"
}
# Example usage
print_heading "*** RUNNING NETWORK TESTS ***" "*" 30 "\e[42m" # Green background
# Function to print test results with check mark and cross sign
print_result() {
local message=$1
local status=$2
if [ "$status" -eq 0 ]; then
echo -e "\e[32m✔️ $message\e[0m" # Green check mark for success
else
echo -e "\e[31m❌ $message\e[0m" # Red cross sign for failure
fi
}
# Function to run mtr and analyze the output
run_mtrs() {
if command -v mtr &>/dev/null; then
print_heading "Running MTR checks..."
for destination in "${DOMAINS[@]}"; do
local destination=$1
echo "Running mtr to $destination..."
mtr --report --report-cycles 10 $destination
done
fi
}
# Function to check network adapter status using ip command
check_network_adapter() {
print_heading "Checking network adapter..."
ip link show | grep -q "state UP"
if [ $? -eq 0 ]; then
print_result "Network adapter is up." $?
else
print_result "Network adapter is down." $?
exit 1
fi
}
# Function to check if the system has an IP address
check_ip_address() {
print_heading "Checking IP address..."
ip addr show | grep -q "inet "
if [ $? -eq 0 ]; then
print_result "IP address is assigned." 0
else
print_result "No IP address assigned. Please check your DHCP settings or static IP configuration." 1
exit 1
fi
}
# Function to check gateway reachability
check_gateway() {
print_heading "Checking gateway reachability..."
GATEWAY=$(ip route | grep default | awk '{print $3}')
if ping -c 1 $GATEWAY &>/dev/null; then
print_result "Gateway is reachable." 0
else
print_result "Gateway is not reachable. Please check your router or modem." 1
exit 1
fi
}
# Function to ping nodes outside the current segment
ping_external_nodes() {
print_heading "Pinging external nodes..."
for node in "${WEBSITES[@]}"; do
if ping -c 1 $node &>/dev/null; then
print_result "Node $node is reachable." 0
else
print_result "Node $node is not reachable." 1
fi
done
}
# Function to detect DNS servers using the best available method
detect_dns_servers() {
print_heading "Checking current DNS servers..."
# Try using nmcli
if command -v nmcli &>/dev/null; then
print_result "DNS servers detected via nmcli:" 0
nmcli dev show | grep 'IP4.DNS' | awk '{print $2}'
return
fi
# Try using systemd-resolve
if command -v systemd-resolve &>/dev/null; then
print_result "DNS servers detected via systemd-resolve:" 0
systemd-resolve --status | grep 'DNS Servers' -A 2 | grep -v 'DNS Servers' | awk '{print $1}'
return
fi
# Fallback to /etc/resolv.conf
if [ -f /etc/resolv.conf ]; then
print_result "DNS servers detected via /etc/resolv.conf:" 0
grep "nameserver" /etc/resolv.conf | awk '{print $2}'
return
fi
print_result "No DNS servers detected. None of the methods are available." 1
}
# Function to check DNS resolution using multiple DNS servers
check_dns() {
print_heading "Checking DNS resolution..."
local command=""
if command -v nslookup &>/dev/null; then
command="nslookup"
elif command -v dig &>/dev/null; then
command="dig"
else
echo "Neither nslookup nor dig is available."
return 1
fi
for server in "${DNS_SERVERS[@]}"; do
for domain in "${DOMAINS[@]}"; do
if [ "$command" == "nslookup" ]; then
if nslookup $domain $server &>/dev/null; then
print_result "DNS resolution for $domain with $server using nslookup: OK" 0
else
print_result "DNS resolution for $domain with $server using nslookup: Failed" 1
fi
elif [ "$command" == "dig" ]; then
if dig @$server $domain &>/dev/null; then
print_result "DNS resolution for $domain with $server using dig: OK" 0
#################### curl --dns-servers $server http://$domain
else
print_result "DNS resolution for $domain with $server using dig: Failed" 1
fi
fi
done
done
}
# Function to check collision rate
check_collisions() {
print_heading "Checking interfaces collision rate..."
if [ command -v netstat ] &>/dev/null && [ command -v ifconfig ] &>/dev/null; then
# Get a list of all network interfaces except the loopback
NICS=$(ifconfig -a | grep -v ^lo | grep UP | awk '{print $1}' | sed 's/://')
# Loop through each network interface and calculate the collision rate
for nic in $NICS; do
print_result -n "$nic: " 0
netstat -i | grep ^$nic | awk '{print $9 / $7 }' 2>/dev/null
done
else
print_result "required commands are not available" 1
fi
}
# Function to check internet connectivity
ping_domains() {
print_heading "Pinging websites..."
for domain in "${WEBSITES[@]}"; do
if ping -c 1 $domain &>/dev/null; then
print_result "Ping poing with $domain." 0
else
print_result "Ping failed on $domain." 1
fi
done
}
# Function to check for potential censorship
check_censorship() {
print_heading "Checking websites..."
for website in "${WEBSITES[@]}"; do
if curl -s --max-time 10 https://$website | grep -q "<html>"; then
print_result "$website is accessible." 0
else
print_result "$website is inaccessible." 1
fi
done
}
# Function to check main update sources
check_sanctions() {
print_heading "Checking sanctions..."
for source in "${UPDATE_SOURCES[@]}"; do
if curl -s --max-time 10 $source | grep -q "<html>"; then
print_result "$source is accessible." 0
else
print_result "Potential issue detected with $source." 1
fi
done
}
# Function to check if proxy is disturbing the connection
check_proxy() {
print_heading "Checking for proxy issues..."
if env | grep -i proxy; then
print_result "Proxy settings detected. Please check if they are correct." 1
else
print_result "No proxy settings detected." 0
fi
}
# Function to check if VPN is disturbing the connection
check_vpn() {
print_heading "Checking for VPN issues..."
if ip a | grep -i tun0; then
print_result "VPN connection detected. Please check if it is causing any issue." 0
else
print_result "No VPN connection detected." 0
fi
}
# Function to check if firewall is blocking the connection
check_firewall() {
print_heading "Checking firewall status..."
iptables -L &>/dev/null
print_result "Firewall is active." $?
}
# Function to check if the system time is correct
check_system_time() {
print_heading "Checking system time..."
if timedatectl | grep -q "NTP synchronized: yes"; then
print_result "System time is synchronized with NTP." 0
else
print_result "System time is not synchronized with NTP. Please check your NTP settings." 1
fi
}
# Function to run traceroute and analyze the output
run_traceroutes() {
if command -v traceroute &>/dev/null; then
print_heading "Running traceroute checks..."
# Loop through each destination and run traceroute
for destination in "${DOMAINS[@]}"; do
traceroute_output=$(traceroute -n $destination)
# Analyze the traceroute output
echo "Analyzing traceroute to $destination..."
echo "$traceroute_output" | awk '
BEGIN { hop_count = 0; }
{
if ($1 ~ /^[0-9]+$/) {
hop_count++;
print "Hop " hop_count ": " $2;
}
}'
echo ""
done
fi
}
# Run all checks
check_network_adapter
check_collisions
check_ip_address
check_gateway
check_proxy
check_vpn
check_firewall
check_system_time
ping_external_nodes
detect_dns_servers
ping_domains
check_censorship
check_sanctions
run_mtrs
check_dns
run_traceroutes
echo "All checks finished."
2024-10-11 13:59:46 +03:30
Parent: 4a027bdceed017c89b178f923b78312c19ec55b3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment