【问题标题】:BASH scripting for username/password constructs用于用户名/密码结构的 BASH 脚本
【发布时间】:2013-03-12 22:19:10
【问题描述】:

我想编写一个简单的 bash 脚本,使用 ncat 打开与 ISP 及其端口的连接。

第一个命令是:

nc address port

执行此操作时,系统会首先提示我提供用户名。我必须按 ENTER,然后系统会提示我提供密码,然后我必须再次按 ENTER。

在此之后,我想打开一个终端进程窗口。谁能指出我有足够的资源来编写这种类型的脚本?

我已经知道用户名和密码,但我不太确定如何解决必须提供它然后按 Enter 的事实。我也不确定如何打开一个新的终端进程。

提前致谢!

【问题讨论】:

标签: bash shell unix ssh


【解决方案1】:

查看期望脚本 Expect

例子:

# Assume $remote_server, $my_user_id, $my_password, and $my_command were read in earlier
# in the script.
# Open a telnet session to a remote server, and wait for a username prompt.
spawn telnet $remote_server
expect "username:"
# Send the username, and then wait for a password prompt.
send "$my_user_id\r"
expect "password:"
# Send the password, and then wait for a shell prompt.
send "$my_password\r"
expect "%"
# Send the prebuilt command, and then wait for another shell prompt.
send "$my_command\r"
expect "%"
# Capture the results of the command into a variable. This can be displayed, or written to disk.
set results $expect_out(buffer)
# Exit the telnet session, and wait for a special end-of-file character.
send "exit\r"
expect eof

【讨论】:

    【解决方案2】:

    秘密在于HEREDOC

    您可以通过以下方式解决此问题:

    $ command-that-needs-input <<EOF
    authenticate here
    issue a command
    issue another command
    EOF
    

    查看我为此处文档提供的链接 - 它包括对变量替换的支持和许多其他有用的东西。尽情享受吧!

    【讨论】:

      猜你喜欢
      • 2019-11-02
      • 2013-04-09
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      相关资源
      最近更新 更多