UNIXETC

LFTP使用实例

lftp是一个功能强大的ftp/http客户端,作者是 Alexander Lukyanov。

lftp支持多种传输协议,如ftp、http、https、sftp、exp等。

如同BASH一样,lftp支持任务管理。并且具备书签管理、内置镜像和多线程传送功能。

使用LFTP

lftp登录服务器

	lftp ftp://user:password@ip:port

以上是一个完整的lftp登录示例,其中包含协议(ftp)、用户名(user)、密码(password)、主机(ip)以及端口(port)

默认情况下lftp直接访问ftp服务,默认登录端口为22,因此我们可以使用以下方法来便捷登录主机

	lftp user@ip

然后系统会提示输入password,完成后登录成功!

lftp命令行参数

	lftp :~> help
		!<shell-command>                     (commands)
		alias [<name> [<value>]]             attach [PID]
		bookmark [SUBCMD]                    cache [SUBCMD]
		cat [-b] <files>                     cd <rdir>
		chmod [OPTS] mode file...            close [-a]
		[re]cls [opts] [path/][pattern]      debug [<level>|off] [-o <file>]
		du [options] <dirs>                  exit [<code>|bg]
		get [OPTS] <rfile> [-o <lfile>]      glob [OPTS] <cmd> <args>
		help [<cmd>]                         history -w file|-r file|-c|-l [cnt]
		jobs [-v] [<job_no...>]              kill all|<job_no>
		lcd <ldir>                           lftp [OPTS] <site>
		ln [-s] <file1> <file2>              ls [<args>]
		mget [OPTS] <files>                  mirror [OPTS] [remote [local]]
		mkdir [-p] <dirs>                    module name [args]
		more <files>                         mput [OPTS] <files>
		mrm <files>                          mv <file1> <file2>
		[re]nlist [<args>]                   open [OPTS] <site>
		pget [OPTS] <rfile> [-o <lfile>]     put [OPTS] <lfile> [-o <rfile>]
		pwd [-p]                             queue [OPTS] [<cmd>]
		quote <cmd>                          repeat [OPTS] [delay] [command]
		rm [-r] [-f] <files>                 rmdir [-f] <dirs>
		scache [<session_no>]                set [OPT] [<var> [<val>]]
		site <site-cmd>                      source <file>
		torrent [-O <dir>] <file|URL>...     user <user|URL> [<pass>]
		wait [<jobno>]                       zcat <files>
		zmore <files>

常用的命令解释如下:

lftp远端操作

lftp本地操作

LFTP使用范例

lftp登录服务器

	root@hostus:~# lftp [email protected]
	Password:
	lftp [email protected]:~> ls

lftp列出服务器目录文件

	lftp [email protected]:/> ls
	drwxr-xr-x    2 0        0            4096 Jan 23  2015 backup
	drwxr-xr-x    2 675      100          4096 Jan 23  2015 cgi-bin
	drwxr-xr-x    2 0        0            4096 Apr 01 03:40 ftplogs
	drwxr-xr-x    4 675      100          4096 Apr 01 11:02 htdocs
	drwxr-xr-x    2 675      100          4096 Jan 23  2015 myfolder
	drwxr-xr-x    2 0        0            4096 Apr 01 02:03 wwwlogs

lftp切换远端目录

	lftp [email protected]:/> cd htdocs/
	lftp [email protected]:/htdocs>

lftp上传文件

	lftp [email protected]:/htdocs> put tb7302.apk
	`tb7302.apk' at 1913856 (24%) 77.7K/s eta:75s [Sending data]
	
	7713808 bytes transferred in 78 seconds (97.0K/s)

lftp下载文件

	lftp [email protected]:/htdocs> get bk.apk
	`bk.apk' at 124528 (1%) 28.1K/s eta:4m [Receiving data]
	
	7713808 bytes transferred in 519 seconds (14.5K/s)

lftp上传文件夹

	lftp [email protected]:/htdocs> mirror -R bootstrap/
	Total: 4 directories, 14 files, 0 symlinks
	New: 14 files, 0 symlinks
	1079609 bytes transferred in 7 seconds (150.7 KiB/s)

lftp下载文件夹

	lftp [email protected]:/htdocs> mirror bootstrap/ new
	Total: 4 directories, 14 files, 0 symlinks
	New: 14 files, 0 symlinks
	1079609 bytes transferred in 6 seconds (189.9 KiB/s)
	lftp [email protected]:/htdocs> !ls new/
	css  fonts  js

lftp中文乱码

有时由于编码问题,我们看到的中文可能是乱码,如下:

	lftp [email protected]:/> ls
	drwxr-xr-x    2 0        0            4096 Jan 23  2015 backup
	drwxr-xr-x    2 675      100          4096 Jan 23  2015 cgi-bin
	drwxr-xr-x    2 0        0            4096 Apr 01 03:40 ftplogs
	drwxr-xr-x    4 675      100          4096 Apr 01 11:02 htdocs
	drwxr-xr-x    2 675      100          4096 Jan 23  2015 myfolder
	drwxr-xr-x    2 0        0            4096 Apr 01 02:03 wwwlogs
	-r-xr--r--    1 55961    100           606 Aug 31  2015 ▒▒▒ȶ▒▒▒.txt

如果出现这种情况,就需要进行编码设置,如下:

	lftp [email protected]:/> set ftp:charset GBK
	lftp [email protected]:/> set file:charset UTF-8

设置完成后再看:

	lftp [email protected]:/> ls
	drwxr-xr-x    2 0        0            4096 Jan 23  2015 backup
	drwxr-xr-x    2 675      100          4096 Jan 23  2015 cgi-bin
	drwxr-xr-x    2 0        0            4096 Apr 01 03:40 ftplogs
	drwxr-xr-x    4 675      100          4096 Apr 01 11:33 htdocs
	drwxr-xr-x    2 675      100          4096 Jan 23  2015 myfolder
	drwxr-xr-x    2 0        0            4096 Apr 01 02:03 wwwlogs
	-r-xr--r--    1 55961    100           606 Aug 31  2015 请先读我.txt

以上两条set命令可以写入lftp配置文件,以方便以后使用,关于lftp配置文件参考下节介绍。

LFTP书签使用方法

我们可以使用书签来存储一些商用的ftp登录信息,lftp的书签文件为~/.lftp/bookmarks,我们可以编辑它来添加书签

lftp书签格式如下

	bkname ftp://user:password@ip:port

设置好书签后,可以使用lftp加书签名来登录

	lftp bkname

书签使用示例:

	root@hostus:~# cat ~/.lftp/bookmarks
	aliyun ftp://[email protected]
	root@hostus:~# lftp aliyun
	Password:
	lftp [email protected]:~> ls
	drwxr-xr-x    2 0        0            4096 Jan 23  2015 backup
	drwxr-xr-x    2 675      100          4096 Jan 23  2015 cgi-bin
	drwxr-xr-x    2 0        0            4096 Apr 01 03:40 ftplogs
	drwxr-xr-x    4 675      100          4096 Apr 01 11:33 htdocs
	drwxr-xr-x    2 675      100          4096 Jan 23  2015 myfolder
	drwxr-xr-x    2 0        0            4096 Apr 01 02:03 wwwlogs

LFTP配置文件

lftp执行配置文件顺序为/etc/lftp.conf~/.lftprc~/.lftp/rc,我们可以编辑这些修改这些文件,来达到全局配置或者用户自定义配置。

set

在设置文件中,使用set来设置一些变量的值,如下:

	set ftp:ssl-force on
	set ssl:verify-certificate on
	set ftp:use-feat off
	set ftp:ssl-protect-data on

debug

默认情况下lftp是静默提示的,如果需要查看一些提示信息,就要对debug进行设置

alias

定义命令别名,比如为ls设置别名dir以方便个人使用习惯,如下:

	alias dir ls
	alias less more
	alias zless zmore
	alias bzless bzmore
	alias reconnect "close; cache flush; cd ."
	alias edit "eval -f "get $0 -o ~/.lftp/edit.tmp.$$ && shell \\"cp -p ~/.lftp/edit.tmp.$$ ~/.lftp/edit.tmp.$$.orig && vim ~/.lftp/edit.tmp.$$ && test ~/.lftp/edit.tmp.$$ -nt ~/.lftp/edit.tmp.$$.orig\\" && put ~/.lftp/edit.tmp.$$ -o $0; shell rm -f ~/.lftp/edit.tmp.$$*"

#Linux #Ftp #Lftp #Download #Upload