An easy way to track state of network connection using Perl
2012-08-09 23:17
666 查看
Tonight, just paste some code here. I'll comment these code when I'm free.
https://gist.github.com/3305055
8/11/2012 Update
Background
Unix Shell can be timed out after setting a environment variable TMOUT, I want to verify if this feature is implemented correctly on my system. so I create a scenario that connect to remote server through Telnet, and issue TMOUT=xx after logging on to Shell,
then issue command "netstat -na" repeatedly to observe if the telnet is available that 's in 'established' state as below.
TCP xx.xx.xx.xx:62453 yy.yy.yy.yy:23
ESTABLISHED
Obviously, it's a tedious work to check connection state manually.
Solution
As a lazy man, I wrote a Perl script to complete this task automatically ^_^. Here I just used a common module Net::Telnet. Other things like regex, grep, qx, split are built-in functions and syntax in Perl.
At first, save a snapshot of telnet connections into a array @conns_old. Thanks to qx and grep syntax, I can do it just in a line.
my@conns_old=grep{/:23\s+/}qx(netstat
-na);
Then, connect to Unix server through Telnet. Need to invoke some interface introduced by Net::Telnet.
my$t=Net::Telnet->new(Timeout=>60,
Prompt=>'/#|>|&/');
$t->open($hostname);
$t->login($username,$password)ordie"Failed
to connect";
my@conns_new=grep{/:23\s+/}qx(netstat
-na);
After logging on to Shell successfully, capture Telnet connections(text) again, here we get a new array.
my@conns_new=grep{/:23\s+/}qx(netstat
-na);
Finally, just compare these two arrays @conns_old and @conns_new. The connection exists in the new array but not in the old array would be the new one created just now.
my($conn)=grep{!defined$existed{$_}}@conns_new;
What's the left thing is to check state of the new one again and again util it turns to other states TIME_WAIT, CLOSE_WAITor CLOSED. BTW, as a Perl newbie, I indeed love this syntactic sugar, split/\s+/,$conn)[4],
Perl guys are all lazy men.
Discussion
I know you may say my implementation is not good that's not exactly and effectively, and it's better to invoke some socket API ... Yes, that's true,
this is not the best way, but should be an easy way, just right so I can complete my work without overwork :)
use Net::Telnet;use strict;
my $hostname = 'xxx';my $username = "xx";my $password = "xx";my @conns_old = grep { /:23\s+/ } qx(netstat -na);my %existed = map { $_ => 1 } @conns_old;
foreach(@conns_old){ print "$_\n";}
my $t = Net::Telnet->new( Timeout => 60, Prompt => '/#|>|&/');print "Connect to $hostname\n";
$t->open($hostname);$t->login($username, $password) or die "Failed to connect";
my $cmd_logs = q(TMOUT=60);my $str = $t->cmd($cmd_logs);
my @conns_new = grep { /:23\s+/ } qx(netstat -na);foreach(@conns_new){ print "$_\n";}
my ($conn) = grep { !defined $existed{$_} } @conns_new;
my $cnt = 0;while((split /\s+/, $conn)[4] eq "ESTABLISHED"){ ++$cnt; print "${cnt}: $conn\n"; sleep 1; @conns_new = grep { /:23\s+/ } qx(netstat -na); my $token = (split /\s+/, $conn)[3]; ($conn) = grep { /$token/ } @conns_new;}
$t->close;
https://gist.github.com/3305055
8/11/2012 Update
Background
Unix Shell can be timed out after setting a environment variable TMOUT, I want to verify if this feature is implemented correctly on my system. so I create a scenario that connect to remote server through Telnet, and issue TMOUT=xx after logging on to Shell,
then issue command "netstat -na" repeatedly to observe if the telnet is available that 's in 'established' state as below.
TCP xx.xx.xx.xx:62453 yy.yy.yy.yy:23
ESTABLISHED
Obviously, it's a tedious work to check connection state manually.
Solution
As a lazy man, I wrote a Perl script to complete this task automatically ^_^. Here I just used a common module Net::Telnet. Other things like regex, grep, qx, split are built-in functions and syntax in Perl.
At first, save a snapshot of telnet connections into a array @conns_old. Thanks to qx and grep syntax, I can do it just in a line.
my@conns_old=grep{/:23\s+/}qx(netstat
-na);
Then, connect to Unix server through Telnet. Need to invoke some interface introduced by Net::Telnet.
my$t=Net::Telnet->new(Timeout=>60,
Prompt=>'/#|>|&/');
$t->open($hostname);
$t->login($username,$password)ordie"Failed
to connect";
my@conns_new=grep{/:23\s+/}qx(netstat
-na);
After logging on to Shell successfully, capture Telnet connections(text) again, here we get a new array.
my@conns_new=grep{/:23\s+/}qx(netstat
-na);
Finally, just compare these two arrays @conns_old and @conns_new. The connection exists in the new array but not in the old array would be the new one created just now.
my($conn)=grep{!defined$existed{$_}}@conns_new;
What's the left thing is to check state of the new one again and again util it turns to other states TIME_WAIT, CLOSE_WAITor CLOSED. BTW, as a Perl newbie, I indeed love this syntactic sugar, split/\s+/,$conn)[4],
Perl guys are all lazy men.
Discussion
I know you may say my implementation is not good that's not exactly and effectively, and it's better to invoke some socket API ... Yes, that's true,
this is not the best way, but should be an easy way, just right so I can complete my work without overwork :)
use Net::Telnet;use strict;
my $hostname = 'xxx';my $username = "xx";my $password = "xx";my @conns_old = grep { /:23\s+/ } qx(netstat -na);my %existed = map { $_ => 1 } @conns_old;
foreach(@conns_old){ print "$_\n";}
my $t = Net::Telnet->new( Timeout => 60, Prompt => '/#|>|&/');print "Connect to $hostname\n";
$t->open($hostname);$t->login($username, $password) or die "Failed to connect";
my $cmd_logs = q(TMOUT=60);my $str = $t->cmd($cmd_logs);
my @conns_new = grep { /:23\s+/ } qx(netstat -na);foreach(@conns_new){ print "$_\n";}
my ($conn) = grep { !defined $existed{$_} } @conns_new;
my $cnt = 0;while((split /\s+/, $conn)[4] eq "ESTABLISHED"){ ++$cnt; print "${cnt}: $conn\n"; sleep 1; @conns_new = grep { /:23\s+/ } qx(netstat -na); my $token = (split /\s+/, $conn)[3]; ($conn) = grep { /$token/ } @conns_new;}
$t->close;
相关文章推荐
- An example of how to streaming audio over network using Qt
- An easy way to update (patch) Linux kernel using yum
- An easy way to syncTime using C#
- Hack the Stack: Using Snort and Ethereal to Master the 8 Layers of an Insecure Network [ILLUSTRATED]
- An Extensive Examination of LINQ: Querying and Searching XML Documents Using LINQ to XML
- An easy instance of using the template class in C++
- An Introduction to Language Processing with Perl and Prolog: An Outline of Theories, Implementation,
- The driver was unable to create a connection due to an inability to establish the client portion of
- How to Make an HTTP Connection Using TCP/IP with RSocket
- No way to map to an envelop schema without use of orchiestration.
- Are you looking for a way to get the entire text of a word document into a RichEdit without using the Clipboard?
- more safe way to assign a property value of an object to a variable
- Java Secret: Using an enum to build a State machine(Java秘术:用枚举构建一个状态机)
- Whats the best way to split an array in ruby into multiple smaller arrays of random size
- How to sort an arbitrarily large set of data using Hadoop?
- An effective way to use pattern to instead of multiple if else statements
- An example of using libexif to set JPEG Orientation Tag
- An easy way to create transparent button
- An easy example of using AJAX
- An easy way to install and uninstall .Net Windows Service