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

Solution 1:

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]

Solution 2:

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.

Solution 3:

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.