简体   繁体   中英

Calling bash shell script from expect script fails : “no such file or directory”

I am having difficulty executing a bash shell script from my expect script. I have an expect script, shown below, which changes the IP address of the machine that it is running on.

#!/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

For this to take effect, the machine needs to reboot, therefore I run a bash script to perform the reboot. The reboot.sh script fails to execute, reporting

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

The reboot.sh script resides in the home folder, the same as the running script. I have also tried

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

but still no different. The permissions are set correctly and the reboot script runs correctly from command line. Can someone tell me what I am doing wrong please?

#!/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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM