BASH脚本为用户名/密码构造

我想用ncat写一个简单的bash脚本来打开一个ISP和它的端口的连接。

第一个命令是:

nc address port 

在这样做的时候,我首先提示提供一个用户名。 我必须按ENTER,然后我会提示input密码,然后我必须再次按ENTER。

在此之后,我想打开一个terminal进程窗口。 任何人都可以指向我足够的资源,这种types的脚本?

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

提前致谢!

检查期望脚本期望

例:

 # 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 

秘密在于HEREDOC

你可以用类似的方法来解决这个问题:

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

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