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

I have the sudden urge to stop with CGI for a while and program things for the commandline instead. I was thinking about system utilities such as a port scanner (for your own system) or anything along the lines of security.

Anyone have ideas on how to write a program to scan your own ports and display information on them? Any other fun ideas to do on your system (security, system information, etc) that's not overdone but would be useful.

I have zero experience scripting anything of this sort, any pointers?

Replies are listed 'Best First'.
Re: System utilities with Perl
by tachyon (Chancellor) on Nov 05, 2004 at 21:23 UTC
    IO::Socket::INET. Here's a command line one for you, s/"/'/ and s/'/"/ on *nix as this is Win32 quoting ;-)
    perl -MIO::Socket::INET -le "do{$s=IO::Socket::INET->new(PeerAddr=>'12 +7.0.0.1',PeerPort=>$_,Timeout=>1);print $s? $_.' OPEN' : $_.' CLOSED' +}for ARGV[0]..$ARGV[1]" 1 1024

    You should also check out the Perl Power Tools Project which has ported many of the popular *nix command line tools to Perl.

    cheers

    tachyon

Re: System utilities with Perl
by talexb (Chancellor) on Nov 05, 2004 at 21:01 UTC

    The nmap utility may already do what you want, unless you're interested in re-writing this utility from scratch, in Perl, as a learning exercise.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      I want to do it to learn from, I don't want to use real system commands that are there. I want to do it via Perl.
Re: System utilities with Perl
by tall_man (Parson) on Nov 05, 2004 at 21:12 UTC
Re: System utilities with Perl
by teabag (Pilgrim) on Nov 06, 2004 at 02:59 UTC
    ah yes, portscanner.
    I feel an interesting conversation coming up soon ;)

    pscanner.pl

    Teabag

    -- Siggy Played Guitar
    Sure there's more than one way, but one just needs one anyway - Teabag
Re: System utilities with Perl
by zentara (Cardinal) on Nov 06, 2004 at 19:59 UTC
    Here's a variation on Tachyon's 1-liner in a form easier for a beginner to see. There are many more advanced scanners, but it's useful to start at the "lowest common denominator", so you can see why it is lacking in features, and why those features are important to making the scanner useful. But if you really want to get serious about scanning, you are better off with nmap...it's huge, up-to-date with all the detailed considerations, and well documented.
    #!/usr/bin/perl use warnings; use IO::Socket; # scan($port, $end_port, $target); scan(1, 2500, '192.168.0.1'); sub scan { my ($from, $to, $where) = @_; print "Scanning $where | from port $from to $to\n\n"; for my $p ($from .. $to) { print "Port $p is open\n" if IO::Socket::INET->new( PeerAddr => $where, PeerPort => $p, Proto => 'tcp', Timeout => 1, ); } print "Port scan complete\n\n"; }

    I'm not really a human, but I play one on earth. flash japh