perl - Can not send data to IO::Socket::INET in conditional statement -
i have issue communicating external system via io::socket::inet. try login , send multiple commands system unfortunately does'n work if command print in line 58 under conditional statement. conditional statements in case required handle response data.
package net::cli::cisco; use 5.006; use strict; use warnings fatal => qw(all); use io::socket::inet; use carp; use data::dumper; $| = 1; sub new { $class = shift; %args = @_; $self = bless { _host => $args{host} || carp('no hostname defined'), _username => $args{username} || carp('no username defined'), _password => $args{password} || carp('no password defined'), _logged_in => 0, }, $class; return $self; } sub connect { $self = shift; $host = $self->{_host}; $port = 23; $handle = io::socket::inet->new( proto => "tcp", peeraddr => $host, peerport => $port, type => sock_stream, timeout => 3 ) or die "can't connect port $port on $host: $!"; $shc = "\r\n"; $self->{shc} = $shc; $self->{handle} = $handle; } sub getinterface { ($self) = @_; $self->connect; @cmd_list = ( "sh clock", "sh ip int brief" ); $self->send_cmd(@cmd_list); } sub send_cmd { ( $self, @cmd_list ) = @_; $handle = $self->{handle}; $response; while ( $response = <$handle> ) { if ( $response =~ m/^username:/ ) { print "conditional statements exec done!\n"; print $handle $self->{_username} . $self->{shc}; } #print $handle $self->{_username} . $self->{shc}; print $response; print $handle $self->{_password} . $self->{shc}; print $handle "enable" . $self->{shc}; print $handle $self->{_password} . $self->{shc}; print $handle "term leng 0" . $self->{shc}; foreach $cmd (@cmd_list) { print $handle "$cmd" . $self->{shc}; } print $handle "exit" . $self->{shc}; } close($handle); } 1; $x = __package__->new( "host" => "1.1.1.1", "username" => "user", "password" => "pw" ); $x->getinterface;
well, can't see why code wrong. note: if recommend line 61 working fine. ideas?
after comment ikegami please find working subroutine below:
sub send_cmd { ( $self, @cmd_list ) = @_; $handle = $self->{handle}; $response; start: while ( $response = <$handle> ) { print $response; if ( $response =~ m/[^username:|^password:|\$%#:>]/ ) { print $handle $self->{_username} . $self->{shc}; print $handle $self->{_password} . $self->{shc}; print $handle "enable" . $self->{shc}; print $handle $self->{_password} . $self->{shc}; print $handle "term leng 0" . $self->{shc}; foreach $cmd (@cmd_list) { print $handle "$cmd" . $self->{shc}; } print $handle "exit" . $self->{shc}; } else { goto start; } } close($handle);
Comments
Post a Comment