How to run a command file in PuTTY using automatic login in a command prompt?

20

8

I am using the following to login automatically to a remote server and then run commands listed in a commands.txt, like this:

C:\path\to\putty.exe -load "[Sessionname]" -l [user] -pw [password] -m C:\path\to\commands.txt

commands.txt contains the following:

ps -elf|grep 'sometext'

However, when I try to do so a new window for PuTTY appears, but it closes and exits instantly after login. As a result, I cannot see the output of the command(s).

I don't understand what's going on here. Am I wrong in my approach or do I need to take more steps to make the PuTTY window pause for some time before exiting?

supportpb

Posted 2012-12-06T05:54:59.713

Reputation:

2Consider to use Plink.exe instead (typically bundled with Putty, remember to include in PATH). I spent 2 hours googling how to specify rsa key in a command line for PuTTY because it's unsafe to hard code passwords. I overlooked Plink solution because, I thought the call had to start with putty. Check answer below mentioning Plink. e.g. `Plink.exe -ssh host -l user -i c:\Users\myName.ssh\myGeneratedPuttyKey.ppk – Soren Havelund Welling – 2016-11-22T14:55:36.943

Answers

11

You should use plink.exe (a command-line interface to the PuTTY back ends) and not putty.exe

You get that from the PuTTY download page

Without plink:

It seems the only way is to use the -log <logfile> options and then print its content and delete it.

Aviram Segal

Posted 2012-12-06T05:54:59.713

Reputation: 231

1@gertvdijk what happens is that he gets fired for using non-approved software on corporate machines... – Torben Gundtofte-Bruun – 2017-05-11T13:31:49.447

unsupported option: -log – Allan Bowe – 2017-06-28T11:36:47.447

I cannot use plink or other command line interfaces. Is there any other way to do this – None – 2012-12-06T06:14:45.117

2@supportpb "I cannot use plink" -- why? what happens if you try? – gertvdijk – 2012-12-06T11:37:22.013

11

First you would need to create a separate file containing all the commands you would like to be executed.

Example: I would like to download and install Drupal on my domain using PuTTY. To do that you must:

First create a .txt file. Within that file are the commands. Mine are: "drush dl drupal" next line, "drush si --account-name=[account name] --account-pass=[account pass] --db-url=mysql://[user]:[pass]@localhost/[database name] --y"

After saving that file, you are now going to create a BAT file or type it in the CMD prompt:

  • ssh.cmd
  • @echo on [for you to see what's going on]
  • [Navigate to your PuTTY installation. Mine is:] cd C:\Program Files\Putty
  • Start putty.exe -ssh [domain name] -l [username] -pw [password] -m [the directory of the .txt file you created which contains the codes you want to be executed]

Jero Dungog

Posted 2012-12-06T05:54:59.713

Reputation: 211

The -m option does not work for me. it is giving out an error saying cannot open command file <path to my text file> – user590849 – 2015-03-07T02:38:43.230

@user590849 does the path to your command text file have any white space? Because I just – Black Frog – 2016-03-11T16:28:17.553

2

Your approach seems good, however, there's one default setting in the behaviour that's causing you trouble here and probably making you believe nothing's happening.

However, when I try to do so a new window for PuTTY appears, but closes and exits instantly after login

Your session ends immediately after the command was executed, and PuTTY closes the window by default. See the documentation of PuTTY on how to change this behaviour for your session.

4.1.3 `Close Window on Exit'

   Finally in the Session panel, there is an option labelled `Close
   Window on Exit'. This controls whether the PuTTY terminal window
   disappears as soon as the session inside it terminates.

gertvdijk

Posted 2012-12-06T05:54:59.713

Reputation: 3 396

Interesting, but that will affect manual sessions as well... – Aviram Segal – 2012-12-06T11:43:12.960

1@AviramSegal It's a session setting. Just duplicate the session configuration, say session-noclose and use that one in scripting. – gertvdijk – 2012-12-06T12:00:15.417

1oh session setting, then yes that will work – Aviram Segal – 2012-12-06T12:01:12.173

2

There would be no issue if you just want to create the batch file to open the Unix machine. Below is the example:

"PuTTY path" -ssh machinename -l username -pw password

The PuTTY path should be in double quotes, like "C:\Program Files\putty\putty.ext"

machinename = machinename without double quotes

password = with quotes

user443234

Posted 2012-12-06T05:54:59.713

Reputation: 21

0

In addition to Aviram's answer:

Below is the example how you will run PuTTY commands from command prompt:

  • First go to the PuTTY installation directory, for example C:\Program Files\PuTTY, and then execute the below command:

    plink.exe -ssh server_ip -P port_no -l user_name -pw password

Vishrant

Posted 2012-12-06T05:54:59.713

Reputation: 115