【发布时间】:2019-10-04 15:18:54
【问题描述】:
我正在尝试制作一个脚本来自动 ssh 登录和下载保存在一个文件中的多个 IP 地址的文件,我使用的是 for i in $(cat /dir/file),问题是 cat 函数正在预期中工作,所以我无法使用它在用期望解释的脚本上。
我想知道是否有可能让 bash 解释一半脚本,而期望像这样解释另一半脚本
#!/bin/bash
code........
#!/usr/bin/expect
code........
我的代码现在看起来像这样
#!/bin/bash
expect <<-EOF
spawn sftp user@server
expect "*password:" { send "password\r" }
expect "*" { send "get /home/file\r" }
expect "*" { send "bye\r" }
EOF
我现在收到此回复
spawn sftp user@server
user@server´s password: user@hostname: /home/user
基本上恢复到正常的终端状态
【问题讨论】:
-
看看sexpect (Expect for Shells),您可以使用它来编写仅使用shell代码的Expect脚本。
-
技术答案 - 不。实用答案 -
bash可以调用expect,所以可以肯定。请参阅下面的 Bayou 解决方案。 -
对于 shell heredocs,不得使用前导空格作为 heredoc 终止符:删除 "EOF" 之前的空格