“未找到命令” shell 脚本执行的expect 脚本中出现错误

发布于 2024-10-10 18:28:44 字数 2519 浏览 4 评论 0原文

我正在尝试实现“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 with expect eof.

This is my first expect script so don't have much idea what to code. Any pointers?

Thanks,
Sandeepan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2024-10-17 18:28:44

不要做任何这样的事情!您应该按照上面的评论建议使用公钥身份验证。您所采取的方式会使密码清晰且脆弱。

公钥身份验证更容易设置,例如:设置说明

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

风轻花落早 2024-10-17 18:28:44

你确定你在做

./script.exp

而不是

. ./script.exp

?后者会让 shell 尝试解释期望程序。

完全同意 ssh 密钥是正确的解决方案。

Are you sure you're doing

./script.exp

And not

. ./script.exp

?? The latter would have the shell trying to interpret the expect program.

Fully agree that ssh keys are the correct solution though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文