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. |
My very old script on perl
Code:
#!/usr/bin/perl -w |
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.
|
You should change
Code:
my %serv=(8031=>'net', 8041=>'net',8051=>'net',8000=>'net'); Code:
my %serv=(your_port=>'net'); or you can just simplify to Code:
#!/usr/bin/perl -w |
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. |
Quote:
Code:
# cat test.pl |
Thank you so much for your help, Troop. I think that'll do it!
|
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 |
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. |
post output
Code:
netstat -ln|grep :69 |
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! |
strange
Code:
# cat test.pl try Code:
# cat test.sh |
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. |
$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() .
|
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.... |
All times are GMT -5. The time now is 01:50 AM. |