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

3

0

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?

pankmish

Posted 2014-04-01T18:43:33.760

Reputation: 181

Might I suggest, that you should be using SSH key-based authentication vs Passwords, it will be more secure and easier to configure. (Though the most secure is Key-Based auth w/ password!) – FreeSoftwareServers – 2017-12-21T12:32:51.340

did you use Plink to connect putty and remote server. – None – 2014-04-01T18:47:43.547

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 http://www.powershelladmin.com/wiki/SSH_from_PowerShell_using_the_SSH.NET_library

– None – 2014-04-01T18:47:49.117

I tried using Plink also to connect putty and remote server, but error was same. – pankmish – 2014-04-01T18:49:30.557

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 – 2014-04-01T18:51:32.513

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 – 2014-04-03T14:13:49.003

Answers

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

pankmish

Posted 2014-04-01T18:43:33.760

Reputation: 181

1I'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 – None – 2016-10-18T05:14:32.287

What if I want to run a command after logging in? – Jack Nicholson – 2018-01-06T00:56:39.170

4

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.

xDogg

Posted 2014-04-01T18:43:33.760

Reputation: 41

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"

Pankaj S

Posted 2014-04-01T18:43:33.760

Reputation: 11

That's not an ssh connection. – DavidPostill – 2016-10-18T08:01:50.593

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

Timothy Tran

Posted 2014-04-01T18:43:33.760

Reputation: 11

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

Vipulkumar Gajera

Posted 2014-04-01T18:43:33.760

Reputation: 11

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.

Aacini

Posted 2014-04-01T18:43:33.760

Reputation: 301

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 – 2014-04-02T07:11:23.780