Scripting with Bash

Ifconfig Command

ifconfig

Key Difference: Ping Output

ping <IP addr>
  • Ping Success: The first ping to 192.168.1.1 was successful, whereas the second ping to 192.168.5.1 failed.

  • Packet Loss: For 192.168.1.1, there was no packet loss, while for 192.168.5.1, there was 100% packet loss.

  • Network Accessibility: 192.168.1.1 is accessible, but 192.168.5.1 is not.

Key Difference: ping 192.168.1.1 Vs ping 192.168.1.1 -c 1

  • ping 192.168.1.1 ➤ Sends continuous ping requests until you stop it manually (with Ctrl + C).

ping 192.168.1.1
  • ping 192.168.1.1 -c 1 ➤ Sends only one ping request, then stops automatically.

ping 192.168.1.1 -c 1
  • Use -c 1 when you just want to check if a host is reachable once.

Ping test to 192.168.1.1 saved to ip.txt

  • The command pings 192.168.1.1 once and saves the output to ip.txt.

ping 192.168.1.1 -c 1 > ip.txt

Grep Command

The command cat ip.txt | grep "icmp" does the following:

  1. cat ip.txt: Displays the contents of the ip.txt file.

  2. |: Pipes the output of the cat command to the next command.

  3. grep "icmp": Searches for and displays lines containing the word "icmp" in the output from cat ip.txt.

It effectively filters and shows only the lines with "icmp" (usually related to ping responses).

cat ip.txt | grep "icmp"

The command cat ip.txt | grep "icmp" | cut -d " " -f 4 does the following:

  1. cat ip.txt: Displays the contents of ip.txt.

  2. | grep "icmp": Filters and shows lines containing the word "icmp."

  3. | cut -d " " -f 4: Splits each line by spaces and extracts the 4th field (typically the time or RTT value from a ping response).

cat ip.txt | grep "icmp" | cut -d " " -f 4

The command cat ip.txt | grep "icmp" | cut -d " " -f 4 | tr -d ":" does the following:

  1. cat ip.txt: Displays the contents of ip.txt.

  2. | grep "icmp": Filters and shows lines containing the word "icmp."

  3. | cut -d " " -f 4: Splits the line by spaces and extracts the 4th field (typically the RTT value).

  4. | tr -d ":": Removes any colon (":") characters from the extracted RTT value.

cat ip.txt | grep "icmp" | cut -d " " -f 4 | tr -d ":"

Last updated