`
xitong
  • 浏览: 6158891 次
文章分类
社区版块
存档分类
最新评论

expect使用ssh登录脚本

 
阅读更多

基础篇:

#!u/s+ SSH
spawn ssh -l root 10.10.10.24

# 等待响应,第一次登录往往会提示是否永久保存 RSA 到本机的 know hosts 列表中;等到回答后,在提示输出密码;之后就直接提示输入密码

expect "(yes/no)?" {

send "yes\r"
expect "password:"
send "123456\r"
} "password:" {send "123456\r"} "*host "
interact

# 这里使用了 interact 命令,使执行完程序后,用户可以在 $host 终端进行交互操作。

多用户登录,使用shell进行调用

#!/usr/bin/expect -f
set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set timeout 30
spawn ssh root@$ipaddress
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
expect "*from*"
interact
~
shell调用脚本

#!/bin/bash
if [ $1 == 24 ];then
i='10.10.10.24'
j='123456'
fi
if [ $1 == 17 ];then
i='10.10.10.17'
j='1qaz2wsx'
fi
echo $i
echo $j
expect /home/shell/benchmark/login.exp $i $j

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics