Step 3.2 Use PuTTY / plink as a Command Line Replacement for telnet / rlogin

Step 3 2 Use PuTTY plink as a Command Line Replacement for telnet rlogin

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.

Action 3 2 1 Create an SSH session from the command line using PuTTY

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.

  • Click on the Start Menu and select Run . In the field provided, type in cmd if you are running Windows NT/2000/XP or command if you are running Windows 9x/ME and click on the OK button. This will give you a Windows command prompt.
  • From the command prompt, invoke PuTTY using the following syntax:

    
    putty -ssh
    
    [
    
    -l
    
    user] [user@]hostname
    

    Where:

    • user = the account you are trying to access on the remote machine.
    • hostname = the hostname or IP address of the machine you are trying to contact.

    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:

    • session name = the name of the saved session. (If the session name contains spaces, it should be surrounded by quotes.)

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.


Action 3 2 2 Using Plink to initiate an SSH session from the command line or a script

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.

  • Plink is a command line tool that will allow you to log in to a remote machine using SSH and either create an SSH session or execute a command, all from the command line and without opening another window. Plink comes with many command line options that can be accessed with the “h option:

    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:

  • -l user= the account you are trying to access on the remote machine.
  • hostname= the hostname or IP address of the machine you are trying to contact
  • command= optional command you want Plink to execute on the remote machine. If no command is specified, an SSH session will be created.
  • -v= enables verbose mode. This is helpful when something is awry and you want more information about the connection.
  • -batch= disables interactive prompts when Plink is establishing a connection. This is useful when you are running Plink within a script.

    If an interactive prompt appears during connection, Plink will fail the connection rather than hang.

  • -load session = the name of the saved PuTTY session to load. This is used as an alternative to giving Plink a user and hostname with which to connect.

To run Plink:

  • If you are not already at a Windows command prompt, click on the Start Menu and select Run . In the field provided, type in cmd if you are running Windows NT/2000/XP or command if you are running Windows 9x/ME and click on the OK button. This will give you a Windows command prompt.
  • From the Windows command prompt, issue the plink command. For example, to establish a connection to remote host server.example.com as user sshuser:

    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.




OpenSSH. A Survival Guide for Secure Shell Handling, Version 1.0
OpenSSH: A Survival Guide for Secure Shell Handling (Version 1.0)
ISBN: 0972427384
EAN: 2147483647
Year: 2002
Pages: 90

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net