expect实现ssh自动登录

下载expect

yum -y instll expect

编辑一个脚本名为login.exp

vim login.exp

#!/usr/bin/expect
set timeout 10
spawn ssh root@10.0.0.43  要登录的呢一台服务器的ip
expect "password*"
send "123456\r"
interact

expect login.exp  

OK。。。。。。。。。。。

 

传参的方法:

#!/usr/bin/expect
if {$argc < 3} {
        puts "Usage:cmd <host> <username> <password>"
        exit 1
}
set timeout -1
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $username@$host
expect {
        "password" {send "$password\r";}
        "yes/no" {send "yes\r";exp_continue}
}
interact

执行的时候 需要穿3个参数 分别是ip地址 主机名和密码
posted @ 2020-01-10 10:44  速溶咖啡  阅读(136)  评论(0)    收藏  举报