gpt4 book ai didi

linux - "command not found"shell脚本执行的expect脚本错误

转载 作者:太空狗 更新时间:2023-10-29 11:20:33 29 4
gpt4 key购买 nike

我正在尝试实现“调用 expect 脚本的 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
  • expect "?assword:"{send -- "$PASSWORD\r";} 不同于 expect "password:"
    发送“嘘!\n”;

  • interact 替换为 expect eof

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

谢谢,
桑迪潘

最佳答案

千万不要这样做!您应该按照上面的评论建议使用公钥身份验证。您采用的方式会留下明文且脆弱的密码。

公钥身份验证更容易设置,例如:setup instructions

关于linux - "command not found"shell脚本执行的expect脚本错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4612937/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com