【问题标题】:Linux expect scriptLinux 期望脚本
【发布时间】:2022-03-05 22:26:49
【问题描述】:

我花了 8 个小时的大部分时间试图通过谷歌解决这个问题,所以我希望这值得在这里提问。

当我尝试从 vmware 中的 lubuntu 映像连接到通过 USB 连接的物理设备时,我需要一个脚本来自动输入密码。

我已经尝试了至少 50 种我在网上找到的不同脚本,但它们都不起作用(甚至无法将 spawn 识别为命令)

这是我的脚本:

#!/usr/expect

spawn CPY2T_old.sh  
expect  "root@10.9.8.2's password:"   
send "ThePassword"
expect eof

CPY2T_old.sh的内容是

#!/bin/bash

cd hellolinux/src/Exercise$1 
scp $2 root@10.9.8.2:

上面的 bash 脚本工作正常,但我必须输入密码,这是我首先要避免的。当我在 cmd 中执行时,expect 脚本给出以下内容:

spawn: command not found
couldn't read file "root@10.9.8.2's password:": no such file or directory

The program 'send' can be found in the following packages:
 * mailutils-mh
 * nmh
Try: sudo apt-get install <selected package>
couldn't read file "eof": no such file or directory

到目前为止,我也至少下载了十几次 mailutils 和 nmh。在我读到的其他地方,我需要在顶部 @echo 关闭,但无法识别此命令并给出错误。

编辑:我无法对此设备进行无密码 ssh,所以请不要建议。

【问题讨论】:

  • @ Tachyon :您必须将命令行参数传递给 bash 脚本。正确的?它在哪里传递给脚本?顺便说一句,在使用send 时,我们还必须添加\r 字符,这相当于发送Enter 密钥。
  • 据我了解,expect 脚本不是 bash 脚本?如果我将这两个脚本结合起来,那就是#!/usr/expect sudo ifdown usb0 sudo ifup usb0 ssh root@10.9.8.2 spawn CPY2T_old.sh expect "root@10.9.8.2's password:" send "root\r" expect eof
  • 正确。当您将shebang 定义为#!/usr/bin/expect 时,它将从Expect 运行。因此,从Expect 的角度来看,您上面提到的任何代码都将失败。因为它会以invalid command name "sudo" 的形式抛出错误。

标签: expect


【解决方案1】:

我看到 2 个错误:第一个

#!/usr/expect

你想要的

#!/usr/bin/expect

这应该会导致错误:你是如何启动你的期望脚本的?

第二

send "ThePassword"

你忘了回车

send "ThePassword\r"

【讨论】:

    【解决方案2】:

    #!/usr/bin/expect

    设置超时 60

    生成 ssh 用户@ip

    期望“user@ip 的密码:”

    发送“密码\r”

    互动

    注意:请确保所有脚本都可以使用命令 $ chmod +x #file_name 或 $ chmod 700 #file_name 执行。

    \r 执行

    Github 链接:https://github.com/asarari207/Lunix_sh

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    相关资源
    最近更新 更多