1

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

第一个命令是:

nc address port

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

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

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

提前致谢!

4

2 回答 2

3

查看期望脚本 期望

例子:

# 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
于 2013-03-12T22:23:20.193 回答
1

秘密在于HEREDOC

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

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

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

于 2013-03-12T22:20:38.100 回答