How to login and access router from remote server using Net::OpenSSH perl package? -


i want access router remote server through perl script.

my perl script located on 1 server access remote server on remote server have accessibility of routers means 2 tier architecture there.

i have tried creating remote server tunnel , access router username , password no luck. should do?

update: following script

my $ssh = net::openssh->new('remotehostip', port => 3345, user => 'username', password => 'password', 'master_opts' => [-o => "stricthostkeychecking=no"]);  ($pty, $pid) = $ssh->open2pty(netcat => 'router_ip', 23);  $telnet = net::telnet::cisco->new(-fhopen => $pty,                                      -prompt => '/.*\$ $/',                                      -telnetmode => 0,                                      -cmd_remove_mode => 0,                                      -output_record_separator => "\r");  $telnet->waitfor(-match => '/username\:/', -errmode => "return") or die "login failed : ".$telnet->lastline; $telnet->send_wakeup("routerusername");  $telnet->waitfor(-match => '/password\:/', -errmode => "return") or die "login failed : ".$telnet->lastline; $telnet->send_wakeup("routerpassword");  @lines = $telnet->cmd(string => "sh ver", timeout => 10);  print @lines; $telnet->close; 

output:

use of /g modifier meaningless in split @ /usr/local/share/perl5/net/telnet/cisco.pm line 756.

|-----------------------------------------------------------------| | system use of authorized users only. | | individuals using computer system without authority, or in | | excess of authority, subject having of | | activities on system monitored , recorded system | | personnel. | | | | in course of monitoring individuals improperly using | | system, or in course of system maintenance, activities | | of authorized users may monitored. | | | | using system expressly consents such monitoring | | , advised if such monitoring reveals possible | | evidence of criminal activity, system personnel may provide | | evidence of such monitoring law enforcement officials. | |-----------------------------------------------------------------|

write error: filehandle isn't open @ test.pl line 45

try using never-released-to-cpan-but-functional net::openssh::gateway module.

it may able find way connect remote machine through gateway you!

use net::openssh; use net::openssh::gateway;  $ssh = net::openssh->new($host, user => $user, ...                             gateway => { proxy => "ssh://$gateway" }); 

update: seems (from code have posted below comment) actually, want telnet remote routers. net::openssh::gateway can not handle designed connecting remote ssh servers through proxies or gateways.

also, seems port forwarding disabled in gateway can not create tunnel using net::openssh::open_tunnel.

instead, have find program in gateway able connect remote router. instance, socat, netcat, telnet, etc. , call through net::openssh object.

in example:

my ($pty, $pid) = $ssh->open2pty(netcat => $host, $port)   or die "unable launch remote netcat: " . $ssh->error;  $telnet = net::telnet::cisco->new(-fopen => $pty,                                      -prompt => '/.*\$ $/',                                      -telnetmode => 0,                                      -cmd_remove_mode => 0,                                      -output_record_separator => "\r"); 

finally, note in code have posted bellow, wrongly using dots (.) instead of commas (,) in couple of places.

update 2: error handling code added.


Comments