skip directories in all_conf
[pxelator] / lib / PXElator / ssh.pm
1 package ssh;
2
3 use warnings;
4 use strict;
5
6 use Net::OpenSSH;
7 use English;
8 use Data::Dump qw/dump/;
9 use client;
10 use store;
11
12 my $id = 3;
13 my $id_rsa = '/root/.ssh/id_rsa';
14
15 sub copy_id {
16         my $ip = shift;
17         my $ssh = client::ip_path( $ip, 'ssh' );
18         return if -l $ssh;
19         my $id = $id_rsa . '.pub';
20         my $cmd = "sudo ssh-copy-id -i $id root\@$ip";
21         warn "# $cmd\n";
22         system $cmd;
23         warn "$id -> $ssh";
24         symlink $id, $ssh;
25 }
26
27 sub ethernet_bridge_to {
28         my $ip = shift;
29
30         die "you need to run this as root\n" unless $UID == 0;
31
32         copy_id $ip;
33
34         warn "# reset local IP address";
35         system "ifconfig virtual 172.16.10.$id";
36
37         warn "# connect to $ip";
38         my $ssh = Net::OpenSSH->new( $ip,
39                 master_opts => [ -i => $id_rsa, -w => "$id:$id", -o => 'Tunnel=ethernet' ],
40         );
41
42         foreach my $command ( "ifconfig tap$id up", "brctl addif virtual tap$id" ) {
43                 warn "# $command";
44                 system $command;
45                 $ssh->system( $command ) or die "$command ", $ssh->error;
46         }
47
48         warn "press enter to close tunnel to $ip from $id";
49         <STDIN>;
50
51         system "ifconfig virtual 172.16.10.1";
52
53 }
54
55 sub shell {
56         my $ip = shift;
57
58         copy_id $ip;
59
60         warn "# ssh $ip -i $id_rsa";
61         my $ssh = Net::OpenSSH->new( $ip,
62                 master_opts => [ -i => $id_rsa ],
63         );
64
65         my $html;
66         my @shell;
67
68         foreach my $command ( @_ ) {
69                 warn "root\@$ip:# $command\n";
70                 my ($out,$err) = $ssh->capture2( $command ) or die "$command ", $ssh->error;
71                 warn "$out\n$err";
72
73                 store::audit( $ip, $command, { ip => $ip, command => $command, out => $out, err => $err } );
74
75                 $html .= qq|<tt style="color: grey">root\@$ip:# <b>$command</b></tt><pre>$out</pre>|;
76                 $html .= qq|<pre style="color: red">$err</pre>| if $err;
77         }
78
79         return $html;
80 }
81
82 1;