/[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

Contents of /lib/PXElator/ssh.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 351 - (show annotations)
Sat Aug 29 21:10:21 2009 UTC (14 years, 7 months ago) by dpavlin
File size: 1468 byte(s)
ssh-copy-id if needed and record output in CouchDB
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 CouchDB;
11
12 my $id = 2;
13
14 sub ethernet_bridge_to {
15 my $host = shift;
16
17 die "you need to run this as root\n" unless $UID == 0;
18
19 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 sub shell {
39 my $ip = shift;
40
41 my $ssh = Net::OpenSSH->new( $ip );
42
43 my $html;
44 my @shell;
45
46 foreach my $command ( @_ ) {
47 warn "root\@$ip:# $command\n";
48 my ($out,$err) = $ssh->capture2( $command ) or die "$command ", $ssh->error;
49 warn "$out\n$err";
50
51 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 $html .= qq|<pre style="color: red">$err</pre>| if $err;
55 }
56
57 return $html;
58 }
59
60 my $id_pub = '/root/.ssh/id_rsa.pub';
61
62 sub copy_id {
63 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 }
71
72 1;

  ViewVC Help
Powered by ViewVC 1.1.26