0

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.

enter image description here

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?

Treize
  • 417
  • 2
  • 5
  • 9

1 Answers1

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));
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • I tried it but I'm having an error. Should I install or activate some applications? C:\files>connect.bat C:\files>if (@CodeSection == @Batch) C:\files\connect.bat(1, 17) Microsoft JScript compilation error: Conditional com pilation is turned off C:\files\connect.bat(1, 17) Microsoft JScript compilation error: Conditional com pilation is turned off C:\files> – Treize Apr 08 '17 at 11:16
  • Ops! I had a typo! The first IF must have an at-sign before: `@if (@CodeSection == @Batch) @then`... I already fixed it in the code – Aacini Apr 08 '17 at 11:51
  • Cool I tested it and it's working, thank you very much! – Treize Apr 08 '17 at 12:11