通过 Bash 脚本通过 Telnet 发送命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25733634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:19:17  来源:igfitidea点击:

Sending Commands through Telnet via Bash Script

bashdebiantelnet

提问by Dylan

I am attempting to automate a telnet session in Debian, but I am having difficulty when it comes to sending commands.

我正在尝试在 Debian 中自动执行 telnet 会话,但是在发送命令时遇到了困难。

By looking here, I had a good understanding of what I needed to do.

通过查看这里,我对我需要做的事情有了很好的了解。

spawn telnet localhost
expect "login:"
send "test\n"
expect "Password:"
send "password\n"
sleep 10 //Time for the connection to be established
send "ls\n"

Running the script, it correctly inputed the username. It got to the password field and i'm assuming it entered it in (though I can't be for sure since passwords are left blank in Debian when entered). It stalled for the wait, then brought me back to my account (root). It did not ever connect to "test".

运行脚本,它正确输入了用户名。它到达密码字段,我假设它输入了它(尽管我不能确定,因为输入时密码在 Debian 中留空)。它停止等待,然后将我带回我的帐户(root)。它从未连接到“测试”。

Modifying my code to be more like my reference:

修改我的代码以使其更像我的参考:

spawn telnet localhost
expect "login:"
send "test\n"
expect "Password:"
send "password\n"
interact

This led to it immediately logging in and connecting in my "test" account. From here I tried adding commands after interact using send, echo, and just typing the command. Nothing would happen until I exited the telnet session, then I would get this error.

这导致它立即登录并连接到我的“测试”帐户。从这里开始,我尝试在使用发送、回显和输入命令进行交互后添加命令。在我退出 telnet 会话之前什么都不会发生,然后我会收到这个错误。

For send:

对于发送:

send: spawn id exp6 not open
    while executing
"send "ls\n""
    (file ".telnet.sh" line 8)

For echo:

对于回声:

invalid command name "echo"
    while executing
"echo "ls\n""
    (file ".telnet.sh" line 8)

With just the command name "ls" invalid command name "ls" while executing "ls" (file ".telnet.sh" line 8)

在执行“ls”(文件“.telnet.sh”第8行)时,仅使用命令名称“ls”无效命令名称“ls”

I realize interact gives control back to the user, but for some reason it is the only way it will actually have me connect into the account.

我意识到交互将控制权交还给用户,但出于某种原因,它是让我真正连接到帐户的唯一方式。

Does anyone know why it is having trouble connecting without interact? If not, why I cannot send those commands until the telnet session has ended with interact?

有谁知道为什么在没有交互的情况下无法连接?如果没有,为什么在 telnet 会话以交互结束之前我不能发送这些命令?

EDITAfter some research I found this link, which at the end comes to a resolve that an expect script ends immediately after everything is finished, closing the telnet session. They said they fixed the issue by adding a expect "$ " to let the script know more commands are being sent its way. I tried just using the '$' symbol like the site suggested, but it ended kicking me again. So I tried this.

编辑经过一些研究,我找到了这个链接,最终解决了一个期望脚本在一切完成后立即结束的问题,关闭了 telnet 会话。他们说他们通过添加一个期望“$”来解决这个问题,让脚本知道更多的命令正在以它的方式发送。我试着像网站建议的那样使用“$”符号,但它再次踢我。所以我尝试了这个。

Here is my updated code:

这是我更新的代码:

#!/usr/bin/expect
spawn telnet localhost
expect "debian login"
send "test\n"
expect "Password:"
send "password\n"
expect "test@debian:~$"
send "ls\n"

Now I am still in the telnet session for about 10 seconds, and then it kicks me off. If I try to enter in a command in that time, it pauses for that time, then runs the command I typed after the telnet session closes.

现在我仍然在 telnet 会话中大约 10 秒钟,然后它让我离开。如果我在那个时候尝试输入命令,它会暂停那段时间,然后在 telnet 会话关闭后运行我输入的命令。

Progress is being made. Slowly but surely.

正在取得进展。缓慢但肯定。

采纳答案by Dylan

I solved this problem by expecting the bash again after I send my command.

我通过在发送命令后再次期待 bash 解决了这个问题。

When ran like this:

当像这样运行时:

send "password\r"
expect "*$*"
send "ls\r"

The telnet session immediately ended. What ended up working was to expect the start of the bash script again. Because the script ends after it sends the last command, it doesn't have a chance to display the result of the command entered.

telnet 会话立即结束。最终的工作是期待 bash 脚本再次启动。因为脚本在发送最后一条命令后结束,所以它没有机会显示输入命令的结果。

I addressed this by adding an additional expect which then allowed my command to process and get the results.

我通过添加一个额外的期望来解决这个问题,然后允许我的命令处理并获得结果。

Here is my final code.

这是我的最终代码。

#!/usr/bin/expect
spawn telnet localhost
set timeout 10
expect "debian login"
send "test\r"
expect "Password:"
send "password\r"
expect "*$*"
send "ls\n"
expect "*$*"
send "command2"
expect "*$*"
send "command3"
expect "*$*"
send "exit\r"

回答by Ram

The problem may be because you are sending the ls command before expecting a terminal , the code expect "*$*" expects a terminal after you enter the password and the regular expression (*$*) may vary depending upon the PS1 environment variable of the telnet session, below is the code which will the list the contents of the current working directory and logs out

问题可能是因为您在等待终端之前发送了 ls 命令,代码expect "*$*" 在您输入密码后需要终端,并且正则表达式 (*$*) 可能因 PS1 环境变量而异telnet 会话,下面是列出当前工作目录的内容并注销的代码

spawn telnet localhost
set timeout 10
expect "debian login"
send "test\r"
expect "Password:"
send "password\r" 
expect "*$*"
send "ls\r"
expect "*$*"
send "logout\r"