Linux shell脚本执行的expect脚本中的“找不到命令”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4612937/
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
"command not found" errors in expect script executed by shell script
提问by Sandeepan Nath
I am trying to implement "shell script calling expect script" so that it does not prompt the user for entering ssh password every time. I started with Using a variable's value as password for scp, ssh etc. instead of prompting for user input every timeand understood that I should have a .sh
file and a .exp
file. I have expect
installed in my system (running expect -v
shows expect version 5.43.0
).
我正在尝试实现“shell 脚本调用期望脚本”,以便它不会每次都提示用户输入 ssh 密码。我从使用变量的值作为 scp、ssh 等的密码开始,而不是每次都提示用户输入并理解我应该有一个.sh
文件和一个.exp
文件。我已经expect
安装在我的系统中(运行expect -v
显示expect version 5.43.0
)。
In my upload-to-server.sh
file I have
在我的upload-to-server.sh
文件中,我有
cd $SOURCE_PATH/shell
./password.exp $DESTINATION_PATH $SSH_CREDENTIALS $PROJECT_INSTALLATION_PATH $PASSWORD
And in my password.exp
file I have
在我的password.exp
文件中我有
#!/usr/bin/expect -f
set DESTINATION_PATH [lindex $argv 0];
set SSH_CREDENTIALS [lindex $argv 1];
set PROJECT_INSTALLATION_PATH [lindex $argv 2];
set PASSWORD [lindex $argv 3];
spawn scp $DESTINATION_PATH/exam.tar $SSH_CREDENTIALS':/'$PROJECT_INSTALLATION_PATH
expect "password:"
send $PASSWORD"\n";
interact
On running the upload-to-server.sh
file I get the following error -
在运行upload-to-server.sh
文件时,我收到以下错误 -
./password.exp: line 9: spawn: command not found
couldn't read file "password:": no such file or directory
./password.exp: line 11: send: command not found
./password.exp: line 12: interact: command not found
I arrived at the above code (in the exp file) from multiple sources (without understanding much basics). In one sourcethe code is like this
我从多个来源(没有了解太多基础知识)得出上述代码(在 exp 文件中)。在一个来源中,代码是这样的
#!/usr/local/bin/expect
spawn sftp -b cmdFile [email protected]
expect "password:"
send "shhh!\n";
interact
Whereas in another sourcelike this
而在像这样的另一个来源
#!/usr/local/bin/expect -f
set TESTCASE_HOME [lindex $argv 0];
set TESTCASE_LIST [lindex $argv 1];
set PASSWORD [lindex $argv 3];
set timeout 200
spawn $TESTCASE_HOME/dobrt -p $TESTCASE_HOME/$TESTCASE_LIST
expect "*?assword:*" {send -- "$PASSWORD\r";}
expect eof
There are some differences there -
有一些不同之处 -
- there is an extra
-f
in the#!/usr/local/bin/expect
line expect "?assword:" {send -- "$PASSWORD\r";} is different from
expect "password:" send "shhh!\n";
interact
replaced withexpect eof
.
-f
该#!/usr/local/bin/expect
行有一个额外的期望“ ?assword:” {send -- "$PASSWORD\r";} 与
expect "password:" send "shhh!\n";
interact
替换为expect eof
.
This is my first expect script
so don't have much idea what to code. Any pointers?
这是我的第一次,expect script
所以不太知道要编码什么。任何指针?
Thanks,
Sandeepan
谢谢,
桑迪潘
回答by sjr
Don't do any of this! You should use public key authentication as the comment above suggests. The way you're going leaves passwords in the clear and is fragile.
不要做任何这些!您应该按照上面的评论建议使用公钥身份验证。您的方式使密码清晰且易碎。
Public key authentication is way easier to setup, for example: setup instructions
公钥认证更容易设置,例如:设置说明
回答by glenn Hymanman
Are you sure you're doing
你确定你在做什么
./script.exp
And not
并不是
. ./script.exp
?? The latter would have the shell trying to interpret the expect program.
?? 后者会让 shell 尝试解释 expect 程序。
Fully agree that ssh keys are the correct solution though.
完全同意 ssh 密钥是正确的解决方案。