简体   繁体   English

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

[英]BASH scripting for username/password constructs

I want to write a simple bash script using ncat to open a connection to a ISP and its port. 我想用ncat编写一个简单的bash脚本来打开与ISP及其端口的连接。

The first command would be: 第一个命令是:

nc address port

Upon doing this, I am prompted first to provide a username. 执行此操作时,首先提示我提供用户名。 I must hit ENTER, and then I will be prompted to provide a password and then I must hit ENTER again. 我必须按ENTER,然后我将被提示提供密码,然后我必须再次按ENTER键。

After this, I want to open a Terminal process window. 在此之后,我想打开一个终端进程窗口。 Can anyone point me to sufficient resources for this type of scripting? 任何人都可以指出我有足够的资源用于这种类型的脚本吗?

I know the username and password already, but I'm not too sure how to work around the fact that I must provide it and then hit enter. 我已经知道了用户名和密码,但是我不太确定如何处理我必须提供它然后按回车的事实。 I'm also unsure how to open a new Terminal proceses. 我也不确定如何打开一个新的终端程序。

Thanks in advance! 提前致谢!

Check out expect script Expect 查看期望脚本Expect

Example: 例:

# 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

The secret lies in the HEREDOC 秘密在于HEREDOC

You can solve this problem with something akin to: 您可以使用类似于以下内容的方法解决此问题:

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

Look at the link I provided for here documents - it includes support for variable substitution and lots of other useful things. 看看我在这里提供的链接文档 - 它包括对变量替换和许多其他有用的东西的支持。 Enjoy! 请享用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM