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 to192.168.5.1
failed.Packet Loss: For
192.168.1.1
, there was no packet loss, while for192.168.5.1
, there was 100% packet loss.Network Accessibility:
192.168.1.1
is accessible, but192.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 (withCtrl + 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 toip.txt
.
ping 192.168.1.1 -c 1 > ip.txt

Grep Command
The command cat ip.txt | grep "icmp"
does the following:
cat ip.txt
: Displays the contents of theip.txt
file.|
: Pipes the output of thecat
command to the next command.grep "icmp"
: Searches for and displays lines containing the word "icmp" in the output fromcat 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:
cat ip.txt
: Displays the contents ofip.txt
.| grep "icmp"
: Filters and shows lines containing the word "icmp."| 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:
cat ip.txt
: Displays the contents ofip.txt
.| grep "icmp"
: Filters and shows lines containing the word "icmp."| cut -d " " -f 4
: Splits the line by spaces and extracts the 4th field (typically the RTT value).| tr -d ":"
: Removes any colon (":") characters from the extracted RTT value.
cat ip.txt | grep "icmp" | cut -d " " -f 4 | tr -d ":"

Last updated