问题
I am running PuTTY from a Perl script using the system command (Windows 7). I use the -m
command line option and it works great, but I do not want a window popping up. It's a short lived session, so it is annoying having the window open up for a second to make the connection and then close.
I searched "windows background" "putty background" and many other variations. I got close but no cigar. The thread "Run putty jobs...MATLAB" Was oh so close, but I was already doing that.
回答1:
You cannot make PuTTY not show its window. See PuTTY wish no-terminal-window.
Though you may use Plink instead. The Plink is a tool from PuTTY suite, with the same functionality and command-line arguments as PuTTY (including the -m
). But contrary to PuTTY, it is a console application. So when you run it from your Perl script, it will inherit the Perl console window. It won't open its own.
See Using the command-line connection tool Plink.
Note that the Plink supports specifying a command on its command line, so you can actually avoid using a temporary file for the -m
switch:
plink.exe -ssh user@example.com "command"
回答2:
This is achievable by using a VB script to call plink.exe -batch -load [SessionName]
.
Assume the command line you need is like this:
plink.exe -batch -load SSHSession
Now you could write a VB script named launchPlink.vbs
to check for a running instance of plink.exe
and that particular session name. If no process was found then load plink.exe
in a hidden window:
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
sQuery = "SELECT CommandLine FROM Win32_Process WHERE Name='plink.exe' AND CommandLine LIKE '%-load SSHSession'"
Set objItems = objWMIService.ExecQuery(sQuery)
If objItems.count = 0 Then
Set objShell = WScript.CreateObject("WScript.Shell")
intReturn = objShell.Run("plink.exe -batch -load SSHSession", 0, False)
End If
To call the script just use the following line:
wscript.exe launchPlink.vbs
This method can also be used with task scheduler to check and restart the connection if something goes wrong.
Look at the following links for more details:
- Ways To Run Batch Files Silently And Hide The Console Window
- VBscript to check if a process is running
- Run Method (WindowsScript Host)
- Win32_Process class
来源:https://stackoverflow.com/questions/32387141/running-putty-m-in-background-no-window-wanted