您的位置:首页 > 其它

expect使用ssh登录脚本

2012-11-21 12:51 211 查看
基础篇:

#!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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: