【发布时间】: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