Skip to content

Instantly share code, notes, and snippets.

@myh-st
Created March 22, 2023 03:14
Show Gist options
  • Save myh-st/5380ae19db6cbb1afc0dd5373108c398 to your computer and use it in GitHub Desktop.
Save myh-st/5380ae19db6cbb1afc0dd5373108c398 to your computer and use it in GitHub Desktop.
Simulate traffic to test Device42
#!/bin/bash
# Define array of IPs and ports
ips=("10.0.0.1" "10.0.0.2" "10.0.0.3")
ports=(80 8080 3306)
# Loop through the IPs and ports with a random sleep time
for ip in "${ips[@]}"
do
for port in "${ports[@]}"
do
sleep $(($RANDOM % 5 + 3))
echo "Connecting to $ip on port $port"
nc -vz $ip $port
done
done
@myh-st
Copy link
Author

myh-st commented Mar 22, 2023

This is a Shell script that defines an array of IP addresses and ports, loops through them and connects to each IP address on each port, with a random sleep time between each connection. Here's a breakdown of what each part of the script does:

  • #!/bin/bash is called a shebang and defines the interpreter for this script as Bash.
  • The lines starting with # are comments and are ignored by the interpreter. They describe what the code does.
  • The two ips and ports arrays define a list of IP addresses and ports respectively.
  • The nested for loops iterate through both arrays and executes the following code block for each combination of IP address and port:
    • The sleep command waits for a random amount of time, between 3 and 7 seconds ($RANDOM % 5 + 3). This is done to simulate real-world delays or network issues when connecting to servers.
    • The echo command prints a message to the console indicating which IP and port is being connected to.
    • The nc command is used to connect to the IP address on the specified port. -vz options are passed to enable verbose output and tell nc not to send any data after initiating the connection. This is often used to check whether a remote host is reachable and also for port scanning purposes.

Overall, this script can be used to test connectivity to multiple servers at once, or to check if specific ports are open on a server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment