spawn:当我使用expect ssh登录我的远程服务器时找不到命令

时间:2018-01-10 08:33:53

标签: bash expect

在我的Mac中,我安装了expect

$ brew install expect 
Updating Homebrew...
Warning: expect 5.45.3 is already installed

我在Mac上写了一个bash脚本:

#!/usr/bin/expect -f  (there I tried #!/bin/bash too)
set timeout 60
set host 103.212.12.76
set name root 
set password my_password

spawn ssh $host -l $name 

expect {
    "(yes/no)?" {
        send "yes\n"
        expect "password:"
        send "$password\n"
    }
    "password:" {
        send "$password\n"
    }
}

expect "#"
# test whether login to host
send "uname\n"
expect "Linux"
send_user "Now you can do some operation on this terminal\n"

interact

但是当我在Mac上执行我的脚本时,我收到错误:

$ ./test01.sh 
./test01.sh: line 11: spawn: command not found
couldn't read file "{": no such file or directory
./test01.sh: line 14: (yes/no)?: No such file or directory
./test01.sh: line 15: send: command not found
couldn't read file "password:": no such file or directory
./test01.sh: line 17: send: command not found
./test01.sh: line 18: syntax error near unexpected token `}'
./test01.sh: line 18: ` }'

它报告spawn: command not found,但我在Mac上安装了expect。

我也尝试将test01.sh名称更改为test01.exp,但也无效。

./test01.exp 
./test01.exp: line 11: spawn: command not found
couldn't read file "{": no such file or directory
./test01.exp: line 14: (yes/no)?: No such file or directory
./test01.exp: line 15: send: command not found
couldn't read file "password:": no such file or directory
./test01.exp: line 17: send: command not found
./test01.exp: line 18: syntax error near unexpected token `}'
./test01.exp: line 18: `    }'

1 个答案:

答案 0 :(得分:0)

期待不是Bash。您需要#!/usr/bin/expectexpect /your/script.exp