In: Computer Science
i want to write bash script that generates syslog if my machine has been pinged
this is my code, but it is not full and not work as i want
#!/bin/bash
status=`echo "$?"`
monitor=`sudo tcpdump -i eth0 icmp and icmp[icmptype]=icmp-echo -n`
if [ "$status" -eq 0 ]; then
sleep 5s
pkill -f "$0"
`echo "$monitor" | awk '{print $3}'
fi
Following command will dump the ping logs continuously to the log file. This will monitor all the interfaces attached to the machine. If you need to monitor only a specific network interface then find the interface name using networksetup -listallhardwareports and find the interface you wan to monitor
sudo tcpdump -i any icmp > log.txt
If you need only few number of output you can use below command which will output only 5 ping dumps
sudo tcpdump -i any -c5 icmp
The tcpdump command process will continue dumping the ICMP request until terminate the process. But if we are giving specific number of response then the program will stop after those requests are completed.
status=`echo "$?"`
monitor=`sudo sudo tcpdump -i any -c5 icmp`
if [ $? -eq 0 ] && [ -n "$monitor" ]; then
sleep 5s
echo "$0"
pkill -f "$0"
echo "$monitor" | awk '{print $3}'
fi
Ping command issued from another terminal
Sample output