How to execute a .sh script on remote server (linux & ubuntu) using PuTTY in windows

How to execute a .sh script on remote server (linux & ubuntu) using PuTTY in windows

PuTTY doesn't need any introduction as its one of the widely remote console utilities. PuTTY is an SSH and telnet client.

Although use the console is already simple and easy you are most likely to make mistakes sometimes (and erase the project folder accidentaly). You can automate your server tasks without type all that you need everyday (go to a folder, do something there, then exit, navigate to other etc).

Make it easy for you and your team. In this article, you'll learn how to execute a shell script from windows to a remote server using PuTTY and the windows command prompt.

Creating a .sh file

Typically a .sh file is a shell script which you can execute in a terminal. This file format is commonly used for Unix shell files, created by the Unix shar utility program.

How safe is it?

You may find it weird that you must perform another task manually in order to execute a file. But this is partially because of strong need for security.

Basically when you download and run a bash script, it is the same thing as somebody telling you "run all these commands in sequence on your computer, i promice that results will be good". Ask yourself if you trust the party that has supplied this file, ask yourself if you are sure that have downloaded the file from the same place as you thought, maybe even have a glance inside to see if something looks out of place (although that requires that you know something about *nix commands and bash programming).

Conclusion : execute only commands that you know , and only if you know how they work.

To get started, create a file somewhere with .sh extension (command_file.sh) somewhere. Then, write some shell script.

In this example (the most basic), we are going to print the date in the Putty console.

# command_file.sh

#Print the date in putty
echo `date`
# Don't close putty to see the output of the date
read -rsp $'Press any key to continue...\n' -n1 key

That was , really easy isn't?.

If you want , start creating your own, the following example shows a basic interaction with commands that you normally execute using putty (subversion command svn update):

Note: as is your first time creating a .sh file to be executed, be sure of what do you to prevent any damage that you can do to your server. We recommend you to test only with the previous script and when you have more experience, try with your own.

# command.sh

# Create a variable named directoryPath
# it contains where your project is located
directoryPath=/var/www/vhosts/myproject
# Go to the project using the cd command
cd $directoryPath
# Update using subversion command
svn update
# Ready ! that was all.
# But let's make something else !
# Delete the content from a folder in the project
# Note: the folder name is "deleteMe" and it is inside /myproject
# Therefore the path is relative to where we are located.
# Real path of folder = /var/www/vhosts/myproject/deleteMe
folderToDelete=deleteMe
#Navigate to the deleteMe folder
cd $folderToDelete
#Delete content using rm command
rm -rf *

# Piece of cake !

Start reading more about all the things that you can do with .sh files in your server. Now that our script is ready, we just need to learn how to execute it automatically.

Executing the script using PuTTY from the windows command prompt

Now that your script file is ready and (theoretically) you know what you want to achieve with it, we just need to execute it and save many precious time.

The structure of the command is the following:

putty.exe [connection (-ssh)] [username@serverIP-or-Domain] [parameter (-pw password)] [parameter (-m (read a remote command or script from a file) sh file to local path)]

The -m option expects to be given a local file name, and it will read a command from that file.
With some servers (particularly Unix systems), you can even put multiple lines in this file and execute more than one command in sequence, or a whole shell script; but this is arguably an abuse, and cannot be expected to work on all servers. In particular, it is known not to work with certain ‘embedded’ servers, such as Cisco routers.
This option is not available in the file transfer tools PSCP and PSFTP.

The entire command can be executed on the command prompt (cmd.exe) in windows easily, e.g:

C:\path\to\putty.exe -ssh username@192.0.0.0.0 -pw "password" -m "C:\path\to\command_sh_script_file.sh"

# Example

C:\Program Files\Putty\putty.exe -ssh user@192.x.x.x.x -pw "ComplexPassword" -m "C:\Users\Admin\Desktop\command_file.sh"

Note : instead of use the full path to the putty executable, you can define an environment variable for putty in windows and then use it as %putty%.

Open the cmd.exe and execute the command replacing respectively the credentials and parameters :

cmd.exe putty implementation

Finally, after the execution of your command in the cmd.exe , putty will be opened and your script will be executed as we made with the first example, the date of the server will be printed in the console :

Shell Script

Tip : you can create an interface using another language like C# to automate even more with a single click

Have fun .

Capture putty output (optional)

If your bash script is not straight-forward (it can generate output with information about errors, information etc), you may want to save the content to analyze it when the script execution is closed.

As putty normally closes the console if there's no statement that pause it when the last line of your script is executed and there's no way to redirect the output (but achievable with plink utility), we need to specify this task to the PuTTY application.

1) Open putty, click on the top icon and select change settings.

Step 1 Putty log in external file

2) Click on Logging item in the right side list. Now select the All session output option button in the Session loggin area.

Putty step 2 log output in external file

Finally, choose a path to putty.log file in your system and click on apply.

All the output generated by putty will be written in this log file.

 

Become a more social person