Jemand zu Hause?
1
2
3
4
my $ssh = Net::OpenSSH->new( '10.10.10.10', {user => 'abc', passwd => 'geheim'} );
print $ssh->capture('pwd'); # /home/abc/
$ssh->capture('cd /tmp');
print $ssh->capture('pwd'); # /home/abc/
Wie frage ich & perlintro
brian's Leitfaden für jedes Perl-Problem
Net::OpenSSH, aber
Net::SSH2 ist da vielleicht hilfreicher. Das basiert auch libssh2 und funktioniert ziemlich gut (wenn man von der Komprimierung der Daten absieht, da unterstützen sie die vom Standard abweichenden Methoden von openssh nicht, ich habe dazu mal einen Thread eröffnet und GwenDragon hat's als Bug reported ... ich habe aber nicht verfolgt, ob es upstream je behoben wurde).1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
$ssh2->connect($server) or die $!; if ($ssh2->auth_publickey(@auth)) { my $chan = $ssh2->channel(); $chan->blocking(1); $chan->shell(); sleep(2); while(<$chan>) {}; sleep(1); if(!waitforready($chan)) { $chan->close; $ssh2->disconnect(); next SERVER; } print $chan "ps axuf\n"; sleep(5); my %counting = (); my @lines; while(my $line = <$chan>) { push @lines, $line; } } sub waitforready { my $chan = shift; print $chan 'echo "OKCOMPUTER"' . "\n"; my $foundanswer = 0; my $lookedforanswer = 0; while($foundanswer == 0 && $lookedforanswer < 10) { if(my $line = <$chan>) { if($line =~ m/OKCOMPUTER/) { $foundanswer = 1; return 1; } } sleep(1); $lookedforanswer++; } return 0; }
Quoteblocking ( flag )
Enable or disable blocking. Note that if blocking is disabled, methods that create channels may fail, e.g. channel, SFTP, scp_*.
1 2 3 4 5 6 7 8 9
my $ssh = Net::OpenSSH->new(...); my ($in, $out, $pid) = $ssh->open2(); # passing no cmd will start a shell print {$in} "ls\n"; while (<$out>) { print "out: $_"; last if /$prompt_re/; } print {$in} "pwd\n"; ...
QuoteCommand mode is designed to run single commands on the remote host. It opens an SSH channel between both hosts, ask the remote computer to run some given command and when it finnish the channel is closed. [...] and it is also the way Net::OpenSSH runs commands on the remote host.