3

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?

migrated from stackoverflow.com Apr 2 '14 at 8:18

This question came from our site for professional and enthusiast programmers.

  • did you use Plink to connect putty and remote server. – ambarox Apr 1 '14 at 18:47
  • On Windows, you can create a Powershell script (using the ssh.net library) to connect to a remote server by SSH and automate a task. See powershelladmin.com/wiki/… – mti2935 Apr 1 '14 at 18:47
  • I tried using Plink also to connect putty and remote server, but error was same. – pankmish Apr 1 '14 at 18:49
  • I think error is coming because password prompt is coming in next line of user name prompt. so please suggest me any example batch script to give password when it is prompted. – pankmish Apr 1 '14 at 18:51
  • I tried same command->> start C:\Windows\System32\putty.exe -ssh server_name -l user -pw %1; and provided password from command line and it worked fine. – pankmish Apr 3 '14 at 14:13
4

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

  • 1
    I'd suggest to surround password with double quotes every time, so in case if it contains spaces or other special characters there would be no problems in executing it properly – user373230 Oct 18 '16 at 5:14
  • What if I want to run a command after logging in? – Jack Nicholson Jan 6 '18 at 0:56
3

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.

1

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.

  • Still same issue, not able to connect to the server. After running the above script; in password prompt password value is not being added. – pankmish Apr 2 '14 at 7:11
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 id="Enter IP Address: "

START "C:\Program Files\putty.exe" -ssh %id% -l username -pw "password" 

@echo
1

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.