/[pxelator]/lib/PXElator/ssh.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /lib/PXElator/ssh.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 351 - (hide annotations)
Sat Aug 29 21:10:21 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 1468 byte(s)
ssh-copy-id if needed and record output in CouchDB
1 dpavlin 283 package ssh;
2    
3 dpavlin 351 use warnings;
4     use strict;
5    
6 dpavlin 283 use Net::OpenSSH;
7 dpavlin 312 use English;
8 dpavlin 348 use Data::Dump qw/dump/;
9 dpavlin 351 use client;
10     use CouchDB;
11 dpavlin 283
12     my $id = 2;
13    
14     sub ethernet_bridge_to {
15     my $host = shift;
16    
17 dpavlin 312 die "you need to run this as root\n" unless $UID == 0;
18    
19 dpavlin 283 warn "# reset local IP address";
20     system "ifconfig virtual 172.16.10.$id";
21    
22     warn "# connect to $host";
23     my $ssh = Net::OpenSSH->new( $host,
24     master_opts => [ -w => "$id:$id", -o => 'Tunnel=ethernet' ],
25     );
26    
27     foreach my $command ( "ifconfig tap$id up", "brctl addif virtual tap$id" ) {
28     warn "# $command";
29     system $command;
30     $ssh->system( $command ) or die "$command ", $ssh->error;
31     }
32    
33     warn "press enter to close tunnel to $host from $id";
34     <STDIN>;
35    
36     }
37    
38 dpavlin 348 sub shell {
39 dpavlin 351 my $ip = shift;
40 dpavlin 348
41 dpavlin 351 my $ssh = Net::OpenSSH->new( $ip );
42 dpavlin 348
43     my $html;
44 dpavlin 351 my @shell;
45 dpavlin 348
46     foreach my $command ( @_ ) {
47 dpavlin 351 warn "root\@$ip:# $command\n";
48 dpavlin 348 my ($out,$err) = $ssh->capture2( $command ) or die "$command ", $ssh->error;
49     warn "$out\n$err";
50    
51 dpavlin 351 CouchDB::audit( $ip, $command, { ip => $ip, command => $command, out => $out, err => $err } );
52    
53     $html .= qq|<tt style="color: grey">root\@$ip:# <b>$command</b></tt><pre>$out</pre>|;
54 dpavlin 348 $html .= qq|<pre style="color: red">$err</pre>| if $err;
55     }
56    
57     return $html;
58     }
59    
60 dpavlin 351 my $id_pub = '/root/.ssh/id_rsa.pub';
61    
62 dpavlin 348 sub copy_id {
63 dpavlin 351 my $ip = shift;
64     my $ssh = client::ip_path( $ip, 'ssh' );
65     return if -l $ssh;
66     my $cmd = "sudo ssh-copy-id -i $id_pub root\@$ip";
67     warn "# $cmd\n";
68     system $cmd;
69     symlink $id_pub, $ssh;
70 dpavlin 348 }
71    
72 dpavlin 283 1;

  ViewVC Help
Powered by ViewVC 1.1.26