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!!
In Linux, you look for files in
/proc/net
and parse that.For example, TCP connections are listed in
/proc/net/tcp
will show something like this
You can then split the lines, look for the open connections and act accordingly. Look at thesource of
netstat
for more.Make a bash script.
LOGIC:
Use
netstat -natp
(filter it throughawk
/sed
to get the ports, then grep it) Then use a simple test to see if the result was empty. Runcurl
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
andawk
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 sincenetstat
does that and formats it nicely, why bother?