苏苏的博客

简约至极

Linux下常用的下载利器

wget

wget -c -O filename "http://xx.com" 下载存储到指定文件,并指定断点续传

wget -nc -c -i url.txt 批量下载文件中指定的资源(每行一个连接),跳过已下载的(同名的),并且断点续传.

wget -i url.txt -O - | mpv - 下载url.txt里的链接,输出到标准输出

使用user-agent

wget --user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36" URL-TO-DOWNLOAD

wget 断点续传 使用 -c 限速使用--limit-rate=300k

wget -c --limit-rate=30k http://...

lftp

wget 可以断点续传 但是不支持多线程下载, lftp 的一个用法可以实现多线程下载.

lftp -c "pget -n 10 http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.18.tar.gz"

curl

Windows 编译好的下载

https://curl.haxx.se/dlwiz/?type=bin&os=Win64&flav=-

常用的命令

curl http://xx.com 只获取http消息正文

curl -I http://xx.com 只获取http消息头

curl -i http://xx.com 获取http消息头和正文

curl -d "name=abc&pass=123" http://xx.com 发送http post请求,采用的是application/x-www-form-urlencoded形式

curl -F "name=abc" -F "pass=123" http://xx.com 发送http post请求,采用的是multipart/form-data 形式,此两种方式都可以由 $_POST 接收

curl -F "name=abc" -F "file=@/tmp/1.jpg" http://xx.com 发送 http post请求, multipart/form-data 形式还可以上传文件,只需改为@加路径

以http body 形式上传

curl -X POST -T todo.md media.suconghou.cn/upload.php -v

curl -A 设置user-agent

设置代理

curl myip.ipip.net -v -x socks5://127.0.0.1:5566

curl myip.ipip.net -v -x socks5h://127.0.0.1:5566 use dns through socks proxy

curl myip.ipip.net -v -x http://127.0.0.1:9088 http 代理

curl 不仅能用来下载,还是网络调试的利器,Chrome的网络请求也可以直接另存为curl请求格式

批量下载规则的URL资源

curl -O "http://www.example.com/[0001-9999].jpg"

在shell中可以这样用,实现同样功能

for i in {10001..19999}; do
wget www.example.com/${i:1}.jpg;
done

for i in {0001..9999}; do
echo www.example.com/${i}.jpg;
done

一个 range 请求

curl -o /tmp/1 -H "Range: bytes=0-10" http://share.xx.cn/temp/ss -v

这样服务器会返回 206 包含 Content-Length: 11 共11个字节 , 即是包含第11字节的

range 后面的值超过content-length ,响应也会正常响应,返回的Content-Range会纠正这个错误,给出正确的range

range 的前值如果超过content-length, 服务器就会给出 416 Requested Range Not Satisfiable

假如文件的大小为1024,则Range: bytes=0-1023 就返回了整个文件大小,Range: bytes=0-1024也能返回同样的数据,因为服务器会纠正range的后值.

start-end 表示不包含start(没有0位这个字节),从start开始一直到end+1 这些个字节.

响应的大小应该是 end-start+1

相继请求 Range:bytes=0-10 Range:bytes=10-20 一共是22字节,其中第11字节是重复的.

应为 Range:bytes=0-9 Range:bytes=10-19 一共是20字节 相当于 Range:bytes=0-19

下载一个压缩包,不存储直接解压

curl -sSL http://www.memcached.org/files/memcached-1.4.30.tar.gz | tar xz

其中-L表示更随重定向-s为安静模式,不输出任何信息,-S为在安静模式下出错的话要输出错误信息.

要解压到指定目录,后面的可以使用 tar xzC ./path/to/dir ,当然这个路径要存在

用curl下载的好处是不会像wget那样出现多个已下载的重复文件

| tar xJ 后面同压缩一样指定不同的压缩格式,xJ 解压 xz 格式

Axel

下载速度最快,分段下载速度优异 axel -n 30 -a -o localfilename http://xx.com/downloadfile 30个线程分段下载,存储为localfilename 参数-a更新进度条而不是每次都输出 参数-H 添加header,参数-U设定user-agent Downloads patch from 4 ftp mirrors axel ftp://ftp.{us,uk,br,fr}.kernel.org/pub/linux/kernel/v2.6/patch-2.6.39.gz

axel http://itmp.suconghou.cn hypriot-rpi-20160306-192317.img.zip http://wtmp.suconghou.cn hypriot-rpi-20160306-192317.img.zip http://btmp.suconghou.cn hypriot-rpi-20160306-192317.img.zip

Aria2c

官网 https://aria2.github.io/

aria2c支持多种协议,URI MAGNET TORRENT_FILE METALINK_FILE,也支持axel类似的分段下载

https://github.com/aria2/aria2/releases 发布 Windows 和 android 及 osx 系统编译好的, android里为一个可执行文件,

但没有Linux版编译好的. 有第三方提供的静态编译好的 https://github.com/q3aql/aria2-static-builds 包含 Linux,Windows,arm树莓派版

aria2c -s 30 -c -o localfilename http://xx.com/downloadfile 30个线程分段下载,存储为localfilename,并断点续传

同时下载多文件 aria2c file1.torrent file2.torrent

从磁力链接中下载种子文件

aria2c --bt-save-metadata "magnet:?xt=urn:btih:xxxxxx"

自动调节连接数(如果每个种子的下载速度都低于 200K 的话, aria2 会临时增加连接数来试着提高下载速度。)

aria2c --bt-request-peer-speed-limit=200K file.torrent

打印一个种子文件中的内容

aria2c -S file.torrent

选择性下载文件

aria2c --select-file=1-4,8 -T file.torrent

bt下载慢? 使用--bt-tracker参数指定tracker服务器.

aria2c --bt-exclude-tracker="*" --bt-tracker="http://tracker1/announce,http://tracker2/announce" file.torrent

例子指示 aria2 移除 file.torrent 文件中所有的 tracker announce URIs ,并用"http://tracker1/announce” 和 “http://tracker2/announce” 代替

关于tracker源可以查看下面两个

https://newtrackon.com/api

https://github.com/ngosang/trackerslist

--file-allocation=none 设定不提前分配文件空间.

包含各项设置的示例

aria2c --seed-time=1 --seed-ratio=0.1 --bt-request-peer-speed-limit=20K --file-allocation=none --select-file=30,34  -T xx.torrent --bt-exclude-tracker="*" --bt-tracker="http://tracker1/announce,http://tracker2/announce"

参考 http://blog.csdn.net/hjwang1/article/details/44408633

On CentOS 7.2.1511, I was able to install it through the EPEL repository:

yum install epel-release -y
yum install aria2 -y

CentOS 6 安装可参考 http://chuansong.me/n/341279751838

pkgs.repoforge.org已不可用, 可参考 https://github.com/repoforge/rpms/issues/375

http://repository.it4i.cz/mirrors/repoforge/

http://mirror-status.repoforge.org/

CentOS6 x86_64

rpm -Uvh http://repository.it4i.cz/mirrors/repoforge/redhat/el6/en/x86_64/rpmforge/RPMS/$(curl -s http://repository.it4i.cz/mirrors/repoforge/redhat/el6/en/x86_64/rpmforge/RPMS/ | grep rpmforge-release | grep x86_64 | grep el6 | sort | tail -n1 | sed 's%.*>\(rpmforge\-release\-.*.rpm\)<.*%\1%')
yum -y install aria2

就可以安装成功,只不过CentOS6源里的aria2版本较旧且不再更新了.

aria2 作为服务运行.

aria2c --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all

后台服务 aria2c --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all > /var/log/aria2.log 2>&1 &

aria2c --conf-path="/etc/aria2/aria2.conf"

/etc/aria2/aria2.conf

## '#'开头为注释内容, 选项都有相应的注释说明, 根据需要修改 ##
## 被注释的选项填写的是默认值, 建议在需要修改时再取消注释  ##

## 文件保存相关 ##

# 文件的保存路径(可使用绝对路径或相对路径), 默认: 当前启动位置
dir=/data/www/share
# 启用磁盘缓存, 0为禁用缓存, 需1.16以上版本, 默认:16M
#disk-cache=32M
# 文件预分配方式, 能有效降低磁盘碎片, 默认:prealloc
# 预分配所需时间: none < falloc ? trunc < prealloc
# falloc和trunc则需要文件系统和内核支持
# NTFS建议使用falloc, EXT3/4建议trunc, MAC 下需要注释此项
file-allocation=none
# 断点续传
continue=true

## 下载连接相关 ##

# 最大同时下载任务数, 运行时可修改, 默认:5
max-concurrent-downloads=1
# 同一服务器连接数, 添加时可指定, 默认:1
max-connection-per-server=5
# 最小文件分片大小, 添加时可指定, 取值范围1M -1024M, 默认:20M
# 假定size=10M, 文件为20MiB 则使用两个来源下载; 文件为15MiB 则使用一个来源下载
min-split-size=10M
# 单个任务最大线程数, 添加时可指定, 默认:5
split=5
# 整体下载速度限制, 运行时可修改, 默认:0
#max-overall-download-limit=0
# 单个任务下载速度限制, 默认:0
#max-download-limit=0
# 整体上传速度限制, 运行时可修改, 默认:0
#max-overall-upload-limit=0
# 单个任务上传速度限制, 默认:0
#max-upload-limit=0
# 禁用IPv6, 默认:false
disable-ipv6=true

## 进度保存相关 ##

# 从会话文件中读取下载任务
input-file=/etc/aria2/aria2.session
# 在Aria2退出时保存`错误/未完成`的下载任务到会话文件
save-session=/etc/aria2/aria2.session
# 定时保存会话, 0为退出时才保存, 需1.16.1以上版本, 默认:0
#save-session-interval=60

## RPC相关设置 ##

# 启用RPC, 默认:false
enable-rpc=true
# 允许所有来源, 默认:false
rpc-allow-origin-all=true
# 允许非外部访问, 默认:false
rpc-listen-all=true
# 事件轮询方式, 取值:[epoll, kqueue, port, poll, select], 不同系统默认值不同
#event-poll=select
# RPC监听端口, 端口被占用时可以修改, 默认:6800
rpc-listen-port=6800
# 设置的RPC授权令牌, v1.18.4新增功能, 取代 --rpc-user 和 --rpc-passwd 选项
#rpc-secret=<TOKEN>
# 设置的RPC访问用户名, 此选项新版已废弃, 建议改用 --rpc-secret 选项
#rpc-user=ruyo
# 设置的RPC访问密码, 此选项新版已废弃, 建议改用 --rpc-secret 选项
#rpc-passwd=RUYO.net

## BT/PT下载相关 ##

# 当下载的是一个种子(以.torrent结尾)时, 自动开始BT任务, 默认:true
#follow-torrent=true
# BT监听端口, 当端口被屏蔽时使用, 默认:6881-6999
listen-port=51413
# 单个种子最大连接数, 默认:55
#bt-max-peers=55
# 打开DHT功能, PT需要禁用, 默认:true
enable-dht=true
# 打开IPv6 DHT功能, PT需要禁用
#enable-dht6=false
# DHT网络监听端口, 默认:6881-6999
#dht-listen-port=6881-6999
# 本地节点查找, PT需要禁用, 默认:false
#bt-enable-lpd=true
# 种子交换, PT需要禁用, 默认:true
enable-peer-exchange=true
# 每个种子限速, 对少种的PT很有用, 默认:50K
#bt-request-peer-speed-limit=50K
# 客户端伪装, PT需要
peer-id-prefix=-TR2770-
user-agent=Transmission/2.77
# 当种子的分享率达到这个数时, 自动停止做种, 0为一直做种, 默认:1.0
#seed-ratio=0
# 强制保存会话, 即使任务已经完成, 默认:false
# 较新的版本开启后会在任务完成后依然保留.aria2文件
#force-save=false
# BT校验相关, 默认:true
#bt-hash-check-seed=true
# 继续之前的BT任务时, 无需再次校验, 默认:false
bt-seed-unverified=true
# 保存磁力链接元数据为种子文件(.torrent文件), 默认:false
bt-save-metadata=true
content-disposition-default-utf8=true

不错的项目提供WebUI

https://github.com/ziahamza/webui-aria2

https://github.com/mayswind/AriaNg