期望在 bash 脚本 × 16172



我尝试在bash中设置期望脚本。

#!/bin/bash

/usr/bin/expect <<- EOD
set router 192.168.0.251
set user admin
set pass test  
set timeout 1000  
set filesave [exec date +%m-%d-%Y]  

spawn telnet $router
send "n"  
expect "Username:"
send "$usern"
expect "Password:"  
send "$passn"  
expect ">"  
send "enn"  
expect "Password:"  
send "$passn"  
send "term len 0n"  
log_file $router--$filesave.cfg
send "show running-confign"  
expect "endr"  
send "n"  
log_file
send "exitn"  
EOD
cat /Users/test/Desktop/python/$router--$filesave.cfg | grep end
exit 0

我刚刚获得了此输出

./script2 
spawn telnet
telnet> telnet> 

尝试从:

中更改您的shebang
#!/bin/bash

to

#!/bin/expect

并删除:

/usr/bin/expect <<- EOD

看看是否有效。

更新:如果您需要期望运行作为BASH脚本的一部分,则将期望代码封装在带有期望的单独脚本中,然后从您的bash脚本中源自bash脚本,或者如下示例中对其进行编码:

expect_sh=$(expect -c "
spawn ssh $login@$IP
expect "password:"
send "$passwordr"
expect "#"
send "cd $dest_dirr"
expect "#"
send "chmod +x $server_side_script $other_scriptr"
expect "#"
send "./$device_side_scriptr"
expect "#"
send "cat $deploy_countr" 
expect "#"
send "exitr"
")
echo "$expect_sh"

问题是,在期待看到脚本之前,Bash正在解释所有$variables。因此,您只是在没有hostName的情况下仅产生telnet。更改:

/usr/bin/expect <<- EOD

to:

/usr/bin/expect <<- 'EOD'

这具有单一引用整个此处的文档的效果。
http://www.gnu.org/software/bash/manual/bashref.html#here-documents

还请注意,下一个bash命令(cat /Users/...)依赖于期望中定义的变量 - 它们未在bash中定义。尝试这个

#!/bin/bash
export router=192.168.0.251
export filesave=$(date +%m-%d-%Y)
/usr/bin/expect <<- 'EOD'
set router $env(router)
set filesave $env(filesave)
# the rest stays the same

相关内容

  • 没有找到相关文章

最新更新