如何写shell脚本自动通过ssh命令登录到服务器
1个回答
展开全部
用EXPECT实现用密码登录,也可配置成不需要密码
#!/usr/bin/expect -f
if { $argc < 3 } {
puts stderr "Usage: $argv0 IPAdress Login OldPasswd"
exit
}
set IPADDR [lindex $argv 0]
set LOGIN [lindex $argv 1]
set OLD_PW [lindex $argv 2]
set timeout 30
stty -echo
spawn ssh $IPADDR -l $LOGIN
expect {
"*Password:*" {
send "$OLD_PW\r"
exp_continue
} "*Last login:*" {
#interact
exit 0
} timeout {
send_user "connection to $IPADDR timeout!\n"
exit 1
} "*incorrect*" {
send_user "password incorrect!\n"
exit 2
} "*Permission*" { #for LINUX ssh
send_user "password Error!\n"
exit 2
} eof {
exit 3
}
}
#!/usr/bin/expect -f
if { $argc < 3 } {
puts stderr "Usage: $argv0 IPAdress Login OldPasswd"
exit
}
set IPADDR [lindex $argv 0]
set LOGIN [lindex $argv 1]
set OLD_PW [lindex $argv 2]
set timeout 30
stty -echo
spawn ssh $IPADDR -l $LOGIN
expect {
"*Password:*" {
send "$OLD_PW\r"
exp_continue
} "*Last login:*" {
#interact
exit 0
} timeout {
send_user "connection to $IPADDR timeout!\n"
exit 1
} "*incorrect*" {
send_user "password incorrect!\n"
exit 2
} "*Permission*" { #for LINUX ssh
send_user "password Error!\n"
exit 2
} eof {
exit 3
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |