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

Hey All! I'm very new to Perl and coding in general. I've been trying to put together a script that to pass some basic ssh commands from a windows system to a Linux box. I was able to get this code to work without issue:

use warnings; use strict; use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect("hostname") or die "Unable to connect to host $@ \n"; $ssh2->auth_password('username','password') or die "Unable to login $@ +\n"; my $chan=$ssh2->channel(); $chan->shell(); print $chan "ifconfig\n"; print "LINE : $_" while <$chan>; print $chan "top\n"; print "LINE : $_" while <$chan>; $chan->close;

However the same basic code but edited to accept user input does not.

use warnings; use strict; use Net::SSH2; print "Hostame: "; my $host = <STDIN>; print "Username: "; my $user = <STDIN>; print "Password: "; my $pass = <STDIN>; my $ssh2 = Net::SSH2->new(); $ssh2->connect($host) or die "Unable to connect $host $@ \n"; $ssh2->auth_password($user,$pass) or die "Unable to login $@ \n"; my $chan=$ssh2->channel(); $chan->shell(); print $chan "ifconfig\n"; print "LINE : $_" while <$chan>; print $chan "top\n"; print "LINE : $_" while <$chan>; $chan->close;

That code returns an error of "No such host is known". Can someone help point out what i'm going wrong?

Replies are listed 'Best First'.
Re: SSH2 user input not working.
by stevieb (Canon) on Apr 26, 2016 at 00:53 UTC

    Hi Goggal0r, welcome to the Monastery!

    By default, reading from STDIN keeps the newlines intact, which very likely could be causing you the issues. You can use chomp to remove them. See if this helps:

    chomp(my $host = <STDIN>); print "Username: "; chomp(my $user = <STDIN>); print "Password: "; chomp(my $pass = <STDIN>);