LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-28-2010, 10:43 PM   #1
bulgin
LQ Newbie
 
Registered: Mar 2010
Posts: 9

Rep: Reputation: Disabled
Need script to monitor port for active, then send command


I need a script that would continuously monitor one port and when it is active (not listening or waiting) execute a series of commands to standard output.

I am familiar with netstat but am not good with scripting so I could use some help.

Thanks.
 
Old 03-28-2010, 11:12 PM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
My very old script on perl
Code:
#!/usr/bin/perl -w
use Socket;

#net - netstat
#tcp - socket
#udp - deprecated, don't work
my %serv=(8031=>'net', 8041=>'net',8051=>'net',8000=>'net');

sub restart {
    my ($port)=@_;
    if($port eq 8031) {system("/home/kernel/run_vlc_yamal start");}
    if($port eq 8041) {system("/home/kernel/run_vlc_eam22 start");}
    if($port eq 8051) {system("/home/kernel/run_vlc_yamal_c start");}
    if($port eq 8000) {system("rc-config restart icecast");}
    if($port eq 9001) {system("/home/kernel/run_flv2");}
}

sub check {
    my ($port,$prot,$du)=@_;
    if($prot ne 'net')
    {
        if($prot eq 'tcp') {socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp'));}
        else {socket(SOCK, PF_INET, SOCK_DGRAM, getprotobyname('udp'));}
        my $paddr = sockaddr_in($port, inet_aton("127.0.0.1"));

        $con=0;
        if($prot eq 'udp')
        {
            send(SOCK, 0, 0, $paddr);
            eval {
                local $SIG{ALRM} = sub { die "alarm time out" };
                alarm 5;
                recv(SOCK, $con, 4, 0);
                alarm 0;
                1;
            } or $con=0;
        }
        else
        {
            $con=connect(SOCK, $paddr);
        }
        close SOCK;
    }
    else
    {
        $con=0;
        open(GREP,"-|") || exec "netstat -ln|grep .".$port;
        while (<GREP>){
            if($_=~/.$port /){$con++;}
        }
        close GREP;
    }
    if($port eq 67 && !$con && !$du){
        sleep(10);
        $con=check($port,$prot,$du+1);
    }
    if(!$con)
    {
        restart($port);
    }
    return $con;
}

my %work;
open (F1,"</home/kernel/sock.log");
while(<F1>)
{
    chomp;
    my @F=split / /;
    $work[$F[0]]=$F[1];
}
close(F1);
open (F1,">/home/kernel/sock.log");
while(($port,$prot) = each(%serv))
{
    $con=check($port,$prot,0);
    if($con){
        print F1 "$port 1\n";
        if($work[$port] eq "0"){
            system("/usr/bin/snmptrap -v 1 -c public $host .1.1 `hostname` 51 0 '' .1 s '$port up'");
        }
    }
    else {
        print F1 "$port 0\n";
        if($work[$port] eq "1"){
            system("/usr/bin/snmptrap -v 1 -c public $host .1.1 `hostname` 51 0 '' .1 s '$port down'");
        }
    }
    
}   
close(F1);

Last edited by troop; 03-28-2010 at 11:15 PM.
 
Old 03-30-2010, 12:36 AM   #3
bulgin
LQ Newbie
 
Registered: Mar 2010
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thanks Troop. Do you think you could provide me with the littlest of notations, like what each section does so I don't blow out my server? It looks like this is checking to see if a service is up and if not, restart it, but I cannot be sure. I only need to check one port number and see if it's active, if so, execute a script.
 
Old 03-30-2010, 05:57 AM   #4
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
You should change
Code:
my %serv=(8031=>'net', 8041=>'net',8051=>'net',8000=>'net');

sub restart {
    my ($port)=@_;
    if($port eq 8031) {system("/home/kernel/run_vlc_yamal start");}
    if($port eq 8041) {system("/home/kernel/run_vlc_eam22 start");}
    if($port eq 8051) {system("/home/kernel/run_vlc_yamal_c start");}
    if($port eq 8000) {system("rc-config restart icecast");}
    if($port eq 9001) {system("/home/kernel/run_flv2");}
}
...
    if(!$con)
    {
        restart($port);
    }
to
Code:
my %serv=(your_port=>'net');

sub restart {
    my ($port)=@_;
    if($port eq your_port) {system("your_command");}
}
...
    if($con) { restart($port); }
if a service on port your_port is active then execute your_command.

or you can just simplify to
Code:
#!/usr/bin/perl -w

my $port=your_port;
$con=0;
open(GREP,"-|") || exec "netstat -ln|grep .".$port;
while (<GREP>){
  if($_=~/.$port /){$con++;}
}
close GREP;
if($con) { execute your command } #if port is up

Last edited by troop; 03-30-2010 at 06:12 AM.
 
Old 03-30-2010, 11:34 AM   #5
bulgin
LQ Newbie
 
Registered: Mar 2010
Posts: 9

Original Poster
Rep: Reputation: Disabled
thank you Troop.

When I run it as:

#!/usr/bin/perl -w

my $port=21;
$con=0;
open(GREP,"-|") || exec "netstat -ln|grep .".$port;
while (<GREP>){
if($_=~/.$port /){$con++;}
}
close GREP;
if($con) { print "hello world 21 is in use!" } #if port is up

I receive the error:
Use of uninitialized value $_ in pattern match (m//) at ./check.pl line 7.
 
Old 03-31-2010, 12:31 AM   #6
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
Quote:
Use of uninitialized value $_ in pattern match (m//) at ./check.pl line 7.
it's just a warning so code is running

Code:
# cat test.pl 
#!/usr/bin/perl -w

my $port=21;
my $con=0;
open(GREP,"-|") || exec "netstat -ln|grep .".$port;
while (<GREP>){
if(defined $_ && $_=~/.$port /){$con++;}
}
close GREP;
if($con) { print "hello world 21 is in use!"; }

# ./test.pl
hello world 21 is in use!
# killall vsftpd
# ./test.pl
#

Last edited by troop; 03-31-2010 at 12:57 AM.
 
Old 04-01-2010, 10:42 PM   #7
bulgin
LQ Newbie
 
Registered: Mar 2010
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thank you so much for your help, Troop. I think that'll do it!
 
Old 04-01-2010, 11:31 PM   #8
bulgin
LQ Newbie
 
Registered: Mar 2010
Posts: 9

Original Poster
Rep: Reputation: Disabled
I may have spoken too soon. I startup my tftpd and see that port 69 is listening. I run this script:

#!/usr/bin/perl -w

my $port=69;
my $con=0;
open(GREP,"-|") || exec "netstat -ln|grep .".$port;
while (<GREP>){
if(defined $_ && $_=~/.$port /){$con++;}
}
close GREP;
if($con) { print "hello world 69 is in use!"; }

I then tftp into the server and GET a file.

The script is still just sitting there, nothing happens
 
Old 04-01-2010, 11:45 PM   #9
bulgin
LQ Newbie
 
Registered: Mar 2010
Posts: 9

Original Poster
Rep: Reputation: Disabled
I think I may have spoken too soon.

I fire up the tftpd daemon on port 69, then run the script indicating my $port=69;

Then I tftp and GET a file, and the script just sits there... nothing happens.

Thanks.
 
Old 04-02-2010, 02:26 AM   #10
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
post output
Code:
netstat -ln|grep :69
 
Old 04-02-2010, 10:01 AM   #11
bulgin
LQ Newbie
 
Registered: Mar 2010
Posts: 9

Original Poster
Rep: Reputation: Disabled
netstat -ln|grep :69

udp 0 0 0.0.0.0:69 0.0.0.0:*

Thanks for your help, Troop. I really appreciate it!
 
Old 04-04-2010, 03:52 AM   #12
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
strange
Code:
# cat test.pl
#!/usr/bin/perl -w
my $port=69;
my $con=0;
#open(GREP,"-|") || exec "netstat -ln|grep :".$port;
open(GREP,"-|") || exec "echo 'udp 0 0 0.0.0.0:69 0.0.0.0:*'";
while (<GREP>){
if($_=~/:$port /){$con++;}
}
close GREP;
if($con) { print "hello world 69 is in use!"; }
# ./1.pl
hello world 69 is in use!
Your perl works differently?
try
Code:
# cat test.sh 
#!/bin/sh
PORT=69;
CON=`netstat -ln|grep -c :$PORT`
if [ $CON -gt 0 ]; then
    echo "$PORT is in use";
fi
 
Old 04-05-2010, 05:58 PM   #13
bulgin
LQ Newbie
 
Registered: Mar 2010
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thank you! the following does work:

#!/usr/bin/perl -w
my $port=69;
my $con=0;
open(GREP,"-|") || exec "netstat -ln|grep :".$port;
while (<GREP>){
if($_=~/:$port /){$con++;}
}
close GREP;
if($con) { print "hello world 69 is in use!"; }

For my own education can you tell me what $con does, is about? I'm trying to learn. I've got the rest of it.
 
Old 04-05-2010, 07:36 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
$con counts cxns. Set to zero initially, (= false in an if() ). If $con gets incremented, a cxn was found, therefore its 'true' in the final if() .
 
Old 04-06-2010, 11:12 PM   #15
bulgin
LQ Newbie
 
Registered: Mar 2010
Posts: 9

Original Poster
Rep: Reputation: Disabled
Now using a variation of what works I tried, using ngrep, but it doesn't work.:

#!/usr/bin/perl -w
my $port=69;
my $con=0;
open(NGREP,"-|") || exec "ngrep|grep :".$port;
while (<NGREP>){
if($_=~/:$port /){$con++;}
}
close NGREP;
if($con) { print "Alert! Transfer on port 69!"; }


if I:
ngrep | grep 69

and then GET a file with tftp

I see output so I know ngrep with grep is working....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Now to send encrypted mails with a script or mail command robertjinx Linux - Server 4 03-06-2009 02:08 PM
command to monitor the Serial port smartgupta AIX 0 12-10-2008 05:53 AM
LXer: Perl script to monitor disk space and send an email alert LXer Syndicated Linux News 1 02-23-2007 02:12 PM
script/command to kill processes running on particular port varunbihani Linux - General 2 07-08-2005 04:35 AM
CGI script problem: Send/Expect command echo on webpage rajatgarg Programming 2 07-11-2004 05:38 PM


All times are GMT -5. The time now is 10:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration