c++linuxip-addresstcp-port

Get all connected IP´s on the Linux machine


Recently i was asked a question in an interview which i couldn't do. Anyone got a solution for this?

Grab all connected IP´s on the Linux machine
check every connected IP if TCP port 1706 is open

if its open > execute command.  CURL ‘http:// some address ’ 
Else do nothing.
program will check this every 60 minits



Plattform Linux Ubuntu Server 12. X64 / x32

WAP in C++

Thanks!!


Solution

  • Make a bash script.

    LOGIC:

    Use netstat -natp (filter it through awk/sed to get the ports, then grep it) Then use a simple test to see if the result was empty. Run curl if it was.

    Put this in a cron job. Simple stuff, really.

    EDIT:

    netstat is a utility which will show you all of the connections on your computer. netstat -natp shows a list of the programs which have tcp sockets on your computer.

    sed and awk are used for text formatting. You can use them to list a specific column.

    grep searches input to find a specified string.

    bash allows for basic logic, and can be used to see if a string is empty.

    cron is a linux process which schedules commands to be run at certain times.

    EDIT #2:

    You COULD poll /proc/net/tcp, but since netstat does that and formats it nicely, why bother?