1754976 Members
3201 Online
108828 Solutions
New Discussion юеВ

netstat

 
Roy Colica
Advisor

netstat

I'm going to write some C-code to get info from netstat by the use of a 'system("netstat > /tmp/netstat.output")' call redirecting the output on a file I'll parse later. I'm wondering if exists an equivalent command (something like netstat()) I can use directly in my C-code. Can someone help me?
6 REPLIES 6
Muthukumar_5
Honored Contributor

Re: netstat

Nettools package may contain source of netstat.

ftp://ftp.cs-ipv6.lancs.ac.uk/pub/Code/Linux/Net_Tools/

Ref: http://www.uwsg.iu.edu/hypermail/linux/net/9707.1/0128.html

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: netstat

I feel you can use netstat > /tmp/netstat.output. Parse output file with c coding. Because parsing applications (awk, grep or perl) will be slower than the application made specially for specific parsing.

You can combine c coding, netstat utility and scripting to suite requirement. It is enough to use netstat + scripting simply.

If you have a great time in speedy IT world then start to work. ;)

hth.

Easy to suggest when don't know about the problem!
Tom Schroll
Frequent Advisor

Re: netstat


I don't know if there is a specific function available to read the same data as the netstat command in "C". Since there are low-level kernel calls going on to get this data, I'd imagine that you are out of luck without access to the source.

HOWEVER, if you want to be slick within your "C" program, instead of writing the output of netstat to a temp file and then reading the temp file back in, you might want to explore "popen()". This fuction will run a shell command for you, and give you a file descriptor, from which you can read as if you were reading from a file. Check out the man page for popen (man 3 popen) for more info.

-- Tom
If it ain't broke, it needs optimized.
rick jones
Honored Contributor

Re: netstat

You could get some SNMP libraries and issue MIB requests to an SNMP agent running on the system - assuming of course that there is an SNMP agent running on the system.

The interface(s) used by the HP-UX netstat command are not documented.

There may be some statistics you can retrieve via pstat() calls on later versions of HP-UX.
there is no rest for the wicked yet the virtuous have no pillows
James R. Ferguson
Acclaimed Contributor

Re: netstat

Hi Roy:

I'd consider crafting a perl script that opens a pipe to 'netstat', reads that output and parses and outputs as it goes. This avoids the (temporary) file and brings you to the key element of your objective -- parsing and reporting the custom data you want.

Regards!

...JRF...
Roy Colica
Advisor

Re: netstat

I understand there's no way to perform the command without invoking using the netstat call, but I had some useful comments on how do it faster.