Search code examples
bashtcpdump

How to check if tcpdump catpured any packets?


I'm trying to see if a system is up. In order to achieve this I can use a tcpdump command to see the packets captured and use this to verify that a system is currently up. However, I'm trying to incorporate this into a script and I'm not sure how to put that logic into a conditional.

I've tried using netcat but I can't seem to get that working at all so I'm only trying to use tcpdump at the moment because at least this works.

Here is the tcpdump command that works but I'm having trouble incorporating into an if conditional

tcpdump -i eth0 "udp port 69420"

Here is some pseudocode as to what I'm trying to accomplish:

if tcpdump captured any packets 
   echo "system is up"

Solution

  • This is a blocking code and will keep listening to packets on the port till 1st packet is received.

    if tcpdump -i eth0 "udp port 69420" -c 1 ;then
         echo "system is up"
     fi
    

    Usage: tcpdump [-aAdDeflLnNOpqRStuUvxX] [-c count] [ -C file_size ] [ -E algo:secret ] [ -F file ] [ -i interface ] [ -M secret ] [ -r file ] [ -s snaplen ] [ -T type ] [ -w file ] [ -W filecount ] [ -y datalinktype ] [ -Z user ] [ expression ]

    PS: tcpdump is not typically used like this(continuous monitoring), it is a resource exhaustive, tool and should be used for debugging purposes.