upgrade to Webconverger 5.5
[pxelator] / lib / PXElator / ssh.pm
index 9ed0931..7895593 100644 (file)
@@ -1,22 +1,42 @@
 package ssh;
 
+use warnings;
+use strict;
+
 use Net::OpenSSH;
 use English;
 use Data::Dump qw/dump/;
+use client;
+use CouchDB;
+
+my $id = 3;
+my $id_rsa = '/root/.ssh/id_rsa';
 
-my $id = 2;
+sub copy_id {
+       my $ip = shift;
+       my $ssh = client::ip_path( $ip, 'ssh' );
+       return if -l $ssh;
+       my $id = $id_rsa . '.pub';
+       my $cmd = "sudo ssh-copy-id -i $id root\@$ip";
+       warn "# $cmd\n";
+       system $cmd;
+       warn "$id -> $ssh";
+       symlink $id, $ssh;
+}
 
 sub ethernet_bridge_to {
-       my $host = shift;
+       my $ip = shift;
 
        die "you need to run this as root\n" unless $UID == 0;
 
+       copy_id $ip;
+
        warn "# reset local IP address";
        system "ifconfig virtual 172.16.10.$id";
 
-       warn "# connect to $host";
-       my $ssh = Net::OpenSSH->new( $host,
-               master_opts => [ -w => "$id:$id", -o => 'Tunnel=ethernet' ],
+       warn "# connect to $ip";
+       my $ssh = Net::OpenSSH->new( $ip,
+               master_opts => [ -i => $id_rsa, -w => "$id:$id", -o => 'Tunnel=ethernet' ],
        );
 
        foreach my $command ( "ifconfig tap$id up", "brctl addif virtual tap$id" ) {
@@ -25,33 +45,38 @@ sub ethernet_bridge_to {
                $ssh->system( $command ) or die "$command ", $ssh->error;
        }
 
-       warn "press enter to close tunnel to $host from $id";
+       warn "press enter to close tunnel to $ip from $id";
        <STDIN>;
 
+       system "ifconfig virtual 172.16.10.1";
+
 }
 
 sub shell {
-       my $host = shift;
+       my $ip = shift;
 
-       my $ssh = Net::OpenSSH->new( $host );
+       copy_id $ip;
+
+       warn "# ssh $ip -i $id_rsa";
+       my $ssh = Net::OpenSSH->new( $ip,
+               master_opts => [ -i => $id_rsa ],
+       );
 
        my $html;
+       my @shell;
 
        foreach my $command ( @_ ) {
-               warn "root\@$host:# $command\n";
+               warn "root\@$ip:# $command\n";
                my ($out,$err) = $ssh->capture2( $command ) or die "$command ", $ssh->error;
                warn "$out\n$err";
 
-               $html .= qq|<tt style="color: grey">root\@$host:# <b>$command</b></tt><pre>$out</pre>|;
+               CouchDB::audit( $ip, $command, { ip => $ip, command => $command, out => $out, err => $err } );
+
+               $html .= qq|<tt style="color: grey">root\@$ip:# <b>$command</b></tt><pre>$out</pre>|;
                $html .= qq|<pre style="color: red">$err</pre>| if $err;
        }
 
        return $html;
 }
 
-sub copy_id {
-       my $host = shift;
-       system 'sudo ssh-copy-id -i /root/.ssh/id_rsa.pub root@' . $host;
-}
-
 1;