| 1 | package MYDan::Util::ExpSSH; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Expect; | ||
| 7 | use MYDan; | ||
| 8 | use MYDan::Node; | ||
| 9 | use MYDan::Util::OptConf; | ||
| 10 | use MYDan::Util::Pass; | ||
| 11 | use MYDan::Util::Hosts; | ||
| 12 | use MYDan::Util::Alias; | ||
| 13 | use MYDan::Util::Proxy; | ||
| 14 | |||
| 15 | use Authen::OATH; | ||
| 16 | use Convert::Base32 qw( decode_base32 ); | ||
| 17 | |||
| 18 | our $TIMEOUT = 20; | ||
| 19 | our $SSH; | ||
| 20 | our $RSYNC; | ||
| 21 | |||
| 22 | BEGIN{ | ||
| 23 | my $alias = MYDan::Util::Alias->new(); | ||
| 24 | $RSYNC = $alias->alias( 'rsync' ) || 'rsync'; | ||
| 25 | $SSH = $alias->alias( 'ssh' ) || 'ssh' . ' -o StrictHostKeyChecking=no -o NumberOfPasswordPrompts=1'; | ||
| 26 | }; | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use MYDan::Util::ExpSSH; | ||
| 31 | |||
| 32 | my $ssh = MYDan::Util::ExpSSH->new( ); | ||
| 33 | |||
| 34 | $ssh->conn( host => 'foo', user => 'joe', | ||
| 35 | pass => '/conf/file', | ||
| 36 | sudo => 'user1', | ||
| 37 | rsync => '-aP /tmp/foo user@host:/tmp/bar', #if rsync | ||
| 38 | ); | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | sub new | ||
| 43 | { | ||
| 44 | my $class = shift; | ||
| 45 | bless +{}, ref $class || $class; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub conn | ||
| 49 | { | ||
| 50 | my ( $self, %conn, $grep ) = splice @_; | ||
| 51 | |||
| 52 | my $hosts = MYDan::Util::Hosts->new(); | ||
| 53 | my @host = $conn{rsync} ? ( $conn{host} ) : $self->host( $hosts, $conn{host} ); | ||
| 54 | |||
| 55 | GOTO: | ||
| 56 | |||
| 57 | @host = grep{ $_ =~ /$grep/ }@host if defined $grep; | ||
| 58 | return unless @host; | ||
| 59 | |||
| 60 | my $i = 0; | ||
| 61 | if ( @host > 1 ) | ||
| 62 | { | ||
| 63 | my @host = map { sprintf "[ %d ] %s", $_ + 1, $host[$_] } 0 .. $#host; | ||
| 64 | print STDERR join "\n", @host, "please select: [ 1 ] "; | ||
| 65 | |||
| 66 | my $x = <STDIN>; | ||
| 67 | if( $x && $x =~ s/^\/// ) { $grep = $x; chomp $grep; goto GOTO; } | ||
| 68 | $i = $1 - 1 if $x =~ /(\d+)/ && $1 && $1 <= @host; | ||
| 69 | } | ||
| 70 | |||
| 71 | my ( undef, $pass ) = MYDan::Util::Pass->new( conf => $conn{pass} ) | ||
| 72 | ->pass( [ $host[$i] ] => $conn{user} ); | ||
| 73 | |||
| 74 | if( $pass && ref $pass ) | ||
| 75 | { | ||
| 76 | my $default = delete $pass->{default}; | ||
| 77 | my ( $j, @user ) = ( 0, keys %$pass ); | ||
| 78 | |||
| 79 | if( @user == 1 ) | ||
| 80 | { | ||
| 81 | $conn{user} = $user[0];$pass = $pass->{$user[0]}; | ||
| 82 | } | ||
| 83 | elsif ( @user > 1 ) | ||
| 84 | { | ||
| 85 | my @u = map { sprintf "[ %d ] %s", $_ + 1, $user[$_] } 0 .. $#user; | ||
| 86 | print STDERR join "\n", @u, "please select: [ 1 ] "; | ||
| 87 | $j = $1 - 1 if <STDIN> =~ /(\d+)/ && $1 && $1 <= @user; | ||
| 88 | $conn{user} = $user[$j]; | ||
| 89 | $pass = $pass->{$user[$j]}; | ||
| 90 | }elsif( $pass = $default ) | ||
| 91 | { | ||
| 92 | $conn{user} = `logname`;chop $conn{user}; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | my %expect; | ||
| 97 | if ( $pass && ref $pass ) | ||
| 98 | { | ||
| 99 | %expect = %$pass; | ||
| 100 | for( keys %expect ) | ||
| 101 | { | ||
| 102 | next unless $expect{$_} =~ /googlecode\s*:\s*(\w+)/; | ||
| 103 | $expect{$_} = Authen::OATH->new->totp( decode_base32( $1 )); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | elsif( defined $pass ) | ||
| 107 | { | ||
| 108 | $expect{assword} = $pass; | ||
| 109 | } | ||
| 110 | |||
| 111 | my $ssh; | ||
| 112 | my $node = $host[$i]; | ||
| 113 | my %node = $hosts->match( $node ); | ||
| 114 | |||
| 115 | my $p = MYDan::Util::Proxy->new( "$MYDan::PATH/etc/util/conf/proxy" ); | ||
| 116 | my %x = $p->search( $node{$node} ); | ||
| 117 | |||
| 118 | $ssh = $SSH. sprintf " %s %s", $conn{user} ? "-l $conn{user}" : '', | ||
| 119 | $x{$node{$node}} ? " -o ProxyCommand='nc -X 5 -x $x{$node{$node}} %h %p'":''; | ||
| 120 | |||
| 121 | if ($conn{rsync} ) | ||
| 122 | { | ||
| 123 | $conn{rsync} =~ s/$node:/$node{$node}:/g; | ||
| 124 | $ssh = "$RSYNC -e \"$ssh\" $conn{rsync}" | ||
| 125 | } | ||
| 126 | else | ||
| 127 | { | ||
| 128 | $ssh = "$ssh -t $node{$node}"; | ||
| 129 | } | ||
| 130 | warn "debug:$ssh\n" if $ENV{MYDan_DEBUG}; | ||
| 131 | |||
| 132 | my $prompt = '::sudo::'; | ||
| 133 | if ( my $sudo = $conn{sudo} ) { $ssh .= " sudo -p '$prompt' su - $sudo" } | ||
| 134 | |||
| 135 | exec $ssh unless $pass; | ||
| 136 | |||
| 137 | my $exp = Expect->new(); | ||
| 138 | |||
| 139 | $SIG{WINCH} = sub | ||
| 140 | { | ||
| 141 | $exp->slave->clone_winsize_from( \*STDIN ); | ||
| 142 | kill WINCH => $exp->pid if $exp->pid; | ||
| 143 | local $SIG{WINCH} = $SIG{WINCH}; | ||
| 144 | }; | ||
| 145 | |||
| 146 | $exp->slave->clone_winsize_from( \*STDIN ); | ||
| 147 | $exp->spawn( $ssh ); | ||
| 148 | $exp->expect | ||
| 149 | ( | ||
| 150 | $TIMEOUT, | ||
| 151 | [ qr/yes\/no/ => sub { $exp->send( "yes\n" ); exp_continue; } ], | ||
| 152 | [ qr/[#\$%] $/ => sub { $exp->interact; } ], | ||
| 153 | [ qr/$prompt$/ => sub { $exp->send( $pass ); $exp->interact; } ], | ||
| 154 | map{ my $v = $expect{$_};[ qr/$_/ => sub { $exp->send( "$v\n" ); exp_continue; } ] }keys %expect | ||
| 155 | ); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub host | ||
| 159 | { | ||
| 160 | my ( $self, $hosts, $host ) = splice @_; | ||
| 161 | |||
| 162 | return $host if $host =~ /^\d+\.\d+\.\d+\.\d+$/; | ||
| 163 | |||
| 164 | my $range = MYDan::Node->new( MYDan::Util::OptConf->load()->dump( 'range') ); | ||
| 165 | my $db = $range->db; | ||
| 166 | |||
| 167 | my %node = map{ $_ => 1 }grep{ /$host/ && /^[\w.-]+$/ } | ||
| 168 | map{ @$_ }$db->select( 'node' ); | ||
| 169 | |||
| 170 | map{ $node{$_} = 1 }grep{ /$host/ && /^[\w.-]+$/ }$hosts->hosts(); | ||
| 171 | return %node ? sort keys %node : $host; | ||
| 172 | } | ||
| 173 | |||
| 174 | 1; |