/
sshExpect.pl
executable file
·84 lines (63 loc) · 2.14 KB
/
sshExpect.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/prog/perl/5.10.1/bin/perl
## #!/usr/bin/perl
use Expect;
use warnings;
use strict;
print "Expect module available... Good, spawning ssh sessions now...\n";
##my $command = "/bin/hostname";
my $command = "ssh somehostname";
#my @params = ("localhost");
my $perltidx;
my $sshkey_timeout = 4;
my $timeout = 30;
#my @match_patterns = ">";
my $cmdAtHost = "uptime"
my @hostlist;
#push( @hostlist, "linux-tin2" );
#push( @hostlist, "usvm-bofh1" );
#my @hostlist = ("linux-tin2", "usvm-bofh1" );
my @esx_hostlist = ("usesx10sc" );
@hostlist = @esx_hostlist;
print "hostlist is @hostlist\n" ;
#my $id ;
#for( $id=13; $id<=16; $id++ ) {
#print "working on... mvusca${id}sc\n"
#}
my $host;
foreach $host ( @hostlist ) {
# for( $id=13; $id<=14; $id++ )
## $host = "vmusca" . $id . "sc";
print( "\n\n ===== Now working on $host ===== \n\n" );
my $exp = new Expect;
#$exp->debug(3);
#$exp->exp_internal(1);
$exp->log_file("sshExpect.log");
$exp->raw_pty(1);
##$exp->spawn( $comand, @params) or die "Cannot spawn $command: $!\n";
$command = "ssh $host";
$exp->spawn( $command ) or die "Cannot spawn $command: $!\n";
##$exp->spawn( "ssh localhost" ) or die "Cannot spawn $command: $!\n";
## ssh may ask to accept key
$perltidx = $exp->expect($sshkey_timeout, 'Are you sure you want to continue connecting (yes/no)?' );
$exp->send("yes\n" );
## don't work, need more sophisticated check... rtfm!
## expect can match multiple patterns, with diff actions, see
## http://search.cpan.org/~rgiersig/Expect-1.15/Expect.pod
# *** Commands to execute on remote host is hard coded below ***
# *** adjust acoordingly
# *** if just one command, consider using $cmdAtDst
$perltidx = $exp->expect($timeout, ">");
$exp->send("hostname\n" );
$perltidx = $exp->expect($timeout, ">");
$exp->send("uptime\n" );
$perltidx = $exp->expect($timeout, ">");
$exp->send("\n" );
$exp->send("exit\n" );
sleep(2);
$exp->soft_close();
print( "\n\n ===== Done working on $host ===== \n" );
}
sleep(4);
print( " ***** All done! \n" );
print( " ***** Reached the end, Expect session closed. Thank you for using Expect, please come back soon!\n" );
exit 0