Windows cmd simulate console input a command

删除回忆录丶 提交于 2020-02-06 07:56:16

问题


I need to write a batch script which connects to a vpn automatically when username and password is saved somewhere (Ex: in a file). VPN client is openconnect which provides a CLI but the problem is user input needs to be provided interactively to the command in order for it to complete.

See the output below when I run :

openconnect <serverhostname>

OUTPUT

POST https://<serverhostname>/
Connected to <serverhostname>:443
SSL negotiation with <serverhostname>
Server certificate verify failed: signer not found

Certificate from VPN server "<serverhostname>" failed verification.
Reason: signer not found
To trust this server in future, perhaps add this to your command line:
    --servercert pin-sha256:<somesha>
Enter 'yes' to accept, 'no' to abort; anything else to view:

So I basically have to type yes manually and press Enter (it also prompts for further input), this needs to be automated in a script. Also, it's worth noting that the output is suggesting to provide --server-cert option and I could do that but when it asks the password, there's no option for it.

I tried putting the input lines in a file and redirecting that to the stdin of the command (which did not work but same the method worked on zsh on linux)

openconnect <serverhostname> < inputfile.txt

I also tried piping to stdin of the command which also didn't work.

I think the particular command doesn't read from stdin but directly from the console somehow which I really don't know how, but I could find a bit of information about something called "CON" on cmd.

Any solution is highly appreciated.


回答1:


You can try using SendKey from vbs/bat hybrid script:

Save as .cmd, and adjust the sleep time (in milliseconds) for answer yes in 8th line : WScript.Sleep 1000 :

You can apply more command by edit adding:

WScript.Sleep [time needs]
wshshell.sendkeys "strings[command] you need"
wshshell.sendkeys "{ENTER}"


<!-- :
@echo off && rem mode 50,03 && title <nul && title .\%~nx0 
cmd /k %__APPDIR__%wScript.exe "%~dpnx0?.wsf"
goto :eof
--> <job> <script language = "vbscript">
Set WshShell = wscript.CreateObject("wscript.Shell") : WScript.Sleep 100 
wshshell.sendkeys "openconnect serverhostname" : WScript.Sleep 100  
wshshell.sendkeys "{ENTER}" : WScript.Sleep 1000 : wshshell.sendkeys "yes"
wshshell.sendkeys "{ENTER}" : 'for exit cmd /k add this line wshshell.sendkeys "exit" : wshshell.sendkeys "{ENTER}"
</script></job>

vbs code:


Set WshShell = wscript.CreateObject("wscript.Shell")
WScript.Sleep 100 
wshshell.sendkeys "openconnect serverhostname"
WScript.Sleep 100  
wshshell.sendkeys "{ENTER}"
WScript.Sleep 1000
wshshell.sendkeys "yes"
wshshell.sendkeys "{ENTER}" 
'for exit cmd /k add this 2 line:
wshshell.sendkeys "exit"
wshshell.sendkeys "{ENTER}"


来源:https://stackoverflow.com/questions/59735977/windows-cmd-simulate-console-input-a-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!