cbro has asked for the wisdom of the Perl Monks concerning the following question:

Hello All: I've searched the categorized Q/A Section, the archives, CPAN, and queried Google but cannot find out if there is a module that has a function equivalent to UNIX 'netstat' command. I have the 'Net' package but do not know if any of my current modules contain such a function. Please name the module if it does exist. Many 'thanks', in advance, for any help.

Replies are listed 'Best First'.
Re: netstat equivalent
by phydeauxarff (Priest) on Mar 21, 2003 at 19:10 UTC
    Checkout the following on a similar attempt by shotgunefx who appears to have run into the same wall you did and worked a solution which is talked about in Node 184721

    There is also an discussion of using Regex with Netstat that contains some code examples at Node 172741

    While not a module, from the quick read of these, they appear to contain enough code snippets to get you well started down the path.

Re: netstat equivalent
by pg (Canon) on Mar 21, 2003 at 18:14 UTC
    Those information netstat printed out are actually accessiable through various c structures. What netstat does is simply present those info visually.

    I am not aware of any Perl module wrap those c stuff.

    Ib this case, you can simply get what you want thru open3 calls.

    One thing important is to always do a waitpid after each open3 call, otherwise you will create lots of zombies.
      Yeah, I don't think I'm going to find a wrapper either.

      I could write a C program, I just prefer Perl DBI over the MySQL C API.

      I might try doing something like this:

      open(STATUS, "netstat -an 2>&1 |") || die "can't fork: $!";

      ...but, I have not decided yet.

      Well, regardless, thank you both for your responses :-)
Re: netstat equivalent
by Rex(Wrecks) (Curate) on Mar 21, 2003 at 17:26 UTC
    Um, there is nothing comprehensive that I know about, but netstat does a lot of things. Is there something specific you are looking to do?

    Update: I'm just curious why I'm getting negative votes for this? All I did was ask for clarification.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
      Yes, thanks for asking.
      I'm trying to find the total number of inbound and outbound email connections on my server. I'm currently doing this by running 'ps' piped through sed and grep to trim out all 'smtp' and 'smtpd' entries.
      I'm attempting to clean this up and move to Perl functions instead of using my current shell script and without using system/backtick commands in Perl. I'd also rather use a netstat type function as opposed to running 'ps'.
Re: netstat equivalent
by grantm (Parson) on Mar 21, 2003 at 19:24 UTC

    You might be able to get the data you're after by poking around in /proc