Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Running interactive SSH session in windows

by srinidhi27 (Initiate)
on Aug 24, 2019 at 07:53 UTC ( #11104933=perlquestion: print w/replies, xml ) Need Help??

srinidhi27 has asked for the wisdom of the Perl Monks concerning the following question:

Team, i know this has been in discussion for long time. But i haven't got an answer searching entire web since yesterday. I need to run series of commands in windows after logging to an server running SSH server.

The problem is as you might have guessed being interactive.....

I can run one of commands bby lot of methods.. I will list what i have tried so that people can suggest something i don't know.

1. Net::SSH2 -- not supported in windows

2. Net:OpenSSH -- again not supported in windows

3. Install openSSH in widnows itself and using System and backticks to run dos commands. Yes Openssh can be installed in windows

without cgywin environment a discovery i made yesterday.

4. IPC::Cmd, a tool similar to using system command but doesn't provide interactive session. Everytime you run new terminal session is spawned.

5. Ofcourse tried plain system and exec and backticks also turned futile. Worked liked a charm for single command run and shutoff.

6. IPC::Session, not avaiable in Active Perl i use and don't know how to install it currently. Supposedly provides for interactive session using terminal. Not been able to get it working..

7. plink tool is also good failing to work even when list of commands is fed to it externally. Cisco Box i am trying to SSH to is throwing illegal characters error. Weird right?

Any leads is appreciated..

  • Comment on Running interactive SSH session in windows

Replies are listed 'Best First'.
Re: Running interactive SSH session in windows
by VinsWorldcom (Prior) on Aug 24, 2019 at 22:12 UTC

    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 }
      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.
Re: Running interactive SSH session in windows
by marto (Archbishop) on Aug 24, 2019 at 09:55 UTC

    "Net::SSH2 -- not supported in windows"

    Are you sure? It ships with Strawberry Perl.

      I have a limitation to use Active Perl 5.16 only. I cant uprade it for my reasons in my company

        You don't say what problems you had installing, just that it doesn't work on windows. See BUILDING.WIN32.

Re: Running interactive SSH session in windows
by 1nickt (Abbot) on Aug 24, 2019 at 12:53 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11104933]
Approved by haukex
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2019-10-23 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?