gpt4 book ai didi

bash - 在 bash 脚本中期待 × 16172

转载 作者:行者123 更新时间:2023-11-29 09:30:38 24 4
gpt4 key购买 nike

我尝试在 bash 中设置一个 expect 脚本。

#!/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 "$user\n"
expect "Password:"
send "$pass\n"
expect ">"
send "en\n"
expect "Password:"
send "$pass\n"

send "term len 0\n"
log_file $router--$filesave.cfg
send "show running-config\n"
expect "end\r"
send "\n"
log_file
send "exit\n"
EOD

cat /Users/test/Desktop/python/$router--$filesave.cfg | grep end
exit 0

我刚得到这个输出

./script2 
spawn telnet

telnet> telnet>

最佳答案

尝试改变你的 shebang:

#!/bin/bash

#!/bin/expect

并删除:

/usr/bin/expect <<- EOD

看看是否可行。

更新:如果您需要将 expect 作为 bash 脚本的一部分运行,请将 expect 代码封装在一个带有 expect shebang 的单独脚本中,并从您的 bash 脚本中获取它,或者按照以下示例对其进行编码:

expect_sh=$(expect -c "
spawn ssh $login@$IP
expect \"password:\"
send \"$password\r\"
expect \"#\"
send \"cd $dest_dir\r\"
expect \"#\"
send \"chmod +x $server_side_script $other_script\r\"
expect \"#\"
send \"./$device_side_script\r\"
expect \"#\"
send \"cat $deploy_count\r\"
expect \"#\"
send \"exit\r\"
")

echo "$expect_sh"

关于bash - 在 bash 脚本中期待 × 16172,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13185658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com