-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathssh_expect.pl
66 lines (56 loc) · 1.53 KB
/
ssh_expect.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
#!/usr/bin/perl
use Expect;
use strict;
$Expect::Log_Stdout = 1;
my $cmd = "";
my $timeout = 4;
my $exp ;
$cmd = 'ssh -e \'?\' root@192.168.1.1';
$exp = Expect->spawn($cmd) or die "Can not spawn $cmd\n";
# I set the terminal size as explained above, but if I resize the window, the application does not notice this.
# You have to catch the signal WINCH ("window size changed"), change the terminal size and propagate the signal to the spawned application:
$SIG{WINCH} = \&winch;
sub winch {
$exp->slave->clone_winsize_from(\*STDIN);
kill WINCH => $exp->pid if $exp->pid;
$SIG{WINCH} = \&winch;
}
$exp->expect($timeout,
["password:" => sub {
my $exp = shift;
$exp->send ("password1\n");
exp_continue;
}],
["info>" => sub {
my $exp = shift;
$exp->send("version\n");
exp_continue;
}],
["Password:" => sub {
my $exp = shift;
$exp->send("password2\n");
exp_continue;
} ],
[ "root#" => sub {
my $exp = shift;
$exp->send("\n");
}]
);
$exp->send("ash\n");
$exp->expect($timeout,
'-re', qr/(\w+):/ => sub {
my $exp = shift;
my $key = ($exp->matchlist)[0];
my $salt = $key;
substr($salt, 0, 3, "ddd");
substr($salt, 5, 2, "bb");
my $digest=`openssl passwd -1 -salt $salt $key`;
my $result = substr($digest, 12,22);
$exp->log_stdout(1);
print ("openssl passwd -1 -salt $salt $key\n");
print ("digest=$digest\nresult=$result\n");
$exp->send("$result\n");
}
);
$exp->interact();
#$exp->soft_close();