从Expect脚本调用bash shell脚本失败:“无此文件或目录”

时间:2019-10-15 10:54:44

标签: bash expect spawn

我无法从我的期望脚本执行bash shell脚本。 我有一个期望脚本,如下所示,该脚本可更改正在其上运行的计算机的IP地址。

#!/usr/bin/expect -f
set new_ipaddr [lindex $argv 0]
set password [lindex $argv 1]
set timeout 2

if {[llength $argv] != 2} {
  send_user "Usage: ./set_ip.sh ipaddress password\n"
  exit 1
}
spawn sudo nmcli con mod "Wired connection 1" ipv4.address $new_ipaddr/24

expect {
    password { send "$password\r"; exp_continue }
    timeout { send_user "\nFailed to get password prompt\n"; exit 1 }
}

send_user "setting Gateway\n"
spawn sudo nmcli con mod "Wired connection 1" ipv4.gateway 10.10.10.254

send_user "setting Static IP\n"
spawn sudo nmcli con mod "Wired connection 1" ipv4.method manual
spawn sudo nmcli con up "Wired connection 1"

spawn "./reboot.sh $password"

close

要使此方法生效,计算机需要重新启动,因此我运行bash脚本来执行重新启动。 reboot.sh脚本执行失败,报告

couldn't execute "reboot.sh": no such file or directory
    while executing
"spawn reboot.sh $password"

reboot.sh脚本位于主文件夹中,与正在运行的脚本相同。我也尝试过

spawn "~/./reboot.sh $password"

但还是一样。权限设置正确,重新启动脚本从命令行正确运行。 有人可以告诉我我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

#!/usr/bin/expect -f
set new_ipaddr [lindex $argv 0]
set password [lindex $argv 1]
set timeout 2

if {[llength $argv] != 2} {
  send_user "Usage: ./set_ip.sh ipaddress password\n"
  exit 1
}
spawn sudo nmcli con mod "Wired connection 1" ipv4.address $new_ipaddr/24

expect {
    password { send "$password\r"; exp_continue }
    timeout { send_user "\nFailed to get password prompt\n"; exit 1 }
}

send_user "setting Gateway\n"
spawn sudo nmcli con mod "Wired connection 1" ipv4.gateway 10.10.10.254

send_user "setting Static IP\n"
spawn sudo nmcli con mod "Wired connection 1" ipv4.method manual
spawn sudo nmcli con up "Wired connection 1"

spawn ./reboot.sh "$password"
interact