35 Making sftp Look More Like ftp

#35 Making sftp Look More Like ftp

The secure version of the file transfer protocol ftp program is included as part of ssh , the secure shell package, but its interface can be a bit confusing for users who are making the switch from the crusty old ftp client. The basic problem is that ftp is invoked as ftp remotehost , and it then prompts for account and password information. By contrast, sftp wants to know the account and remote host on the command line and won't work properly (or as expected) if only the host is specified.

To address this, a simple wrapper script allows users to invoke mysftp exactly as they would have invoked the ftp program, using prompts for needed fields.

The Code

 #!/bin/sh # mysftp - Makes sftp start up more like ftp. echo -n "User account: " read account if [ -z $account ] ; then   exit 0;       # changed their mind, presumably fi if [ -z "" ] ; then   echo -n "Remote host: "   read host   if [ -z $host ] ; then     exit 0   fi else   host= fi # End by switching to sftp. The -C flag enables compression here. exec /usr/bin/sftp -C $account@$host 

Running the Script

As with the ftp client, if users omit the remote host the script continues by prompting for a remote host, but if the script is invoked as mysftp remotehost , the remotehost provided is used instead.

The Results

First off, what happens if you invoke sftp without any arguments?

 $  sftp  usage: sftp [-vC1] [-b batchfile] [-o option] [-s subsystempath] [-B buffer_size]             [-F config] [-P direct server path] [-S program]             [user@]host[:file [file]] 

Useful, but confusing. By contrast, invoke this script without any arguments and you can proceed to make an actual connection:

 $  mysftp  User account:  taylor  Remote host:  intuitive.com  Connecting to intuitive.com... taylor@intuitive.com's password: sftp>  quit  

Invoke the script as if it were an ftp session by supplying the remote host, and it'll prompt for the remote account name and then invisibly invoke sftp :

 $  mysftp intuitive.com  User account:  taylor  Connecting to intuitive.com... taylor@intuitive.com's password: sftp>  quit  

Hacking the Script

There's a trick in this script worth mentioning: The last line is an exec call. What this does is replace the currently running shell with the application specified. Because you know there's nothing left to do after calling the sftp command, this method of ending our script is more efficient than having the shell hanging around waiting for sftp to end.

We'll revisit the sftp command in Script #83, to see how it can be used to securely and automatically synchronize a local and remote directory.

Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

Similar book on Amazon

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