执子之手

与子偕老


  • 首页

  • 分类

  • 归档

  • 标签

  • 关于

  • 搜索
close

Linux常用命令介绍 01 - curl

时间: 2020-03-13   |   分类: 运维     |   阅读: 1745 字 ~4分钟   |   访问: 0

很久没有更新博客了。之前一直用hexo,配置起来稍显复杂,单个的时间一长有些忘了,搞不起来了,于是荒废了很长时间。最近下定决心迁移到了Hugo上。总体感觉Hugo速度确实快,这一点非常喜欢。以后有时间可以介绍一下这次迁移的过程。

这几年一直不间断的与Linux打交道,但是Linux的命令一直没有深入研究过,所以碰到问题的时候还是要各种搜索,挺麻烦的。这次想整理一个常用命令的介绍系列,把平常用到的命令简单介绍一下。不会是详尽的帮助类介绍,更多的是基于场景的介绍:譬如要实现什么功能用什么参数之类的。作为以后的备查吧,就不用再到处搜索了。:)

第一个命令介绍curl。

1. 网站诊断

有时候碰到某个网站连接有问题,可以使用curl命令协助解决。例如可以用来排查证书是否正确、返回Header信息是否正确之类的。例如:

 1> curl -v -I https://www.baidu.com
 2*   Trying 180.101.49.12...
 3* TCP_NODELAY set
 4* Connected to www.baidu.com (180.101.49.12) port 443 (#0)
 5* ALPN, offering h2
 6* ALPN, offering http/1.1
 7* successfully set certificate verify locations:
 8*   CAfile: /etc/ssl/cert.pem
 9  CApath: none
10* TLSv1.2 (OUT), TLS handshake, Client hello (1):
11* TLSv1.2 (IN), TLS handshake, Server hello (2):
12* TLSv1.2 (IN), TLS handshake, Certificate (11):
13* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
14* TLSv1.2 (IN), TLS handshake, Server finished (14):
15* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
16* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
17* TLSv1.2 (OUT), TLS handshake, Finished (20):
18* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
19* TLSv1.2 (IN), TLS handshake, Finished (20):
20* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
21* ALPN, server accepted to use http/1.1
22* Server certificate:
23*  subject: C=CN; ST=beijing; L=beijing; OU=service operation department; O=Beijing Baidu Netcom Science Technology Co., Ltd; CN=baidu.com
24*  start date: May  9 01:22:02 2019 GMT
25*  expire date: Jun 25 05:31:02 2020 GMT
26*  subjectAltName: host "www.baidu.com" matched cert's "*.baidu.com"
27*  issuer: C=BE; O=GlobalSign nv-sa; CN=GlobalSign Organization Validation CA - SHA256 - G2
28*  SSL certificate verify ok.
29> HEAD / HTTP/1.1
30> Host: www.baidu.com
31> User-Agent: curl/7.64.1
32> Accept: */*
33>
34< HTTP/1.1 200 OK
35HTTP/1.1 200 OK
36< Accept-Ranges: bytes
37Accept-Ranges: bytes
38< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
39Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
40< Connection: keep-alive
41Connection: keep-alive
42< Content-Length: 277
43Content-Length: 277
44< Content-Type: text/html
45Content-Type: text/html
46< Date: Fri, 13 Mar 2020 08:51:05 GMT
47Date: Fri, 13 Mar 2020 08:51:05 GMT
48< Etag: "575e1f60-115"
49Etag: "575e1f60-115"
50< Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
51Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
52< Pragma: no-cache
53Pragma: no-cache
54< Server: bfe/1.0.8.18
55Server: bfe/1.0.8.18
56
57<
58* Connection #0 to host www.baidu.com left intact
59* Closing connection 0

其中的参数介绍如下:

  • -v, –verbose 显示详细的提示信息;
  • -I, –head 只获取请求头信息。在诊断链接的时候使用该参数忽略真正返回的内容比较方便排查原因。

2. 忽略证书

当访问一个自签名证书的网站的时候,可以忽略对证书的校验。例如:

 1# 该网站是自签名证书,直接访问无法访问
 2> curl https://192.168.2.241
 3curl: (60) SSL certificate problem: self signed certificate in certificate chain
 4More details here: https://curl.haxx.se/docs/sslcerts.html
 5
 6curl failed to verify the legitimacy of the server and therefore could not
 7establish a secure connection to it. To learn more about this situation and
 8how to fix it, please visit the web page mentioned above.
 9
10# 加上-k参数,跳过证书检测
11> curl -k https://192.168.2.241
12<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
13
14<html lang="en">
15<head>
16    <meta http-equiv="content-type" content="text/html; charset=utf8">
17    <meta http-equiv="refresh" content="0;URL='/ui'"/>
18</head>
19</html>

参数介绍:

  • -k, –insecure 跳过对服务器端证书的检测。

3. 设置代理

可以通过-x参数设置代理服务器。例如:

1-x socks5://127.0.0.1:12345
2-x http://127.0.0.1:12346

参数介绍:

  • -x, --proxy [protocol://]host[:port]
  • protocol可以为:socks4, socks4a, socks5, socks5h, http或者https。默认是http。

4. 发送json请求

可以通过curl直接模拟http访问发送json格式的请求。例如:

1curl -H "Content-Type: application/json" -X POST -d '{ "customerId": 1, "customerName": "青岛卓望信息技术有限公司", "channelId": 1, "channelName": "青岛卓望信息技术有限公司"}' https://taxer.fxl.eveus.com/api/admin/qrcode
  • -H 指定请求用的header。这里指定了Content-Type为application/json;
  • -X 指定请求类型。默认是GET。可以是POST、PUT、OPTION等;
  • -d 后面指定请求用的参数。

也可以从文件中读取消息内容,使用-d @filename方式。例如,我们有一个文件request.json,内容如下:

1{ "customerId": 1, "customerName": "青岛卓望信息技术有限公司", "channelId": 1, "channelName": "青岛卓望信息技术有限公司"}

然后使用命令请求如下:

1curl -H "Content-Type: application/json" -X POST -d @request.json https://taxer.fxl.eveus.com/api/admin/qrcode

5. 使用个人证书

如果网站使用了双向https认证,则会提供一个客户端证书给访问者。curl也支持使用个人证书进行授权认证。方法是使用参数:--cert和--key。根据证书内容的不同,--key有时可以不使用(如果证书中已经包含了私钥)。 curl不支持p12格式的证书,如果收到的是p12格式的证书(Java程序常用),则可以使用openssl转换成需要的pem格式。方法如下:

1# 如果p12文件是密钥保护的,则需要输入密码
2# 提取证书,包含私钥
3openssl pkcs12 -in client.p12 -out cert&key.pem
4# 只保留证书,不包含私钥
5openssl pkcs12 -in client.p12 -nokeys -out cert.pem 
6# 只提取私钥
7openssl pkcs12 -in client.p12 -nocerts -out key.pem -nodes

使用证书进行授权方法的命令如下:

1# 使用包含私钥的证书
2curl --cert cert&key.pem -H "Content-Type: application/json" -X POST -d '{ "customerId": 1, "customerName": "青岛卓望信息技术有限公司", "channelId": 1, "channelName": "青岛卓望信息技术有限公司"}' https://taxer.fxl.eveus.com/api/admin/qrcode
3# 使用分开的证书和私钥
4curl --cert cert.pem --key key.pem -H "Content-Type: application/json" -X POST -d '{ "customerId": 1, "customerName": "青岛卓望信息技术有限公司", "channelId": 1, "channelName": "青岛卓望信息技术有限公司"}' https://taxer.fxl.eveus.com/api/admin/qrcode
#Linux#
Mac上面的文件比较功能
iOS证书及推送相关概念
  • 文章目录
  • 站点概览
Orchidflower

Orchidflower

Do one thing at a time, and do well.

77 日志
6 分类
84 标签
GitHub 知乎 OSC 豆瓣
  • 1. 网站诊断
  • 2. 忽略证书
  • 3. 设置代理
  • 4. 发送json请求
  • 5. 使用个人证书
© 2009 - 2024 执子之手
Powered by - Hugo v0.113.0
Theme by - NexT
ICP - 鲁ICP备17006463号-1
0%