“未找到命令” shell 脚本执行的expect 脚本中出现错误
我正在尝试实现“shell脚本调用expect脚本”,这样它就不会每次都提示用户输入ssh密码。我从 使用变量的值作为 scp、ssh 等的密码,而不是每次都提示用户输入,并且理解我应该有一个 .sh
文件和一个 .exp< /代码> 文件。我的系统中安装了
expect
(运行 expect -v
显示 expect 版本 5.43.0
)。
在我的 upload-to-server.sh
文件中
cd $SOURCE_PATH/shell
./password.exp $DESTINATION_PATH $SSH_CREDENTIALS $PROJECT_INSTALLATION_PATH $PASSWORD
,在我的 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 我收到以下错误 -
./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
我从多个来源(不了解太多基础知识)得到了上述代码(在 exp 文件中)。在 one source 中,代码如下所示
#!/usr/local/bin/expect
spawn sftp -b cmdFile [email protected]
expect "password:"
send "shhh!\n";
interact
,而在 另一个来源 像这样
#!/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
有一些差异 -
- 中有一个额外的
-f
- 在
#!/usr/local/bin/expect
行expect " ?assword:" {send -- "$PASSWORD\r";} 与
expect "password:" 不同 send "shhh!\n";
interact
替换为expect eof
。
这是我的第一个 expect 脚本
,所以不太知道要编码什么。有什么指点吗?
谢谢,
桑迪潘
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 time and 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
).
In my upload-to-server.sh
file I have
cd $SOURCE_PATH/shell
./password.exp $DESTINATION_PATH $SSH_CREDENTIALS $PROJECT_INSTALLATION_PATH $PASSWORD
And in my password.exp
file I have
#!/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 -
./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 source the code is like this
#!/usr/local/bin/expect
spawn sftp -b cmdFile [email protected]
expect "password:"
send "shhh!\n";
interact
Whereas in another source like 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
.
This is my first expect script
so don't have much idea what to code. Any pointers?
Thanks,
Sandeepan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要做任何这样的事情!您应该按照上面的评论建议使用公钥身份验证。您所采取的方式会使密码清晰且脆弱。
公钥身份验证更容易设置,例如:设置说明
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
你确定你在做
而不是
?后者会让 shell 尝试解释期望程序。
完全同意 ssh 密钥是正确的解决方案。
Are you sure you're doing
And not
?? The latter would have the shell trying to interpret the expect program.
Fully agree that ssh keys are the correct solution though.