Re: [Ssh-sftp-perl-users] Net::SSH2::Channel read and write methods
Brought to you by:
dbrobins
From: Rob V. <rob...@br...> - 2006-03-16 07:51:19
|
Hi there, I asked the same kinda question and did get an answer from Bryan Bueter. With his help I managed to come up with the following example: ###########begin #!/usr/bin/perl # use warnings and strict to force neat coding, # and prevent bugs in the future use warnings; use strict; use Net::SSH2; # some variables my $remotehost="10.9.8.7"; my $remotefile="/etc/passwd"; my $localfile="copyoffpasswd"; # this is not smart , make sure your scripts can run as non-root my $login="root"; # this is not smart , plain text password, use a keypair my $password="verysecretpasswordinplaintext"; my $chan; # creating the ssh2 object my $ssh2 = Net::SSH2->new(); # connecting and logging in to the machine die "connect fail" unless ($ssh2->connect($remotehost)); die "password fail" unless ($ssh2->auth_password($login,$password)); # using exec to execute a command $chan=$ssh2->channel(); $chan->exec('uname -a'); print "EXEC : $_" while <$chan>; # don't forget to close since you can do only 1 exec $chan->close; # this is a get example, I think you can figure out the put version $ssh2->scp_get($remotefile,$localfile); # writing to a channel # a new channel, remember the last one is closed $chan=$ssh2->channel(); $chan->shell(); print $chan "uptime\n"; print $chan "who\n"; # reading from a channel print "SHELL: $_" while <$chan>; # lets see what the output is # clean up afterwards $chan->close; ########end |