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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48183232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:44:58  来源:igfitidea点击:

spawn: command not found when I use expect to ssh login my remote server

bashexpect

提问by aircraft

In my Mac, I have installed expect:

在我的 Mac 中,我已经安装了expect

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

I write a bash script on my Mac:

我在我的 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

But when I execute my script on my Mac, I get error:

但是当我在 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: ` }'

It report spawn: command not found, but I installed the expect in my Mac.

它报告spawn: command not found,但我在我的 Mac 中安装了期望。

I also tried change the test01.shname to test01.exp, not work too.

我也试过把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: `    }'

回答by

Expect is not Bash. You need #!/usr/bin/expector expect /your/script.exp.

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