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

written by: Allan Servedio; article published: year 2007, month 03;


In: Root » Computers and technology » Linux » Using Plink to initiate an SSH session from the command line or a script

Dutch French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic Bookmark and Share this Article

Using PuTTY from the command line 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.

Disclaimer

1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here.

link to this article