-
Notifications
You must be signed in to change notification settings - Fork 2
/
easyssh
executable file
·65 lines (58 loc) · 1.61 KB
/
easyssh
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
#!/usr/bin/expect
#一个自动ssh登录的脚本工具,免去每一次都要输入密码的烦恼
#
#easyssh ip [root] [username] [password]
#ip:目的主机的ip
#root:是否以root身份登录,任何非空字符串都会认识需要以root登录
#username:用户名,如果不传采用脚本中写好的用户名
#password:密码,如果不传采用脚本中写好的密码
#
#关于退出:在命令行输入zzzz即可快速退出
set username "xxx"
set password "xxxx"
set timeout 5
set ip [lindex $argv 0];
set root [lindex $argv 1];
if { [string length [lindex $argv 2] ] != 0 } {
set username [lindex $argv 2]
}
if { [string length [lindex $argv 3] ] != 0 } {
set password [lindex $argv 3]
}
if { [string length $ip] == 0 } {
send_user "ERROR:EMPTY IP!!\n";
exit;
}
spawn ssh $username@$ip
#公钥校验和密码校验
expect {
"*yes/no*" { send "yes\n" ; exp_continue;}
"*assword*" { send "$password\n";}
"*口令*" { send "$password\n";}
eof { send_user "ERROR:END OF FILE!!\n"; exit;}
timeout { send_user "ERROR:TIME OUT!!\n"; exit;}
}
#检测是否已经登录成功
expect {
"$username" {}
"@" {}
"~" {}
"denied" { send_user "ERROR:WRONG PASSWORD!!\n";exit;}
eof { send_user "ERROR:END OF FILE!!\n"; exit;}
timeout { send_user "ERROR:TIME OUT!!\n"; exit;}
}
# 切换到root用户
if { [string length $root] != 0 } {
send "sudo -s\n";
expect {
"*assword*" { send "$password\n";}
"root@" {;}
"*口令*" { send "$password\n";}
eof { send_user "ERROR:END OF FILE!!\n"; exit;}
timeout { send_user "ERROR:TIME OUT!!\n"; exit;}
}
}
#交由用户控制
interact {
"zzzz" {send "exit\nexit\n"}
}