Step 3.4 Use PuTTYs Tools to Transfer Files from the Windows Command Line
Step 3 4 Use PuTTY s Tools to Transfer Files from the Windows Command Line
Problem: Something is needed to replace the insecure FTP and rcp Windows command line utilities.
Action 3 4 1 Interactively transfer files from the command line with PSFTP
- 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: emp New local directory is c: emp psftp> lpwd Current local directory is c: emp 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: emp 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:>
Action 3 4 2 Transfer files from the command line with PSCP
A second method to transfer files from a Windows command line prompt is to use PSCP. Unlike PSFTP, PSCP is not interactive and is designed to transfer files "in one shot" and then exit, much like OpenSSH's scp command. PSCP also allows you to specify wildcards within filenames (PSFTP does not). Additionally, PSCP will work with any SSH server as it is not dependent on SSHv2 being present.
Note |
PSCP will blindly copy files to the remote server, overwriting any files with the same name , without prompting for verification. Be careful when using PSCP to copy files. If you wish to prevent PSCP from overwriting any files, remove any write permissions on any file you wish to keep. This can be done on a UNIX system by issuing the following command: $ chmod --w file This will prevent PSCP from overwriting the file. |
PSCP is run from the command line and has many options, which you can see via the “h option:
C:>pscp -h PuTTY Secure Copy client Release 0.53b Usage: pscp [options] [user@]host:source target pscp [options] source [source...] [user@]host:target pscp [options] -ls user@host:filespec Options: -p preserve file attributes -q quiet, don't show statistics -r copy directories recursively -v show verbose messages -load sessname Load settings from saved session -P port connect to specified port -l user connect with specified username -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 -unsafe allow server-side wildcards (DANGEROUS)
Some of the more common options are explained in more detail below:
- -p = This will preserve the date and timestamps on any file transferred.
- -r = By default, PSCP will only copy files and skip over any directories encountered. The “r option will recursively copy any directory structures encountered .
The following are two examples of utilizing PSCP to exchange files with machine server.example.com as user sshuser .
- 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.
The syntax for downloading a file from a remote machine is as follows :
pscp user@hostname:remote-filename local-filename
where user is the user ID to which you will connect on the remote machine, hostname is the hostname or IP address of the remote machine, remote-filename is the full path and name of the file on the remote machine to download and local-filename is the full path and name of the downloaded file.
It is not necessary to specify the directory for the remote-filename or target-filename arguments. If the directory is not specified in the remote-filename argument, PSCP assumes the file is in the remote home directory of the user ID that is specified. If the directory is not specified in the local-filename argument, PSCP will download the file into the current directory. However, both arguments are required, so if you do not wish to change the name of the downloaded file and you want to put it into the current local directory, specify "." for the local-filename argument. "." represents the current directory:
- Here is an example of a user using PSCP to connect to machine server.example.com as user sshuser and downloading the file named test.pl to the current directory:
C:>pscp sshuser@server.example.com:test.pl . sshuser@server.example.com's password: ******** test.pl 0 kB 0.1 kB/s ETA: 00:00:00 100% C:>
The syntax for uploading a file is as follows:
pscp local-filename user@hostname:remote-filename
where user is the user to connect to on the remote machine, hostname is the hostname or IP address of the remote machine, local-filename is the full path and name of the local file to upload and remote-filename is the full path and name of the remote uploaded file. If no directory is specified for the local-filename argument, the current local directory will be used. If no directory is specified for the remote-filename argument, the remote home directory of the user logging in will be used. However, the colon following the hostname must still be present.
- Here is an example of a user using PSCP to connect to machine server.example.com as user sshuser and uploading all files named test.* to /tmp :
C:>pscp test.* sshuser@server.example.com:/tmp sshuser@server.example.com's password: test.c 0 kB 0.1 kB/s ETA: 00:00:00 100% test.sh 0 kB 0.2 kB/s ETA: 00:00:00 100% C:>