Thread: [Ssh-sftp-perl-users] Net::SSH2 example
Brought to you by:
dbrobins
From: Rob V. <rob...@br...> - 2006-02-13 13:52:34
|
Hi everyone, I'm trying to get Net::SSH2 to work but I'm kinda stuk after the connection is made. Could anyone provide an example on how to set up a connection, give a remote command, and then read the output of that command(either stdout or stderr) I would also apreciate an example of the use of chan->shell(); on how to put several commands into that one and how to read the output of those commands (stderr and stdout again) Thanx Rob |
From: Bryan B. <br...@bu...> - 2006-02-13 19:40:08
|
The best I could come up with is this: --- snip ---> use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect("127.0.0.1"); $ssh2->auth(username => scalar getpwuid($<), interact => 1) or die; print "\n"; my $chan = $ssh2->channel(); $chan->shell(); print $chan "uptime\n"; print $chan "who\n"; print "LINE: $_" while <$chan>; <--- end --- The problem is that once you read from the channel, you no longer seem to be able to interact with it. So in the above code, you will see your uptime and who output, but in the following, all you see is your uptime output: --- snip ---> print $chan "uptime\n"; print "LINE: $_" while <$chan>; print "LINE: $_" while <$chan>; print $chan "who\n"; <--- end --- I suppose this is a bug, but its been difficult to get anything more meaningfull then "read operations seem funny". And I just havnt had time to play with it more. I hope this gets you further then you were before. Bryan http://www.sourceforge.net/projects/rover > Hi everyone, > > I'm trying to get Net::SSH2 to work but I'm kinda stuk after the > connection is made. > > Could anyone provide an example on how to set up a connection, give a > remote command, and then read the output of that command(either stdout > or stderr) > > I would also apreciate an example of the use of chan->shell(); on how to > put several commands into that one and how to read the output of those > commands (stderr and stdout again) > > Thanx > Rob > > > > ------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. Do you grep through log > files > for problems? Stop! Download the new AJAX search engine that makes > searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642 > _______________________________________________ > Ssh-sftp-perl-users mailing list > Ssh...@li... > https://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users > |
From: Rutger O. <ov...@us...> - 2006-02-13 20:36:46
|
BB> The problem is that once you read from the channel, you no longer seem to BB> be able to interact with it. So in the above code, you will see your BB> uptime and who output, but in the following, all you see is your uptime BB> output: Have you tried $chan->blocking(1); after the $chan->shell; call? |
From: Rob V. <rob...@br...> - 2006-02-14 13:45:55
|
Hi there, Many thanx for the example, I got it to work. This is the code I used: --Start--> #!/usr/bin/perl use warnings; use strict; use Net::SSH2; my $ssh2 = Net::SSH2->new(); print "connect fail\n" unless ($ssh2->connect('testbox')); print "password fail\n" unless ($ssh2->auth_keyboard('root','*******')); my $chan=$ssh2->channel(); $chan->shell(); print $chan "uname -a\n"; print "LINE : $_" while <$chan>; print $chan "who\n"; print "LINE : $_" while <$chan>; $chan->close; <--end-- and the output: --start--> LINE : Linux testbox 2.6.5-7.111.19-smp #1 SMP Fri Dec 10 15:10:58 UTC 2004 i686 i686 i386 GNU/Linux LINE : root pts/0 Feb 14 14:33 (robspc) <--end-- As you can see I can push more than one command to a channel after I read from the channel. Don't know why this isn't working for you, if you want to know what version libs I use let me know which ones you want hear about. Thanx Rob Op ma, 13-02-2006 te 09:39 -0500, schreef Bryan Bueter: > The best I could come up with is this: > > --- snip ---> > use Net::SSH2; > > my $ssh2 = Net::SSH2->new(); > $ssh2->connect("127.0.0.1"); > $ssh2->auth(username => scalar getpwuid($<), interact => 1) or die; > print "\n"; > > my $chan = $ssh2->channel(); > $chan->shell(); > > print $chan "uptime\n"; > print $chan "who\n"; > print "LINE: $_" while <$chan>; > <--- end --- > > The problem is that once you read from the channel, you no longer seem to > be able to interact with it. So in the above code, you will see your > uptime and who output, but in the following, all you see is your uptime > output: > > --- snip ---> > print $chan "uptime\n"; > print "LINE: $_" while <$chan>; > > print "LINE: $_" while <$chan>; > print $chan "who\n"; > <--- end --- > > I suppose this is a bug, but its been difficult to get anything more > meaningfull then "read operations seem funny". And I just havnt had time > to play with it more. > > I hope this gets you further then you were before. > > > Bryan > http://www.sourceforge.net/projects/rover > > > Hi everyone, > > > > I'm trying to get Net::SSH2 to work but I'm kinda stuk after the > > connection is made. > > > > Could anyone provide an example on how to set up a connection, give a > > remote command, and then read the output of that command(either stdout > > or stderr) > > > > I would also apreciate an example of the use of chan->shell(); on how to > > put several commands into that one and how to read the output of those > > commands (stderr and stdout again) > > > > Thanx > > Rob > > > > > > > > ------------------------------------------------------- > > This SF.net email is sponsored by: Splunk Inc. Do you grep through log > > files > > for problems? Stop! Download the new AJAX search engine that makes > > searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! > > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642 > > _______________________________________________ > > Ssh-sftp-perl-users mailing list > > Ssh...@li... > > https://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users > > > > > > > ------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. Do you grep through log files > for problems? Stop! Download the new AJAX search engine that makes > searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642 > _______________________________________________ > Ssh-sftp-perl-users mailing list > Ssh...@li... > https://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users |
From: Rob V. <rob...@br...> - 2006-02-14 14:35:58
|
Hi, It seems that I managed to reproduce the bug you found Bryan. (the blocking flag wont work for me) If I use Net::SSH2 to connect to a suse linux 9.1 professional machine, I can issue multiple commands to the shell and read the channel and issue more commands. However if I try this on a debian machine it fails, I have to close the channel and open it again. Maybe the ssl version running on the remote machine makes a difference. ssl&ssh versions on suse box: sshd version OpenSSH_3.8p1, OpenSSL 0.9.7d kernel on suse box: Linux fil996 2.6.5-7.111.19-smp #1 SMP Fri Dec 10 15:10:58 UTC 2004 i686 i686 i386 GNU/Linux17 Mar 2004 ssl&ssh versions on debian box: OpenSSH_3.8.1p1 Debian-8.sarge.4, OpenSSL 0.9.7e 25 Oct 2004 kernel on debian box: Linux eco 2.4.31 #1 Thu Sep 15 12:11:20 CEST 2005 i686 GNU/Linux Regards Rob Op di, 14-02-2006 te 14:44 +0100, schreef Rob Verduijn: > Hi there, > > Many thanx for the example, I got it to work. > > This is the code I used: > > --Start--> > #!/usr/bin/perl > use warnings; > use strict; > > use Net::SSH2; > > my $ssh2 = Net::SSH2->new(); > > print "connect fail\n" unless ($ssh2->connect('testbox')); > print "password fail\n" unless > ($ssh2->auth_keyboard('root','*******')); > > my $chan=$ssh2->channel(); > $chan->shell(); > print $chan "uname -a\n"; > print "LINE : $_" while <$chan>; > print $chan "who\n"; > print "LINE : $_" while <$chan>; > $chan->close; > <--end-- > > > and the output: > --start--> > LINE : Linux testbox 2.6.5-7.111.19-smp #1 SMP Fri Dec 10 15:10:58 UTC > 2004 i686 i686 i386 GNU/Linux > LINE : root pts/0 Feb 14 14:33 (robspc) > <--end-- > > As you can see I can push more than one command to a channel after I > read from the channel. > Don't know why this isn't working for you, if you want to know what > version libs I use let me know which ones you want hear about. > > Thanx > Rob > > > > > > Op ma, 13-02-2006 te 09:39 -0500, schreef Bryan Bueter: > > The best I could come up with is this: > > > > --- snip ---> > > use Net::SSH2; > > > > my $ssh2 = Net::SSH2->new(); > > $ssh2->connect("127.0.0.1"); > > $ssh2->auth(username => scalar getpwuid($<), interact => 1) or die; > > print "\n"; > > > > my $chan = $ssh2->channel(); > > $chan->shell(); > > > > print $chan "uptime\n"; > > print $chan "who\n"; > > print "LINE: $_" while <$chan>; > > <--- end --- > > > > The problem is that once you read from the channel, you no longer seem to > > be able to interact with it. So in the above code, you will see your > > uptime and who output, but in the following, all you see is your uptime > > output: > > > > --- snip ---> > > print $chan "uptime\n"; > > print "LINE: $_" while <$chan>; > > > > print "LINE: $_" while <$chan>; > > print $chan "who\n"; > > <--- end --- > > > > I suppose this is a bug, but its been difficult to get anything more > > meaningfull then "read operations seem funny". And I just havnt had time > > to play with it more. > > > > I hope this gets you further then you were before. > > > > > > Bryan > > http://www.sourceforge.net/projects/rover > > > > > Hi everyone, > > > > > > I'm trying to get Net::SSH2 to work but I'm kinda stuk after the > > > connection is made. > > > > > > Could anyone provide an example on how to set up a connection, give a > > > remote command, and then read the output of that command(either stdout > > > or stderr) > > > > > > I would also apreciate an example of the use of chan->shell(); on how to > > > put several commands into that one and how to read the output of those > > > commands (stderr and stdout again) > > > > > > Thanx > > > Rob > > > > > > > > > > > > ------------------------------------------------------- > > > This SF.net email is sponsored by: Splunk Inc. Do you grep through log > > > files > > > for problems? Stop! Download the new AJAX search engine that makes > > > searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! > > > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642 > > > _______________________________________________ > > > Ssh-sftp-perl-users mailing list > > > Ssh...@li... > > > https://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users > > > > > > > > > > > > > ------------------------------------------------------- > > This SF.net email is sponsored by: Splunk Inc. Do you grep through log files > > for problems? Stop! Download the new AJAX search engine that makes > > searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! > > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642 > > _______________________________________________ > > Ssh-sftp-perl-users mailing list > > Ssh...@li... > > https://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users > > > > ------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. Do you grep through log files > for problems? Stop! Download the new AJAX search engine that makes > searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642 > _______________________________________________ > Ssh-sftp-perl-users mailing list > Ssh...@li... > https://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users |