Leser: 19
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
#!/usr/bin/perl use Net::SSH2; my $IP = "192.168.1.2"; my $PRIVKEY = "/home/user/.ssh/id_rsa"; my $PUBKEY = "/home/user/.ssh/id_rsa.pub"; my $USER = "remoteUser"; my $PASS = "mySecretPassphrase"; my $ssh = Net::SSH2->new(); $ssh->connect($IP) or die("connect(): $!\n"); print " * connected\n"; $ssh->auth_publickey($USER,$PUBKEY,$PRIVKEY,$PASS); print "DEBUG - post authentication\n"; print " * authenticated\n" if($ssh->auth_ok); my $channel = $ssh->channel() or die("channel(): $!\n"); print " * got channel\n"; $channel->shell(); print " * got shell\n"; print $channel "ls -a\n"; while(<$channel>) { print ">$_<"; } $channel->close()
Quote# ./key-auth.pl
* connected
DEBUG - post authentication
$ssh->error && print "#SSH-Fehler: " .$ssh->error;
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
#!/usr/bin/perl use Net::SSH2; my $IP = "123.4.5.6"; my $PRIVKEY = "/home/user/.ssh/id_rsa"; my $PUBKEY = "/home/user/.ssh/id_rsa.pub"; my $USER = "remoteUser"; my $ssh = Net::SSH2->new(); $ssh->connect($IP,22) or die("connect(): $!\n"); $ssh->error && print "Connect: " . $ssh->error . "\n"; $ssh->auth_publickey($USER,$PUBKEY,$PRIVKEY,'foobar'); $ssh->error && print "Auth: " . $ssh->error . "\n"; die("authentication failed\n") unless($ssh->auth_ok); my $channel = $ssh->channel() or die("channel(): $!\n"); $ssh->error && print "Channel: " . $ssh->error . "\n"; $channel->shell(); $ssh->error && print "Shell: " . $ssh->error . "\n"; print $channel "uname -a\n"; while(<$channel>) { print "$_"; } $channel->close();
1
2
3
4
5
6
$ ./key-auth.pl
Connect: -37
Auth: -37
Channel: -37
Shell: -37
Linux foobar 2.6.29.6-smp #2 SMP Mon Aug 17 00:52:54 CDT 2009 i686 Intel(R) Pentium(R) 4 CPU 2.66GHz GenuineIntel GNU/Linux
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
0 "NONE",
1 "SOCKET_NONE",
2 "BANNER_NONE",
3 "BANNER_SEND",
4 "INVALID_MAC",
5 "KEX_FAILURE",
6 "ALLOC",
7 "SOCKET_SEND",
8 "KEY_EXCHANGE_FAILURE",
9 "TIMEOUT",
10 "HOSTKEY_INIT",
11 "HOSTKEY_SIGN",
12 "DECRYPT",
13 "SOCKET_DISCONNECT",
14 "PROTO",
15 "PASSWORD_EXPIRED",
16 "FILE",
17 "METHOD_NONE",
18 "PUBLICKEY_UNRECOGNIZED",
19 "PUBLICKEY_UNVERIFIED",
20 "CHANNEL_OUTOFORDER",
21 "CHANNEL_FAILURE",
22 "CHANNEL_REQUEST_DENIED",
23 "CHANNEL_UNKNOWN",
24 "CHANNEL_WINDOW_EXCEEDED",
25 "CHANNEL_PACKET_EXCEEDED",
26 "CHANNEL_CLOSED",
27 "CHANNEL_EOF_SENT",
28 "SCP_PROTOCOL",
29 "ZLIB",
30 "SOCKET_TIMEOUT",
31 "SFTP_PROTOCOL",
32 "REQUEST_DENIED",
33 "METHOD_NOT_SUPPORTED",
34 "INVAL",
35 "INVALID_POLL_TYPE",
36 "PUBLICKEY_PROTOCOL",
37 "EAGAIN"