本站内容有下面分类知识,欢迎您的到来^_^

shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言

设soyo123为首页 收藏本站
当前位置:|主页>shell相关E文资料>

#35 Making sftp Look More Like ftp

百度收藏 QQ搜藏

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 "$1" ] ; then
  echo -n "Remote host: "
  read host
  if [ -z $host ] ; then
    exit 0
  fi
else
  host=$1
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 subsystem|path] [-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.


上一篇:#34 Emulating GNU-Style Flags with Quota 下一篇:#36 Fixing grep
power by soyo123 2007-2008