【问题标题】:"command not found" errors in expect script executed by shell script由 shell 脚本执行的期望脚本中的“找不到命令”错误
【发布时间】:2011-01-06 08:04:26
【问题描述】:

我正在尝试实现“shell 脚本调用期望脚本”,这样它就不会每次都提示用户输入 ssh 密码。我从Using a variable's value as password for scp, ssh etc. instead of prompting for user input every time 开始,明白我应该有一个.sh 文件和一个.exp 文件。我的系统中安装了expect(运行expect -v 显示expect version 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

在运行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

我从多个来源(不了解很多基础知识)获得了上述代码(在 exp 文件中)。 one source中的代码是这样的

#!/usr/local/bin/expect
spawn  sftp  -b cmdFile user@yourserver.com
expect "password:"
send "shhh!\n";
interact

而在another source 这样的

#!/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

那里有一些差异 -

  • #!/usr/local/bin/expect 行中有一个额外的-f
  • 期望 "?assword:" {send -- "$PASSWORD\r";} 不同于 expect "password:" send "shhh!\n";

  • interact 替换为expect eof

这是我的第一个expect script,所以不太清楚要编码什么。有什么指点吗?

谢谢,
桑迪潘

【问题讨论】:

  • 可以选择公钥认证吗?如果是这样,那将是更好(也更容易)的方法。
  • 请注意,将密码作为参数传递是不安全的。出于某种原因,您的期望脚本被解释为 shell 脚本。从您发布的内容中看不出这是为什么。
  • @Dennis - 这是否将密码存储在某些日志中,并且任何人都可以使用ps aux 或类似的东西查看?
  • @The MYYN - 我认为公钥身份验证在这里不是一个精确的解决方案。请查看我之前的问题以获得解释

标签: linux bash shell expect


【解决方案1】:

不要这样做!您应该按照上面的评论建议使用公钥身份验证。你要走的路让密码很清楚而且很脆弱。

公钥认证更容易设置,例如:setup instructions

【讨论】:

    【解决方案2】:

    你确定你在做吗

    ./script.exp
    

    不是

    . ./script.exp
    

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

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

    【讨论】:

    • 是的,这就是 ./password.exp $DESTINATION_PATH $SSH_CREDENTIALS $PROJECT_INSTALLATION_PATH $PASSWORD 的那部分。但是使用 ssh 密钥我如何才能限制使用 SSH 密码的访问呢?如果系统具有正确的密钥,任何人都可以运行该脚本(根据公钥身份验证的需要 - 尚未深入)。请检查我的问题stackoverflow.com/questions/4594698/…人们如何做到这一点?我错过了什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    相关资源
    最近更新 更多