execute shell commands over ssh for OpenVZ
[pxelator] / lib / PXElator / ssh.pm
index e611a0a..9ed0931 100644 (file)
@@ -2,6 +2,7 @@ package ssh;
 
 use Net::OpenSSH;
 use English;
+use Data::Dump qw/dump/;
 
 my $id = 2;
 
@@ -29,4 +30,28 @@ sub ethernet_bridge_to {
 
 }
 
+sub shell {
+       my $host = shift;
+
+       my $ssh = Net::OpenSSH->new( $host );
+
+       my $html;
+
+       foreach my $command ( @_ ) {
+               warn "root\@$host:# $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>|;
+               $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;