#!/usr/bin/perl -w # # test # ------ # Test connection to equipment via TCP/IP use vars qw( $VERSION ); use Getopt::Long; use File::Basename; use Net::Telnet; use strict; use warnings; local $|=1; $VERSION = 20110214; # Process options and input arguments Getopt::Long::Configure( "prefix_pattern=(-|\/)" ); my %Config; my $Result = GetOptions( \%Config, qw( ip|i=s port|p=s help|?|h ) ); $Config{help} = 1 if( ! $Result ); if ( $Config{help} || not exists($Config{ip}) ) { Syntax(); exit(); } my $hostname; my $hostport; $hostname=$Config{ip}; $hostport=$Config{port}; # Connect to receiver my $file1="debug.out"; my $file2="input.out"; my $Rtelnet = new Net::Telnet ( Timeout=>1, Errmode=>'die', Prompt => '/\>/', Binmode=>1,Dump_log=>$file1,Input_log=>$file2,Telnetmode=>0); $Rtelnet->open(host => $hostname, port => $hostport); # Stop previous output and clear buffers $Rtelnet->put("SSSSSSSSSS\n"); $Rtelnet->buffer_empty; # Send an empty line WHY??-otheriwse it does not work... $Rtelnet->cmd(''); # find out the name of the port (format IP##) my $prompt; my $tryout=0; my $Rdevice; while ( $tryout <= 5 ) { my @lines2=$Rtelnet->cmd(''); $prompt=pop(@lines2); if (!(defined($prompt))) { print "Read on IP port timed out while testing for prompt\n"; } elsif ( $prompt =~ /(\S\S\d\d)/i ) { $Rdevice=$1; last; } $tryout++; } if ( $tryout > 5 ){ die "Unable to determine the prompt"; } $prompt=$Rdevice.">"; my $Rreprompt='/'.$prompt.'$/i'; print "Prompt is [$prompt]\n" ; # Set prompt as match operators (for record seperators) $Rtelnet->prompt($Rreprompt); # Start command line mode print "Connected to $hostname in command mode; enter command, quit or exit\n"; print $prompt; my $data; while () { chomp; last if (/^quit$/i || /^exit$/i); $data=Rcmd($_); print "$data"; print $prompt; } # End of command mode sub Rcmd{ my ($cmd)=@_; my $buffer; my @lines; @lines = $Rtelnet->cmd(String=>$cmd); $buffer=join('',@lines); return $buffer; } sub Syntax{ my( $Script ) = ( $0 =~ m#([^\\/]+).pl$# ); my $Line = "-" x length( $Script ); print << "EOT"; $Script (Version: $VERSION) $Line Download, mirror and remove internal data files. Syntax: $Script -i [-p -h] -i ...IP name/address. -p .......IP port to conect to. -?|h|help.......This help. Examples: $Script -i 192.168.0.1 -p 8888 EOT }