Building a Chat-Ping Program in Linux Using Bash Scripting
we will explore the process of creating a basic chat-ping program in Linux using Bash scripting. This program allows users to send pings to a specified IP address or hostname and displays the round-trip time (RTT) for each ping. By following the steps outlined here, you can develop a simple yet effective communication tool within your Linux environment.
Prerequisites: Before diving into the script, ensure that you have a Linux system with Bash installed. Most Linux distributions come with Bash pre-installed, making it readily available for development.
Creating the Script: The script, named chat_ping.sh
, is written in Bash and can be easily customized to suit your needs. By defining variables for the target IP address, interval between pings, and the number of pings to send, you have the flexibility to adapt the program according to your specific requirements.
#!/bin/bash
# Define the IP address or hostname to ping
target="example.com"
# Define the interval between pings (in seconds)
interval=1
# Define the number of pings to send
count=10
# Loop to send pings
for (( i=1; i<=$count; i++ ))
do
# Send a ping and capture the output
output=$(ping -c 1 -W 1 $target)
# Extract the round-trip time from the output
rtt=$(echo "$output" | awk '/time=/ { print $7 }' | cut -d '=' -f 2)
# Print the round-trip time
echo "Ping $i: $rtt ms"
# Wait for the specified interval before sending the next ping
sleep $interval
done
- Variables:
target
: This variable holds the IP address or hostname to ping. In the example, it is set to "example.com", but you can replace it with your desired target.interval
: This variable determines the interval between pings, in seconds. By default, it is set to 1 second.count
: This variable specifies the number of pings to send. The script is set to send 10 pings by default.- Loop:
- The script uses a
for
loop to send the specified number of pings. - Within the loop, it sends a ping to the target using the
ping
command with options-c 1
(send one ping) and-W 1
(wait for 1 second for a response). - The output of the ping command is captured in the
output
variable. - Extracting Round-Trip Time (RTT):
- The script uses
awk
andcut
commands to extract the round-trip time (RTT) from the ping output. awk '/time=/ { print $7 }'
searches for lines containing "time=" and prints the 7th field (which contains the RTT).cut -d '=' -f 2
separates the RTT value from the string "time=".- Printing RTT:
- The script prints the ping number and the corresponding RTT by echoing the message with the
echo
command. - For example, it displays “Ping 1: 12.345 ms” for the first ping.
- Waiting Between Pings:
- After each ping, the script waits for the specified interval using the
sleep
command.
The script then uses a loop to send the specified number of pings. Each time, it sends a ping using the ping
command and captures the output. It extracts the round-trip time (RTT) from the output and prints it. Finally, it waits for the specified interval before sending the next ping.
Running the Script:
- Make the script executable using
chmod +x chat_ping.sh
. - Execute the script with
./chat_ping.sh
. - The script will start sending pings to the specified target and display the RTT for each ping.
Customization:
- The article suggests possible modifications to customize the script:
- Changing the target: Replace “example.com” with the desired IP address or hostname.
- Modifying the interval: Adjust the
interval
variable to set the desired interval between pings. - Modifying the ping count: Update the
count
variable to specify the number of pings to send.
Happy learning !