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.
#!/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
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.
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... firstname.lastname@example.org'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... email@example.com's password: sftp> quit
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.