【发布时间】:2016-11-22 23:21:31
【问题描述】:
我正在尝试连接到路由器的子模块,在其上运行一些命令并将输出保存到文件中。我遇到的问题是子模块需要用户名和密码,如果将所有命令放在一个脚本中,则会在提示输入它们之前发送用户名/密码详细信息。有没有办法在命令之间设置延迟,或者在同一个 SSH 会话下批量运行它们?
我使用 EOF 方法并将命令放在单独的文件中(首选):
ssh user@root > file.log << EOF
session slot 1 # command to connect to the subslot.
# a delay of about 3 sec would be required before the credentials are sent
slot_username
slot_password
command1
command2
command3
exit
exit
EOF
另外,第二个出口应该让我离开路由器,但它只是被忽略了。
【问题讨论】:
-
我建议使用
expect。 -
谢谢。最后我使用expect创建了一个脚本。我将它与 bash 脚本结合起来,以提示输入菜单并获取用户输入。我确信有一种“更智能”的方法可以做到这一点,但我还没有找到一种通过期望获取用户输入并将数据用作变量的方法..