Interactively transfer files from the command line with PSFTP

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


In: Root » Computers and technology » Linux » Interactively transfer files from the command line with PSFTP

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

One method to transfer files from the Windows command line is to use PSFTP. PSFTP creates an interactive SFTP file transfer session where you can use many of the commands available within a normal FTP session. Since PSFTP uses the SFTP protocol, which is only available with servers running protocol SSHv2, you may not be able to run it on every server.

PSFTP is run from the command line and provides numerous options. To see the options available run PSFTP with the –h option:

        C:\>psftp -h
        PuTTY Secure File Transfer (SFTP) client
        Release 0.53b
        Usage: psftp [options] user@host
        Options:
          -b file   use specified batchfile
          -bc       output batchfile commands
          -be       don't stop batchfile processing if errors
          -v        show verbose messages
          -load sessname  Load settings from saved session
          -l user   connect with specified username
          -P port   connect to specified port
          -pw passw login with specified password
          -1 -2     force use of particular SSH protocol version
          -C        enable compression
          -i key    private key file for authentication
          -batch    disable all interactive prompts

Once you have started PSFTP, you will be placed into the PSFTP shell where you can connect to a remote machine (if you have not already done so), transfer files and modify file and directory attributes. To view a list of the commands available and what they do, run the help command from within the PSFTP shell:

        psftp> help
        !      run a local Windows command
        bye    finish your SFTP session
        cd     change your remote working directory
        chmod  change file permissions and modes
        del    delete a file
        dir    list contents of a remote directory
        exit   finish your SFTP session
        get    download a file from the server to your local machine
        help   give help
        lcd    change local working directory
        lpwd   print local working directory
        ls     list contents of a remote directory
        mkdir  create a directory on the remote server
        mv     move or rename a file on the remote server
        open   connect to a host
        put    upload a file from your local machine to the server
        pwd    print your remote working directory
        quit   finish your SFTP session
        reget  continue downloading a file
        ren    move or rename a file on the remote server
        reput  continue uploading a file
        rm     delete a file
        rmdir  remove a directory on the remote server

The following is an example PSFTP session showing how to connect to remote server server.example.com as user sshuser and transfer files.

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.

Next, type psftp at the command line and you will be placed into a PSFTP shell. Remember, you must have the path to the PuTTY executables defined within your Windows environment variable "PATH" in order for this to work.

        C:\> psftp
        psftp: no hostname specified; use "open host.name" to connect
        psftp>

Open a connection to the remote machine by invoking the open command with the following syntax:
open [user@]hostname

where user is the optional user ID you will connect to the remote machine as and hostname is the name or IP address of the host to which you will connect. You can alternatively specify the remote host and user name when invoking PSFTP from the command line.

If you have not connected to this machine previously, you may be asked whether or not you want to cache the host key.

Once the connection to the machine is open, you will be prompted for authentication. Upon successful authentication, the connection to the remote machine will have completed and the transfer of files can begin.

        psftp> open sshuser@server.example.com
        Using username "sshuser".
        sshuser@server.example.com's password: ********
        Remote working directory is /home/sshuser
        psftp>

Next, you should verify the current local directory. This is done with the lpwd command. If you are not within the correct directory, the current local directory can be changed with the lcd command which takes a directory name as its argument. For example:

        psftp> lpwd
        Current local directory is C:\
        psftp> lcd c:\temp
        New local directory is c:\temp
        psftp> lpwd
        Current local directory is c:\temp
        psftp>

Once you are within the correct local directory, you can change to the correct directory on the remote machine. This is done using the pwd and cd commands. The pwd command will print the remote connection's current directory and cd will change the remote connection to the directory provided as an argument.

        psftp> pwd
        Remote directory is /home/sshuser
        psftp> cd /tmp
        Remote directory is now /tmp
        psftp>

Now that you are within the correct remote directory, you can verify that the file to download is present. This is done using the dir command. The dir command will display a UNIX-style listing of the current remote directory:

 Note  For those familiar with UNIX, the "ls" command can be used in place of "dir". Use the "help" command to see a full listing of commands available.
 

        psftp> dir
        Listing directory /tmp
        drwx------    2 root    root       1024 Jan 9 14:07 .
        drwxr-xr-x    8 root    root       1024 Jan 9 14:06 ..
        -rw-r--r--    1 root    root        124 Jan 9 14:06 test.c
        -rw-r--r--    1 root    root       3511 Jan 9 14:06 test.C
        -rw-r--r--    1 sshuser sshuser     151 Jan 9 14:07 test.pl
        psftp>

Use the get command to download remote files. The get command's syntax is as follows:

    get remote-filename[local-filename]

where remote-filename is the name of the file you wish to download. You can optionally specify a new name for the downloaded file with the local-filename argument:

    psftp> get test.pl
    remote:/tmp/test.pl => local:test.pl
    psftp>

You have seen the PSFTP lpwd command to show the current local directory and the lcd command to change the current local directory, but there are no commands to see the contents of the local directory. To accomplish this, you can use the "!" command. The "!" command will execute the supplied OS command and receive and display whatever output is returned.

So, in order to see if a file you wish to upload is present in the current local directory - test.sh in this case - we will use the "!" command with dirtest.sh as its arguments.

        psftp> !dir test.sh

         Volume in drive C has no label.
         Volume Serial Number is 1234-ABCD

         Directory of C:\temp

        07/10/02 03:03p                     165 test.sh
                        1 File(s)             165 bytes
                                    4,577,963,520 bytes free
psftp>

You can upload files to the remote server using the put command. The syntax for the put command is as follows:

    put local-filename[remote-filename]

where local-filename is the name of the file you wish to upload. You can also optionally specify a new name for the file to be uploaded as with the remote-filename argument.

      psftp> put test.sh
      local:test.sh => remote:/tmp/test.sh
      psftp> dir
      Listing directory /tmp
      drwxrwxrwt     2 root      root         1024 Jan 9 14:07 .
      drwxr-xr-x     8 root      root         1024 Jan 9 14:06 ..
      -rw-r--r--     1 root      root          124 Jan 9 14:06 test.c
      -rw-r--r--     1 root      root         3511 Jan 9 14:06 test.C
      -rw-r--r--     1 sshuser   sshuser       151 Jan 9 14:07 test.pl
      -rw-rw-r--     1 sshuser   sshuser       165 Jan 9 14:37 test.sh
      psftp>

To close down a PSFTP connection, issue the bye command and you will be returned to the Windows command prompt:

      psftp> bye

      C:\>

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