用户名/密码结构的BASH脚本

时间:2013-03-12 22:19:11

标签: bash shell unix ssh

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

第一个命令是:

nc address port

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

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

我已经知道了用户名和密码,但我不太清楚如何解决这个问题,我必须提供它,然后按回车键。我也不确定如何打开一个新的终端程序。

提前致谢!

2 个答案:

答案 0 :(得分:3)

查看期望脚本 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

答案 1 :(得分:1)

秘密在于HEREDOC

您可以使用类似于:

的内容解决此问题
$ command-that-needs-input <<EOF
authenticate here
issue a command
issue another command
EOF

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