Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Running interactive SSH session in windows

by VinsWorldcom (Prior)
on Aug 24, 2019 at 22:12 UTC ( #11104962=note: print w/replies, xml ) Need Help??

in reply to Running interactive SSH session in windows

Indeed, as marto mentions above - Net::SSH2 does work on Windows - it's included with Strawberry Perl. It's actually the only one I've gotten to work on Windows (kudos to you for getting Net::SSH::Perl to work - I've never got it and it's endless dependencies installed on Windows).

You mentioned Cisco - if that's the ultimate box you're trying to get to, did you look at Net::SSH2::Cisco which is a drop in replacement for Net::Telnet::Cisco, relies on Net::SSH2 as the backend and definitely works on Windows - I know, I "wrote" it and used it exclusively on Windows. NOTE: It will have troubles with newer IOS-XE/XR and / or NXOS operating systems, it worked fine on regular old IOS. For that matter, again if it's Cisco, have a look at CRAPPS.

I've used the following for interactive sessions to Linux and Cisco devices:

use strict; use warnings; use Net::SSH2; use Term::ReadLine; my $host = 'hostname'; my $user = 'username'; my $pass = 'password'; my $ssh2 = Net::SSH2->new(); if ($ssh2->connect($host, 22)) { if ($ssh2->auth_password($user, $pass)) { my $chan = $ssh2->channel(); $chan->blocking(0); # Needed on Windows $chan->shell(); binmode($chan) # Seems to take care of both Unix SSH and Cisco + servers # Also set binmode in Net::Telnet::Cisco (\n vs + \r\n) # Needed to "clear", flush() doesn't seem to work # Otherwise, first command does not work chan_read($chan); # START: Interactive Mode my $prompt = "ssh> "; my $ssh = Term::ReadLine->new("SSH"); $ssh->ornaments(0); # Main loop. This is inspired from the POD page of Term::Readl +ine. while (defined (my $cmd = $ssh->readline($prompt))) { chomp $cmd; # nothing if ($cmd =~ /^\s*$/) { next } # exit if ($cmd =~ /^\s*exit\s*$/) { last } cmd($chan, $cmd) } $chan->close } else { warn "Authentication failed\n" } } else { warn "Unable to connect to host $host: $!\n" } sub cmd { my ($chan, $cmd) = @_; chomp $cmd; $chan->write("$cmd\n"); select(undef,undef,undef,0.2); # or sleep 1 my $ret = chan_read($chan, $cmd); print $ret } sub chan_read { my ($chan, $cmd) = @_; # need actual polling method rather than select or sleep to wait f +or a bit # example from the Net::SSH2 code, but doesn't seem to work: # # my @poll = ({handle => $chan, events => 'in'}); # if($ssh2->poll(250, \@poll) && $poll[0]->{revents}->{in}) { # print "HERE\n" # } # # or select(): # # my ($rin, $rout, $ein, $eout) = ('', '', '', ''); # vec($rin, $chan, 1) = 1; # tried $ssh2 and $chan, both "worked" # # should be: fileno($var) # if (select($rout=$rin, undef, $eout=$ein, 5000)) { # print $buf while defined ($len = $chan->read($buf,512)) # } my $DONE = 0; my $buffer; # Read until $DONE while (1) { last if $DONE; my $buf; # Read chunk while (defined (my $len = $chan->read($buf,512))) { # Found $PROMPT - then $DONE if ($buf =~ $PROMPT) { $buf =~ s/$PROMPT//; $DONE++; } $buffer .= $buf; } } # Remove $cmd from first line so no echo back if ((defined $cmd) and ($buffer =~ $cmd)) { $buffer =~ s/^$cmd\r\n//; } return $buffer }

Replies are listed 'Best First'.
Re^2: Running interactive SSH session in windows
by srinidhi27 (Initiate) on Aug 25, 2019 at 07:58 UTC
    Many thanks for this approach, but my company use Active Perl 5.16 only and i am not allowed to change it. I can install any module it offers me though.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11104962]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2021-01-23 21:15 GMT
Find Nodes?
    Voting Booth?