Problem: Even though most work within Windows is done through a graphical interface, there are times when it is necessary to create an SSH session from a Windows command prompt or from within a script.
There are multiple ways to create an SSH session from the command line using PuTTY.
The first way involves using the PuTTY program itself. PuTTY comes with a number of options that can be used to invoke the graphical PuTTY terminal from the command line. A description of these options is available within the PuTTY help file. To run PuTTY from the command line:
Note |
For any command line PuTTY program to work, the Windows PATH environment variable must be set to include the path to the directory in which the PuTTY programs are installed. |
putty -ssh [ -l user] [user@]hostname
Where:
PuTTY also allows you to open a saved session from the command line. To do this, use one of the following arguments at the command line:
putty -load "session name" putty @"session name"
Where:
For example, if you want to start PuTTY with a connection to server.example.com as user sshuser, type the following at the Windows command prompt and press Enter :
C:>putty -ssh -l sshuser server.example.com
To start PuTTY with a saved session called "ssh server", type the following at the Windows command prompt and press Enter :
C:>putty -load "ssh server"
After authenticating successfully, you will have a shell prompt on the remote machine.
Using PuTTY from the command line as in the way described in the previous section will create an SSH interactive session. This may not be what we want if for example we need to remain at the Windows command line or we want to issue an SSH command from within a script. In order to satisfy these types of needs, PuTTY provides a tool called Plink.
C:>plink -h PuTTY Link: command line connection utility Release 0.53b Usage: plink [options] [user@]host [command] ("host" can also be a PuTTY saved session name) Options: -v show verbose messages -load sessname Load settings from saved session -ssh -telnet -rlogin -raw force use of a particular protocol (default SSH) -P port connect to specified port -l user connect with specified username -m file read remote command(s) from file -batch disable all interactive prompts The following options only apply to SSH connections: -pw passw login with specified password -L listen-port:host:port Forward local port to remote address -R listen-port:host:port Forward remote port to local address -X -x enable / disable X11 forwarding -A -a enable / disable agent forwarding -t -T enable / disable pty allocation -1 -2 force use of particular protocol version -C enable compression -i key private key file for authentication
However, you will probably never use many of the options available. The most common options you will likely use are as follows :
plink [ -v ] [ -batch ] [ -l user] [-load session] [user@]hostname[command]
where:
If an interactive prompt appears during connection, Plink will fail the connection rather than hang.
To run Plink:
C:>plink -ssh sshuser@server.example.com sshuser@server.example.com's password: ******* Last login: Thu Jan 9 10:40:13 2003 from 192.168.1.10 $
In this case, Plink opens up an SSH session from the local machine to the remote host just as PuTTY would.
Note |
Running an SSH connection from within a Windows command prompt may result in undesirable effects, due to the fact that the Windows command prompt does not know how to handle the control codes from the different terminal types that are set up in a remote UNIX shell session. Therefore, running commands such as clear may cause garbage to display on the screen. If possible, use PuTTY to open a shell session to a remote machine. |
Plink can also be used to create an SSH session and execute a command or script on the remote machine from the Windows command line. For example, to connect to remote host server.example.com as user sshuser to run the command uname “a :
C:>plink -ssh sshuser@server.example.com "uname -a" sshuser@server.example.com's password: ******** Linux server.example.com 2.4.7-10smp #1 SMP Thu Sep 6 17:09:31 EDT 2001 i686 unknown C:>
As you can see, Plink connects to the remote machine, is prompted for authentication, and upon successful authentication executes the command provided and displays any output on the local machine.
SECTION I - Obtaining, Compiling and Installing OpenSSH
SECTION II - How to Use OpenSSH Clients for Unix-to-Unix Connectivity
SECTION III - How To Use PuTTY/WinSCP For PC-To-Unix Connectivity
SECTION IV - Using Public Key Authentication
SECTION V - Troubleshooting SSH Connections
SECTION VI - Advanced SSH Topics
Conclusion
Appendix - Sample sshd_config File