input second username and password in putty using

2019-06-12 04:32发布

问题:

I would like to know how to automate an input of username and password in putty after the initial login was successful.

I have a bat file connect.bat

@echo off

putty [user]@[host] -pw [password]

After I run this, it opens a new putty window and validates the login.

When successful, I am redirected not to the shell but to another login screen. Sample below.

Then I need to manually input a different username and password by typing the following.

[username]
[ENTER KEY]
[password]
[ENTER KEY]

To be finally redirected to the host's menu.

I tried the following:

echo [username] & [password] | putty [user]@[host] -pw [password] 

putty [user]@[host] -pw [password] < user_pass.txt
putty [user]@[host] -pw [password] -m user_pass.txt

but failed, is there a way to perform this via batch/powershell script or should I incorporate another application?

回答1:

You may try the Sendkeys method:

@if (@CodeSection == @Batch) @then


@echo off

rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"

rem Start the other program in the same Window
start "" /B putty [user]@[host] -pw [password]

rem Send the additional keys
%SendKeys% "[username]{ENTER}"
ping -n 2 localhost > NUL
%SendKeys% "[password]{ENTER}"

goto :EOF


@end


// JScript section

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));