If you're using linux shell, you can use tcpdump
to get the job done. Most of the commands used in the shell are pre-installed in most linux distributions.
#!/bin/sh
# # # Run this as sudo
# # # Pass an argument of your IP address
# # $ Usage: sudo ./<nameOfTheScript> <IPofYourMachine>
# Parameters
captureTime=300
# Capture packets for X Seconds
tcpdump -i eth0 dst host $1 >> /dev/null 2> out &
pid=$!
sleep $captureTime
kill $pid
# Extract relevant data
cat out | tail -2 | head -1 | cut -d " " -f 1
# Cleaning up!
rm out
The shell runs tcpdump
for 300
seconds and terminates it. tcpdump
outputs the data that your require to stderr
. That would be the reason to redirect that stream to a file (2> out
). The last-second command of the shell extracts the data that we want from the full message that's thrown from tcpdump
. It requires to be run as sudo
! Also don't forget to modify the listening interface as required.
If it's still not clear about why I've done what I've done, let me know!