1 |
package ssh; |
2 |
|
3 |
use Net::OpenSSH; |
4 |
use English; |
5 |
use Data::Dump qw/dump/; |
6 |
|
7 |
my $id = 2; |
8 |
|
9 |
sub ethernet_bridge_to { |
10 |
my $host = shift; |
11 |
|
12 |
die "you need to run this as root\n" unless $UID == 0; |
13 |
|
14 |
warn "# reset local IP address"; |
15 |
system "ifconfig virtual 172.16.10.$id"; |
16 |
|
17 |
warn "# connect to $host"; |
18 |
my $ssh = Net::OpenSSH->new( $host, |
19 |
master_opts => [ -w => "$id:$id", -o => 'Tunnel=ethernet' ], |
20 |
); |
21 |
|
22 |
foreach my $command ( "ifconfig tap$id up", "brctl addif virtual tap$id" ) { |
23 |
warn "# $command"; |
24 |
system $command; |
25 |
$ssh->system( $command ) or die "$command ", $ssh->error; |
26 |
} |
27 |
|
28 |
warn "press enter to close tunnel to $host from $id"; |
29 |
<STDIN>; |
30 |
|
31 |
} |
32 |
|
33 |
sub shell { |
34 |
my $host = shift; |
35 |
|
36 |
my $ssh = Net::OpenSSH->new( $host ); |
37 |
|
38 |
my $html; |
39 |
|
40 |
foreach my $command ( @_ ) { |
41 |
warn "root\@$host:# $command\n"; |
42 |
my ($out,$err) = $ssh->capture2( $command ) or die "$command ", $ssh->error; |
43 |
warn "$out\n$err"; |
44 |
|
45 |
$html .= qq|<tt style="color: grey">root\@$host:# <b>$command</b></tt><pre>$out</pre>|; |
46 |
$html .= qq|<pre style="color: red">$err</pre>| if $err; |
47 |
} |
48 |
|
49 |
return $html; |
50 |
} |
51 |
|
52 |
sub copy_id { |
53 |
my $host = shift; |
54 |
system 'sudo ssh-copy-id -i /root/.ssh/id_rsa.pub root@' . $host; |
55 |
} |
56 |
|
57 |
1; |