Linux expect 传参
bash是通过$0 ... $n接收参数expect是通过set <变量名称> [lindex $argv <param index>],例如:set username [lindex $argv 0]#!/usr/bin/expectset timeout 10set username [lindex $argv 0]set password [lindex $argv
·
bash是通过$0 ... $n
接收参数
expect是通过set <变量名称> [lindex $argv <param index>],
例如:set username [lindex $argv 0]
#!/usr/bin/expect
set timeout 10
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostname [lindex $argv 2]
spawn ssh $username@$hostname
expect "yes/no"
send "yes\r"
expect "password:"
send "$password\r"
expect eof
执行脚本./ssh.exp root pasword hostname1
一个比较粗糙的 Linux expect 通过 telnet console 口配置网络设备
#!/bin/bash
PORT=$1
cmd="""sys\r
interface g1/1\r
ip add 1.1.1.1 24\r"""
# 执行 expect
# 每个 expect 的判断间隔为 5 秒,确保命令可以正常退出
# expect 可以通过发送 ascii 码来执行键盘组合
expect <<END
set timeout 5
spawn telnet 10.0.0.1 1111
expect "*]'" { send "\r" }
expect "*assword:" { send "P@ssword\r" }
expect {
"*>" { send "screen disable\r" }
"*]" { send "screen disable\r" }
}
expect {
"*>" { send "$cmd" }
"*]" { send "$cmd" }
}
expect "*]" { send "\03" }
expect eof
END
expect 可以通过发送 ascii 码来执行键盘组合
expect语法介绍
=====
通过 expect 进行ssh登录并停留在交互界面
#!/bin/bash
PASSWD='Lu@$\(..QXoTFu!'
sw_login(){
expect -c "
# 每个判断等待两秒
set timeout 2
spawn bash -c \"ssh cloudnet@$1\"
# 判断是否需要保存秘钥
expect {
\"yes/no\" { send yes\n }
}
# 判断发送密码
expect {
\"*assword\" { send $PASSWD\n }
}
# 停留在当前登录界面
interact
"
}
sw_login $1
更多推荐
已为社区贡献1条内容
所有评论(0)