Linux常用命令(二)


scp命令

# 获取远程服务器上的文件
$ scp -P 2222 root@192.168.1.100:/root/tomcat.tar.gz /home/tomcat.tar.gz

# -P是端口号参数,2222表示更改SSH端口后的端口,如果没有更改SSH端口可以不用添加该参数 
# root@192.168.1.100 表示使用root用户登录远程服务器192.168.1.100
# :/root/tomcat.tar.gz 表示远程服务器上的文件
# /home/tomcat.tar.gz 表示保存在本地上的路径和文件名

# 获取远程服务器上的目录
$ scp -P 2222 -r root@192.168.1.100:/root/tomcat/ /home/ben/dev/

# -P是端口号参数,2222表示更改SSH端口后的端口,如果没有更改SSH端口可以不用添加该参数
# -r参数表示递归复制(即复制该目录下面的文件和目录)
# root@192.168.1.100 表示使用root用户登录远程服务器192.168.1.100
# :/root/tomcat/ 表示远程服务器上的目录
# /home/ben/dev/ 表示保存在本地上的路径。

# 将本地文件上传到服务器上
$ scp -P 2222 /home/tomcat.tar.gz root@192.168.1.100:/root/tomcat.tar.gz

# -P是端口号参数,2222表示更改SSH端口后的端口,如果没有更改SSH端口可以不用添加该参数。 
# /home/tomcat.tar.gz 表示本地上准备上传文件的路径和文件名
# root@192.168.1.100 表示使用root用户登录远程服务器

# 将本地目录上传到服务器上
$ scp -P 2222 -r /home/tomcat/ root@192.168.1.100:/root/dev/tomcat/

# -P是端口号参数,2222表示更改SSH端口后的端口,如果没有更改SSH端口可以不用添加该参数。
# -r 参数表示递归复制(即复制该目录下面的文件和目录)
# /home/tomcat/表示准备要上传的目录
# root@192.168.1.100 表示使用root用户登录远程服务器192.168.1.100
# :/root/dev/tomcat/ 表示保存在远程服务器上的目录位置。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

nc命令

# 今天学到了另一种远程传输文件的方式,原来只知道scp,rz/sz
# 远程服务器(目的主机监听)
# 命令格式:nc -l 监听端口<未使用端口> > 要接收的文件名
$ nc -l 9999 > elasticsearch-jdbc-1.7.3.0-dist.zip
# 解释:远程服务器开启一个9999的端口进行监听,并且等待接收elasticsearch-jdbc-1.7.3.0-dist.zip文件

# 本地机器(源主机发起请求)
# 命令格式:nc 目的主机ip 目的端口 < 要发送的文件
$ nc 10.120.10.105 9999 < /Users/ben/Downloads/letv/ES/elasticsearch-jdbc-1.7.3.0-dist.zip
# 解释:本地机器指定远程服务器的ip地址和port端口号将指定的本地文件传送到远程服务器
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

rz/sz命令

Zmodem(rz/sz命令)一般需要和SSH客户端一起配合使用

下面介绍两种客户端

  1. iterm2(适用Mac环境)
  2. secureCRT(适用Windows环境)
iterm2
1. 安装brew工具
$ brew install lszrz
# 如果没有brew工具,请参考:http://blog.csdn.net/tsxw24/article/details/15500517
2. 下载iterm2-zmodem到/usr/local/bin目录(将两个shell脚本放到/usr/local/bin/目录下然后开始配置)
$ sudo wget https://raw.github.com/mmastrac/iterm2-zmodem/master/iterm2-send-zmodem.sh 
$ sudo wget https://raw.github.com/mmastrac/iterm2-zmodem/master/iterm2-recv-zmodem.sh 
$ sudo chmod 777 /usr/local/bin/iterm2-*
3. 运行下载的iterm,添加trigger
打开iterm2 -->  同时按command和,键 --> Profiles --> Default --> Advanced --> Triggers的Edit按钮
4. Triggers的设置代码如下 
Regular expression: \*\*B0100
Action: Run Silent Coprocess 
Parameters: /usr/local/bin/iterm2-send-zmodem.sh

Regular expression: \*\*B00000000000000
Action: Run Silent Coprocess 
Parameters: /usr/local/bin/iterm2-recv-zmodem.sh
5. 上传和下载文件和secureCRT方式一样
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

iterm2

secureCRT
# 上传文件
1. 打开secureCRT,通过SSH连到至远程linux主机
2. 键入rz命令
3. 在跳出的窗口选择想要上传的文件
4. 点击ADD后加入传输列表
5. 点击确认以后传送文件
6. 查看状态,一般都是发送正功
7. 查看目录,确定刚才选的文件已经上传成功

# 下载文件
1. 远程连接到linux主机
2. 输入sz fileName
3. 在弹出的窗口中选择保存文件到本地的目录
4. 确定后,等待文件下载完成
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

curl命令

以下的案例都使用的百度开源API进行的测试,具体地址如下: 
https://www.juhe.cn/docs/api/id/39

名称类型必填说明
citynamestringY城市名或城市ID,如:”苏州”,需要utf8 urlencode
dtypestringN返回数据格式:json或xml,默认json
formatintN未来6天预报(future)两种返回格式,1或2,默认1
keystringY你申请的key
名称类型必填说明
keystring应用APPKEY
typestring类型:top(头条,默认),shehui(社会),guonei(国内),guoji(国际),yule(娱乐),tiyu(体育)junshi(军事),keji(科技),caijing(财经),shishang(时尚)
curl -d 传递请求数据
# 参数:
# -G:指定GET方式发送请求
# --data/-d:指定使用POST方式传递数据
  • 1
  • 2
  • 3
curl 发送GET请求
# 方式一:curl 以?方式传递参数发送GET请求
# 默认curl使用POST方式请求数据,这种方式下直接通过URL传递数据,也就是以?方式添加到url后面
# 格式:curl "https://hostname.com:8080/getMethod?param1=value1&param2=value2"

# 方式二:curl -G -d 发送GET请求
# 但是如果有-d参数,也可以加上-G参数指定使用GET方式请求数据,如果不加上-G则默认使用的POST方式请求数据
# 格式:curl -G -d "param1=value1&param2=value2" "http://hostname.com:8080/getMethod"

# 使用?方式传递参数,不使用-d方式
$ curl "http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=您申请的KEY"

# 也可以使用-d方式传递参数,但是必须指定-G来指定GET方式请求数据,因为该接口只支持GET方式请求,如果不加-G参数,使用-d参数就默认为POST方式请求
$ curl -G -d "format=2&cityname=%E8%8B%8F%E5%B7%9E&key=您申请的KEY" "http://v.juhe.cn/weather/index"

# 以下的方式就会报错,因为该接口只支持GET方式请求,不支持POST方式请求,如果不加-G参数,使用-d参数就默认为POST方式请求
$ curl -d "format=2&cityname=%E8%8B%8F%E5%B7%9E&key=您申请的KEY" "http://v.juhe.cn/weather/index"
# 返回的结果是error_code:203901,获取不到cityname参数
{"resultcode":"201","reason":"Error Cityname!","result":null,"error_code":203901}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
curl 发送POST请求
# 格式:curl -d/--data "param1=value1&param2=value2" "http://hostname.com:8080/html/postMethod"
$ curl -d "type=tiyu&key=申请的KEY" "http://v.juhe.cn/toutiao/index"
$ curl --data "type=tiyu&key=申请的KEY" "http://v.juhe.cn/toutiao/index"

# POST提交多个同名参数,paramList是一个数组参数
$ curl -XPOST 'http://hostname.com/postMethod' -d 'userName=birdben&paramList=["aa", "bb"]'

# form表单上传文件
# 可以通过 --form/-F 方式指定使用POST方式传递数据
# --form/-F 参数相当于设置form表单的method="POST"和enctype="multipart/form-data"两个属性
# filename为文本框中的name元素对应的属性值。<type="text" name="filename">
$ curl --form "filename=@filename.txt" "http://hostname.com:8080/html/uploadFile"
$ curl -F "filename=@filename.txt;type=text/plain"  "http://hostname.com:8080/html/uploadFile"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
curl 特殊字符处理
# 默认情况下,通过GET/POST方式传递过去的数据中若有特殊字符(或者中文字符),首先需要将特殊字符转义在传递给服务器端,如"value 1"值中包含有空格,则需要先将空格转换成%20,如:
$ curl -d "value%201" "http://hostname.com"

# 在新版本的curl中,提供了新的选项 --data-urlencode,通过该选项提供的参数会自动转义特殊字符。
$ curl --data-urlencode "value 1" "http://hostname.com"
  • 1
  • 2
  • 3
  • 4
  • 5
curl 指定其它协议
# 除了使用GET和POST协议外,还可以通过 -X 选项指定其它协议,一般用过ES删除索引的时候用的比较多,如:
$ curl -X DELETE "https://hostname.com"
  • 1
  • 2
curl 设置http请求头信息
# 以下操作可以挂代理抓包查看并且验证
# -v 通过使用 -v 和 -trace获取更多的链接信息
# 访问https://www.github.com/下面返回的信息会提示该域名已经被重定向到https://github.com/
$ curl -v "https://www.github.com/"

* Rebuilt URL to: https://www.github.com/
* Hostname was NOT found in DNS cache
*   Trying 192.30.252.130...
* Connected to www.github.com (192.30.252.130) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
* Server certificate: github.com
* Server certificate: DigiCert SHA2 Extended Validation Server CA
* Server certificate: DigiCert High Assurance EV Root CA
> GET / HTTP/1.1
> User-Agent: curl/7.37.1
> Host: www.github.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Content-length: 0
< Location: https://github.com/
< Connection: close
<
* Closing connection 0

# -A 设置http请求头User-Agent,可以指定自己这次访问所模拟的浏览器信息
# 前一个直接用curl命令的,User-Agent: curl/7.37.1
> User-Agent: curl/7.37.1
# 使用-A参数重新指定浏览器信息后,User-Agent: Mozilla/5.0 Firefox/21.0
> User-Agent: Mozilla/5.0 Firefox/21.0
$ curl -v -A "Mozilla/5.0 Firefox/21.0" "https://www.github.com/"

 Hostname was NOT found in DNS cache
*   Trying 192.30.252.120...
* Connected to www.github.com (192.30.252.120) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
* Server certificate: github.com
* Server certificate: DigiCert SHA2 Extended Validation Server CA
* Server certificate: DigiCert High Assurance EV Root CA
> GET / HTTP/1.1
> User-Agent: Mozilla/5.0 Firefox/21.0
> Host: www.github.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Content-length: 0
< Location: https://github.com/
< Connection: close
<
* Closing connection 0

# -e 设置http请求头Referer,解释:检查http访问的Referer是服务器端常用的限制方法。比如你先访问网站的首页,再访问里面所指定的下载页,这第二次访问的referer地址就是第一次访问成功后的页面地址。这样,服务器端只要发现对下载页面某次访问的referer地址不是首页的地址,就可以断定那是个盗链了。所以这里我们可以自己修改我们的Referer链接地址。
$ curl -v -e "http://birdben.com/" "http://birdben.com/download.html"

* Hostname was NOT found in DNS cache
*   Trying 50.63.202.46...
* Connected to birdben.com (50.63.202.46) port 80 (#0)
> GET /download.html HTTP/1.1
> User-Agent: curl/7.37.1
> Host: birdben.com
> Accept: */*
> Referer: http://birdben.com/
>
< HTTP/1.1 302 Found
< Connection: close
< Pragma: no-cache
< cache-control: no-cache
< Location: /download.html
<
* Closing connection 0

# --header/-H 设置header
$ curl -H "Connection:keep-alive" -H "Host:127.0.0.1" -H "Accept-Language:es" -H "Cookie:ID=1234" "https://www.github.com/"

* Hostname was NOT found in DNS cache
*   Trying 192.30.252.129...
* Connected to www.github.com (192.30.252.129) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
* Server certificate: github.com
* Server certificate: DigiCert SHA2 Extended Validation Server CA
* Server certificate: DigiCert High Assurance EV Root CA
> GET / HTTP/1.1
> User-Agent: curl/7.37.1
> Accept: */*
> Connection:keep-alive
> Host:127.0.0.1
> Accept-Language:es
> Cookie:ID=1234
>
< HTTP/1.1 301 Moved Permanently
< Content-length: 0
< Location: https://github.com/
< Connection: close
<
* Closing connection 0

# -I 设置http响应头处理,仅仅返回header
$ curl -I "https://github.com"

# --dump-header/-D 将http header保存到/tmp/header文件
$ curl -D /tmp/header "https://github.com"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
curl -o/-O 下载
# 通过-o/-O选项保存下载的文件到指定的文件中:
-o:将文件保存为命令行中指定的文件名的文件中
-O:使用URL中默认的文件名保存文件到本地

# 将文件下载到本地并命名为test.html
$ curl -o test.html "http://apistore.baidu.com/apiworks/servicedetail/478.html"

# 将文件保存到本地并命名为478.html
$ curl -O "http://apistore.baidu.com/apiworks/servicedetail/478.html"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
curl -L 重定向
# 默认情况下CURL不会发送HTTP Location headers(重定向).当一个被请求页面移动到另一个站点时,会发送一个HTTP Loaction header作为请求,然后将请求重定向到新的地址上。

# 例如:浏览器访问京东以前的域名www.360buy.com,会自动将地址重定向到www.jd.com上。通过curl命令就会出现如下信息
$ curl "www.360buy.com"
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<h1>301 Moved Permanently</h1>
<p>The requested resource has been assigned a new permanent URI.</p>
<hr/>Server: JDWS</body>
</html>

# 这时可以通过使用-L选项进行强制重定向,让curl使用地址重定向,此时就会返回京东首页的信息
$ curl -L "www.360buy.com"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
curl -O 从FTP服务器下载文件
# curl同样支持FTP下载,若在url中指定的是某个文件路径而非具体的某个要下载的文件名, curl则会列出该目录下的所有文件名而并非下载该目录下的所有文件

# 列出public_html下的所有文件夹和文件
$ curl -u user:pass -O "ftp://ftp_server/html/"
# 下载index.css文件
$ curl -u user:pass -O "ftp://ftp_server/css/index.css"

# 从FTP服务器下载文件的另一种写法
$ curl "ftp://user:pass@ftp_server:port/path/file"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
curl -T 上传文件到FTP服务器
# 通过 -T 选项可将指定的本地文件上传到FTP服务器上
# 将myfile.txt文件上传到服务器
$ curl -u user:pass -T myfile.txt "ftp://ftp.testserver.com:port/path/"

# 同时上传多个文件
$ curl -u user:pass -T "{file1,file2}" "ftp://ftp.testserver.com"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
curl -u 授权(没有测试过)
# 在访问需要授权的页面时,可通过-u选项提供用户名和密码进行授权
$ curl -u username:password URL

# 通常的做法是在命令行只输入用户名,之后会提示输入密码,这样可以保证在查看历史记录时不会将密码泄露
$ curl -u username URL
  • 1
  • 2
  • 3
  • 4
  • 5
curl -x 设置代理
# -x 选项可以为CURL添加代理功能
# 指定代理主机和端口
$ curl -x proxysever.com:8888 -U user:pass "http://google.co.jp"
  • 1
  • 2
  • 3

参考文章:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值