简体   繁体   English

如何在PowerShell上获取telnet输出?

[英]How to get telnet output on PowerShell?

I thought I could run the command as below on PowerShell. 我以为可以在PowerShell上运行以下命令。

$output = & echo quit | telnet localhost 22
echo $output

However, $output has nothing in it. 但是, $ output中没有任何内容。 I'm trying to verify ssh or telnet connection. 我正在尝试验证ssh或telnet连接。

Could you please tell me how to get output from above command ? 您能告诉我如何从上述命令获取输出吗?

It is not possible. 这不可能。 MS telnet doesn't use stdin/out. MS telnet不使用stdin / out。 See here: C# + telnet process w/ redirected standard streams quits immediately 请参阅此处:具有重定向的标准流的C#+ telnet进程立即退出

For redirecting the output of a telnet session you can use the -f logfile argument and then importing it into a variable after you are done with it: $output = get-contents logfile 要重定向telnet会话的输出,可以使用-f logfile参数,然后在完成后将其导入变量: $output = get-contents logfile

For sending keycommands to telnet you could use $wshell = new-object -com wscript.shell and then: 要将键盘命令发送到telnet,可以使用$wshell = new-object -com wscript.shell ,然后:

start-process telnet -argumentlist "-f w:\tmp\log.txt"; 
sleep 1; 
$wshell.SendKeys("quit{ENTER}")

Yes I know... not what you would expect, but it is the only way afaik with the MS builtin telnet console. 是的,我知道...不是您所期望的,但这是afaik与MS内置telnet控制台的唯一方式。 If you know any better way, I would be glad to hear about it. 如果您知道更好的方法,我将很高兴听到它。

If you only want to check open ports, then why not just query them instead of telnetting? 如果只想检查打开的端口,那为什么不只查询它们而不是telnetting? See here . 这里

Trying to adapt this for collecting Telnet output from APC PDUs (reading current load). 尝试对此进行调整以收集来自APC PDU的Telnet输出(读取当前负载)。 The specified keys are successfully entered and the output is generated in the telnet session window, but the window doesn't close once the telnet session is ended. 在telnet会话窗口中成功输入了指定的键并生成了输出,但是一旦telnet会话结束,该窗口就不会关闭。 It stays open with "Connection to host lost." 它保持打开状态,并显示“主机连接丢失”。

I can press any key to close the window, but PS script execution does not continue, so no code following the SendKeys statement is executed. 我可以按任意键关闭窗口,但PS脚本执行不会继续,因此不会执行SendKeys语句之后的代码。

Am I missing something simple? 我是否缺少简单的东西? Code below. 下面的代码。

$wshell = new-object -com wscript.shell
start-process telnet -argumentlist "pdu1-a6.domain.local -f c:\temptelnet.log"
sleep 1
$wshell.SendKeys("username{ENTER}password{ENTER}current{ENTER}quit{ENTER}")
$telnetoutput = get-content c:\temptelnet.log
$telnetoutput

try this script and add it at start and end of your command to capture every logs 尝试此脚本并将其添加到命令的开头和结尾以捕获每个日志

$ipV4 = (Test-Connection -ComputerName (hostname) -Count 1).IPV4Address.IPAddressToString -- this 

Start-Transcript -Path .\TraceLogs_$ipV4.txt

<**Add your code here**>

Stop-Transcript

Here $ipV4 will capture the source host and logs will be generated in as file name .\\TraceLogs_$ipV4.txt 这里$ ipV4将捕获源主机,日志将以文件名。\\ TraceLogs_ $ ipV4.txt生成。

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

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