Notes: Unix Lab 10

  1. ftp -file transfer protocol
  2. sftp - secure file transfer program
  3. scp - secure copy
  4. wget

  1. ftp - file transfer protocol

    ftp is a program that allows the transfer of files between two unix hosts. It is generally used interactively, although its functions may be scripted with a shell script.

     
        
    >>
    >> ftp
    ftp> open
    (to) apollo
    Connected to apollo.lsc.vsc.edu.
    220 ProFTPD 1.2.8 Server (ProFTPD Default Installation) [apollo.lsc.vsc.edu]
    Name (apollo:mark): tuckerm
    331 Password required for tuckerm.
    Password:
    230 User tuckerm logged in.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> bye
    221 Goodbye.
    >>
    
    Above, the ftp session is initiated with the ftp command. ftp then prompts the user for the next action, which in this case is to open a connection. ftp then prompts for the name of the host to connect to. Once connected, the remote host requests the appropriate username and password. Give the correct credentials, the user is then left with the ftp command prompt where subsequent actions such as retrieving files may be done. ftp may be terminated by entering the command "bye".

     
        
    >>
    >> ftp apollo
    Connected to apollo.lsc.vsc.edu.
    220 ProFTPD 1.2.8 Server (ProFTPD Default Installation) [apollo.lsc.vsc.edu]
    Name (apollo:mark): tuckerm
    331 Password required for tuckerm.
    Password:
    230 User tuckerm logged in.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> bye
    221 Goodbye.
    >>
    
    The example above is the same as the previous example only the sesion was started by specifying the remote host to connect to on the ftp command line.

    interactive commands:

    • navigating the local and remote filesystems within ftp
      Once connected to a remote host you will be able to navigate its file system with the cd command. Unless the ftp host is configured differently, you will start in the home directory of the user that you authenticated your ftp connection with. All cd commands will be performed on the remote host's file system, not the local file system. By default, ftp will download any requested files to the current directory that the user's shell was in when the ftp program was started. If you wish to change the current working directory on the local machine you may use the lcd command ("local cd"). You may use the command ls to show a long listing of the files in the current directory on the remote host. Example ftp session below:
       
          
      >>
       ftp apollo
      Connected to apollo.lsc.vsc.edu.
      220 ProFTPD 1.2.8 Server (ProFTPD Default Installation) [apollo.lsc.vsc.edu]
      Name (apollo:mark): tuckerm
      331 Password required for tuckerm.
      Password:
      230 User tuckerm logged in.
      Remote system type is UNIX.
      Using binary mode to transfer files.
      ftp> lcd /data
      Local directory now /data
      ftp> cd /web/apollo/htdocs
      250 CWD command successful.
      ftp> pwd
      257 "/web/apollo/htdocs" is current directory.
      ftp> ls
      200 PORT command successful
      150 Opening ASCII mode data connection for file list
      drwxrwxr-x   8 alumni   www-data     2048 Nov 21 16:06 alum
      drwxrwxr-x  28 ams      ams          4096 Feb 17 01:29 ams
      drwxr-xr-x   2 metadmin metadmin     8192 Feb 18 17:06 anim_lsc
      drwxr-xr-x   4 metadmin metadmin     2048 Oct 20 14:52 campbell
      drwxrwxr-x  26 tuckerm  faculty      2048 Jan 20 18:27 classes
      drwxrwxr-x   5 tuckerm  tuckerm      2048 Dec 11 22:51 common
      drwxrwxr-x   2 tuckerm  tuckerm      2048 Oct 23 13:05 data
      drwxr-xr-x   9 tuckerm  tuckerm      2048 Nov  6 13:24 dept
      -rw-r--r--   1 tuckerm  tuckerm      3262 Nov  6 17:55 favicon.ico
      drwxrwxr-x   2 tuckerm  webscript     2048 Dec  1 20:23 forecast
      drwxr-xr-x   5 tuckerm  tuckerm      2048 Dec 17  2002 idm3020
      -rw-rw-r--   1 tuckerm  tuckerm      2761 Dec 18 17:24 index.html
      drwxrwxr-x  18 tuckerm  metadmin     2048 Feb 11 18:37 intranet
      drwxrwxr-x   3 tuckerm  tuckerm      2048 Dec 30 02:55 main
      drwxrwxr-x  11 tuckerm  tuckerm      2048 Feb  6 19:18 metadmin
      drwxrwxr-x   2 tuckerm  tuckerm      2048 Nov 21 14:58 newalum
      drwxrwxr-x   8 tuckerm  tuckerm      2048 Feb 14 14:07 photos
      drwxrwxr-x   3 tuckerm  tuckerm      2048 Oct 29 17:06 ppt
      drwxr-xr-x  25 tuckerm  tuckerm      2048 Nov  1 03:00 seabreeze
      drwxr-xr-x   2 tuckerm  tuckerm      2048 Oct  7 20:35 weather_prog
      drwxrwxr-x   2 tuckerm  web          2048 Nov  7 02:00 webcam
      drwxrwxr-x  10 tuckerm  tuckerm      2048 Dec 31 18:35 wxdata
      226 Transfer complete.
      ftp> bye
      221 Goodbye.
      >>
      >>
      
    • transfer mode
      ftp has two "modes" for transferring files: ascii and binary. Ascii transfer mode will convert any carriage return/line feed data to match that of the local machine if you are retrieving files or to that of the remote machine if you are transfererring files to the remote host. This conversion will cause binary data files (images, executables, tar files, etc.) to become corrupt so it should generally be avoided. In the case where binary data is to be transferred the binary mode should be used. The ftp commands bin and asc set the transfer mode to binary or ascii respectively. Unless you have a specific need, it is best to use the binary transfer mode.
       
          
      >>
      >> ftp apollo
      Connected to apollo.lsc.vsc.edu.
      220 ProFTPD 1.2.8 Server (ProFTPD Default Installation) [apollo.lsc.vsc.edu]
      Name (apollo:mark): tuckerm
      331 Password required for tuckerm.
      Password:
      230 User tuckerm logged in.
      Remote system type is UNIX.
      Using binary mode to transfer files.
      ftp> asc
      200 Type set to A
      ftp> bin
      200 Type set to I
      ftp> bye
      221 Goodbye.
      >>
      
    • ftp get command
      Once connected to a remote host, the get command will copy a file from the remote host to the local host.
       
          
      >>
      >> ftp apollo
      Connected to apollo.lsc.vsc.edu.
      220 ProFTPD 1.2.8 Server (ProFTPD Default Installation) [apollo.lsc.vsc.edu]
      Name (apollo:mark): tuckerm
      331 Password required for tuckerm.
      Password:
      230 User tuckerm logged in.
      Remote system type is UNIX.
      Using binary mode to transfer files.
      ftp> get unix5.txt
      local: unix5.txt remote: unix5.txt
      200 PORT command successful
      150 Opening BINARY mode data connection for unix5.txt (772 bytes)
      226 Transfer complete.
      772 bytes received in 0.0111 secs (68 Kbytes/sec)
      ftp> bye
      221 Goodbye.
      >>
      
    • put
      The inverse of the get command is the put command. This will upload a file from the current working directory to the current directory on the remote host.
       
          
      >>
      >> ftp apollo
      Connected to apollo.lsc.vsc.edu.
      220 ProFTPD 1.2.8 Server (ProFTPD Default Installation) [apollo.lsc.vsc.edu]
      Name (apollo:mark): tuckerm
      331 Password required for tuckerm.
      Password:
      230 User tuckerm logged in.
      Remote system type is UNIX.
      Using binary mode to transfer files.
      ftp> put wrf.tgz
      local: wrf.tgz remote: wrf.tgz
      200 PORT command successful
      150 Opening BINARY mode data connection for wrf.tgz
      226 Transfer complete.
      13601558 bytes sent in 1.35 secs (9.8e+03 Kbytes/sec)
      ftp> bye
      221 Goodbye.
      >>
      
    • mget,mput
      mget and mput allow you to download or upload files (respectively) using common unix wildcard characters to specify what files to transfer.
       
          
      >>
      >> ftp apollo
      Connected to apollo.lsc.vsc.edu.
      220 ProFTPD 1.2.8 Server (ProFTPD Default Installation) [apollo.lsc.vsc.edu]
      Name (apollo:mark): tuckerm
      331 Password required for tuckerm.
      Password:
      230 User tuckerm logged in.
      Remote system type is UNIX.
      Using binary mode to transfer files.
      ftp> mget *.jpg
      mget daffhood1024.jpg? y
      200 PORT command successful
      150 Opening BINARY mode data connection for daffhood1024.jpg (137628 bytes)
      226 Transfer complete.
      137628 bytes received in 0.042 secs (3.2e+03 Kbytes/sec)
      mget isabelle.jpg? y
      200 PORT command successful
      150 Opening BINARY mode data connection for isabelle.jpg (50107 bytes)
      226 Transfer complete.
      50107 bytes received in 0.0312 secs (1.6e+03 Kbytes/sec)
      mget mtns3.jpg? y
      200 PORT command successful
      150 Opening BINARY mode data connection for mtns3.jpg (7803 bytes)
      226 Transfer complete.
      7803 bytes received in 9.8e-05 secs (7.8e+04 Kbytes/sec)
      mget noctilucent.jpg? y
      200 PORT command successful
      150 Opening BINARY mode data connection for noctilucent.jpg (30792 bytes)
      226 Transfer complete.
      30792 bytes received in 0.0185 secs (1.6e+03 Kbytes/sec)
      ftp> bye
      221 Goodbye.
      >>
      

      In the example above, mget prompts the user for each file that is transferred. To disable this confirmation, use the command prompt and all group transferrs will occur without the confirmation prompt.
    • anonymous ftp
      ftp can be setup on the remote host to allow any user to login without a proper username or password. In this case, the username will be "anonymous" and the password may be anything. Typically, a connecting user will provide their email address as the password but it is usually not required.
       
          
      >>
      >> ftp ftp.us.debian.org
      Connected to ftp.us.debian.org.
      220 saens.debian.org FTP server (vsftpd)
      Name (ftp.us.debian.org:mark): anonymous
      331 Please specify the password.
      Password:
      230-
      230-This site is just another one in a worldwide array of Debian mirrors.
      230-It is not the "primary Debian FTP site" -- it is merely an official
      230-mirror that is in the United States of America.
      230-
      230-If you are connecting from outside the USA, please consider using another
      230-official Debian mirror, one that is closer to you. This will likely help
      230-you by speeding up your downloads, and it will help us by lessening the
      230-load on this machine.
      230-
      230-Current list of Debian mirrors is at http://www.debian.org/mirror/list
      230-
      230 Login successful. Have fun.
      Remote system type is UNIX.
      Using binary mode to transfer files.
      ftp> bye
      221 Goodbye.
      >>
      

  2. sftp - secure file transfer program

    sftp functions very simliarly to ftp only is uses the encryption and authentication mechanisms found in the ssh program. Not all hosts supporting ssh will have sftp enabled.
     
        
    >>
    >> sftp tuckerm@apollo
    Connecting to apollo...
    The authenticity of host 'apollo (155.42.21.5)' can't be established.
    RSA key fingerprint is 8c:7a:4d:bd:f0:f4:0d:43:22:b2:4b:66:1b:26:44:ab.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'apollo,155.42.21.5' (RSA) to the list of known hosts.
    tuckerm@apollo's password:
    sftp> get who.out
    Fetching /mnt/homes/tuckerm/who.out to who.out
    /mnt/homes/tuckerm/who.out
    sftp> bye
    >>
    
    Note that all of the common ftp commands such as ls, pwd, lcd and cd all function in the same manner. sftp does not support different transfer modes as all are done in "binary".

  3. scp - secure copy

    scp functions similar to the unix cp command with the difference being that you can specify remote files or directories as the source or destination of the copy function. scp like sftp uses the same encryption and authentication as the ssh command. scp can specify the remote host, username and path to the file in the following manner:

    username@host:/path/filename

    "username" would be the name of the user account on the remote machine that will be used to log onto the remote host. "host" would be the hostname or network address of the remote host. "/path/filename" would be the absolute path to the file "filename" on the remote host. A few examples of file copies using scp are below:

    • Copy file from remote host to local directory:
       
      >>
      >> scp root@omega:/scripts/runme.sh /newscripts/
      >>
      
      The first argument above, root@omega:/root/runme.sh specifies the source for the file copy. scp will log on to the host omega as the user named root. The host and username information is separated from the remote file and path by the colon ":" The file specified /scripts/runme.sh will be found on the remote host, omega, and copied from there to the directory /newscripts.

    • Copy file from remote host to current working directory:
       
      >>
      >> scp tuckerm@omega:/path/to/some/file.txt .
      >>
      
      The command above will connect to the host Omega, using the tuckerm account. The file on Omega, /path/to/some/file.txt, will be copied from its location on Omega to the current directory on the local machine.

    • Copy file from local host to directory on remote host:
       
          
      >>
      >> scp localfile.txt tuckerm@kangaroo:/path/to/place/file/
      >>
      
      In this example, the file localfile.txt will be copied from the current working directory on the local host to the host named kangaroo. On kangaroo, the file will be copied to the directory /path/to/place/file/ using the tuckerm account on kangaroo.

    • Copy file from local host to home directory on remote host:
       
          
      >>
      >> scp /absolute/path/to/file.dat tuckerm@kangaroo:
      >>
      
      This command will copy the file named file.dat located in the directory /absolulte/path/to/ on the local machine. It will be copied to tuckerm's home directory on kangaroo.

  4. wget

    wget is a commandline file retrieval tool that can fetch files from ftp or web servers. It does not display the contents of the file, it only downloads a copy of the file to the current directory. There are many options that can modify the behavior of wget.
     
        
    >>
    >> wget http://apollo.lsc.vsc.edu/index.html
    --18:33:19--  http://apollo.lsc.vsc.edu/index.html
               => `index.html'
    Resolving apollo.lsc.vsc.edu... done.
    Connecting to apollo.lsc.vsc.edu[155.42.21.5]:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: unspecified [text/html]
    
        [ <=>                                                           ] 16,843       498.43K/s
    
    18:33:19 (498.43 KB/s) - `index.html' saved [16843]
    >>
    

    wget fetching files via anonymous ftp:
     
        
    >>
    >> wget ftp.us.debian.org:/debian/README
    --18:35:02--  ftp://ftp.us.debian.org//debian/README
               => `README'
    Resolving ftp.us.debian.org... done.
    Connecting to ftp.us.debian.org[204.152.189.120]:21... connected.
    Logging in as anonymous ... Logged in!
    ==> SYST ... done.    ==> PWD ... done.
    ==> TYPE I ... done.  ==> CWD /debian ... done.
    ==> PORT ... done.    ==> RETR README ... done.
    Length: 943 (unauthoritative)
    
    100%[==============================================================>] 943            3.18K/s    ETA 00:00
    
    18:35:14 (3.18 KB/s) - `README' saved [943]
    
    
    >>