I am required to create a shell script in Mac, which will monitor and if a specified URL (for example, *.google.com) is hit from any browser or program, shell script will prompt or do an operation. Could anyone guide how to do this?
If you want to monitor or capture network traffic, tcpdump is your friend- requires no proxy servers, additional installs, etc., and should work on stock Mac OS as well as other *nix variants.
Here's a simple script-
sudo tcpdump -ql dst host google.com | while read line; do echo "Match found"; done
The while read
loop will keep running until manually terminated; replace echo "Match found"
with your preferred command. Note that this will trigger multiple times per page load; you can use tcpdump -c 1
if you only want it to run until it sees relevant traffic.
As Azize mentions, You could also have tcpdump
outputting to a file in one process, and monitor that file in another. incrontab
is not available on Mac OS X; you could wrap tail -f in a while read
loop:
sudo tcpdump -l dst host google.com > /tmp/output &
tail -fn 1 /tmp/output | while read line; do echo "Match found"; done
There's a good similar script available on github. You can also read up on tcpdump filters if you want to make the filter more sophisticated.