SSH to a remote server using PuTTY through Windows batch file?

On Windows I use PuTTY to log in to a remote server via SSH. I want to use a batch script to SSH to the remote server using PuTTY. The server is running a Linux-based OS.

I used the below command to do this:

start C:\Windows\System32\putty.exe -ssh server_name -l pankmish -pw wxyz

However I got the following error:

unable to connect to remote host

If I use this command instead:

start C:\Windows\System32\putty.exe -ssh server_name -l user_name

Everything works well and I get a PuTTY window with username "user_name" in it. If I provide the correct password I am able to connect to the server. However via a batch script I am not able to provide the password when prompted.

How can I solve this?

7

6 Answers

I tried passing the password using command line and it worked fine for me.

start C:\Users\pankmish\Downloads\putty.exe -ssh server_name -l user -pw %1

and executed command from my windows cmd as below

test_file.bat password

2

Make sure that putty is installed and putty.exe is present in C:\Windows\System32

Open up notepad: Type in the following

start putty <username@ip/hostname> -pw <password>

Replace the above with your username, ip and password and save the file as .bat file. I think that should do it.

You may try the same trick of this answer:

@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 putty window with the user name only
start C:\Windows\System32\putty.exe -ssh server_name -l user_name
rem Send the password to putty window
%SendKeys% "wxyz{ENTER}"
goto :EOF
@end
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));

Please, post the result.

1

The below command is working for me as-is from inside of a bat file.

"D:\userdata\panshriv\Desktop\putty.exe" "pankaj@10.91.124.171" -pw "mypassword"

  • My putty.exe is in my desktop
  • My username is "pankaj"
  • My password is "mypassword"
1

I use this code to input an IP address from the user. Fill in username and password with double quote "":

@echo off
set /p
START "C:\Program Files\putty.exe" -ssh %id% -l username -pw "password"
@echo

there are two ways to do this::

  1. put your password in "" and run in command prompt as

start C:\software\putty.exe -ssh server_name -l user -pw "MyPassword$1"

OR

  1. Create a file Connection123.bat with below command

start C:\software\putty.exe -ssh server_name -l user -pw %1

save the file and run the batch file as

start c:\Connection123.bat MyPassword$1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like