简体   繁体   English

Powershell-隐藏的Windows中的键盘按键

[英]Powershell - Keyboard keystroke in a Hidden Windows

I'm in internship and I have to write a powershell script to automate a telnet script connection. 我正在实习,我必须编写一个powershell脚本来自动执行telnet脚本连接。 I do it with cmd, I send keystroke to connect like this: 我用cmd来做,我发送击键来像这样连接:

$cmd = 'C:\\Windows\\System32\\cmd.exe' Start-Process $cmd -Verb runAs $wshell = New-Object -ComObject wscript.shell; $wshell.AppActivate($cmd) Sleep 3 $wshell.SendKeys('telnet IP_ADDRESS')

And it work fine! 而且效果很好! But I have a problem... 但是我有一个问题

I have to run the cmd window in a hidden style, so I have done this: 我必须以隐藏样式运行cmd窗口,所以我这样做了:

Start-Process $cmd -Verb runAs -Windowstyle hidden
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate($cmd) 
Sleep 3
$wshell.SendKeys('telnet IP_ADDRESS')

The window launch on a hidden style, it's ok, but the keyboard keystroke don't go in the hidden cmd window 窗口以隐藏样式启动,可以,但是键盘按键不在隐藏的cmd窗口中

Do anybody have a solution? 有人有解决办法吗?

The problem is you're using SendKeys which literally just simulates keypresses on the keyboard. 问题是您使用的是SendKeys,它实际上只是模拟键盘上的按键。 If there is no window, there is nothing to send the keypresses to. 如果没有窗口,则没有任何内容可以发送按键。

Start-Process $cmd -ArgumentList "/K telnet IP_ADDRESS" -Verb runAs -Windowstyle hidden

This just tells CMD to run the specified command upon opening, should do the trick. 这只是告诉CMD在打开时运行指定的命令,应该做到这一点。

You can use Putty for telnet. 您可以将Putty用于telnet。 Plink, a command line version of Putty, accepts the password on the command line. Plink是Putty的命令行版本,它在命令行上接受密码。 Unfortunately the telnet protocol (unlike SSH) does not have a standardized mechanism for sending the password. 不幸的是,telnet协议(与SSH不同)没有用于发送密码的标准化机制。 However you can pipe input to Plink's stdin. 但是,您可以将输入通过管道传递到Plink的stdin。 So you could, probably, pipe the password in to Plink. 因此,您可以将密码通过管道传输到Plink。

Note that after you get logged in, you have some additional problems to consider. 请注意,登录后,还需要考虑一些其他问题。 Since you have a hidden Window you must be planning on scripting the whole Telnet session. 由于您有一个隐藏的窗口,因此您必须计划编写整个Telnet会话的脚本。 In order to script Telnet, you probably will need to send the input to telnet when the host program expects it. 为了编写Telnet脚本,您可能需要在主机程序需要时将输入发送到telnet。 If you send all the input at once the host program/OS may not deal with it correctly. 如果一次发送所有输入,则主机程序/ OS可能无法正确处理。 That means you probably need to do something along the lines of Tcl Expect , where you script what output to look for and then what input is sent when the expected output appears. 这意味着您可能需要按照Tcl Expect方式做一些事情,在脚本中编写要查找的输出,然后在出现期望的输出时发送什么输入。 I have seen at least one implementation of something like that in Powershell. 我已经在Powershell中看到了至少一种类似的实现。

BTW, if you can use SSH instead of Telnet you avoid the need to pipe the password in. Piping the password in, like the Telnet protocol in general, is not secure. 顺便说一句,如果您可以使用SSH而不是Telnet,则可以避免通过管道输入密码。像通常使用Telnet协议那样,输入密码并不安全。

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

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