每天一命令之curl
跳转到导航
跳转到搜索
常用参数
-o: --output <file> Write to file instead of stdout 内置option:-o(小写)保存网页
将文件保存为命令行中指定的文件名的文件中
借助-o选项,提供名称,下载文件会以该名称保存;如使用-O选项,文件就会以原始名称保存。
-O:使用URL中默认的文件名保存文件到本地
-L, --location Follow redirects 进行强制重定向
--location-trusted Like --location, and send auth to other hosts
--login-options <options> Server login options
--mail-auth <address> Originator address of the original email
--mail-from <address> Mail from this address
--mail-rcpt <address> Mail to this address
下载2022 -L, --location Follow redirects 进行强制重定向 -s/--silent 静音模式。不输出任何东西 -O:使用URL中默认的文件名保存文件到本地
curl -L -s -O https://kuboard.cn/statics/learning/job/job-tmpl.yaml
举个栗子
curl -L linux.net/lnmp.sh -o 1.sh
把lnmp.sh 下载回来 并重命令为1.sh
指令:curl
在linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具。它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具。
语法:# curl [option] [url]
常见参数:
-A/--user-agent <string> 设置用户代理发送给服务器
-b/--cookie <name=string/file> cookie字符串或文件读取位置
-c/--cookie-jar <file> 操作结束后把cookie写入到这个文件中
-C/--continue-at <offset> 断点续转
-D/--dump-header <file> 把header信息写入到该文件中
-e/--referer 来源网址
-f/--fail 连接失败时不显示http错误
-o/--output 把输出写到该文件中
-O/--remote-name 把输出写到该文件中,保留远程文件的文件名
-r/--range <range> 检索来自HTTP/1.1或FTP服务器字节范围
-s/--silent 静音模式。不输出任何东西
-T/--upload-file <file> 上传文件
-u/--user <user[:password]> 设置服务器的用户和密码
-w/--write-out [format] 什么输出完成后
-x/--proxy <host[:port]> 在给定的端口上使用HTTP代理
-#/--progress-bar 用进度条显示当前的传送状态
用curl的内置option就好,存下http的结果,用这个option: -o
curl -o page.html http://www.yahoo.com
大写的O,这么用:这样,就可以按照服务器上的文件名,自动存在本地了
curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen1.JPG
访问不到?肯定是你的proxy没有设定了。
使用curl的时候,用这个option可以指定http访问所使用的proxy服务器及其端口: -x
curl -x 123.45.67.89:1080 -o page.html http://www.yahoo.com
下载
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
curl -fsSL -o php.tar.xz https://www.php.net/distributions/php-7.4.22.tar.xz
其它例子本身就是一个sh脚本 哈哈 写得不错
https://test.docker.com/
curl -fsSL https://test.docker.com/ | bash
curl -fsSL https://test.com/lnmp.sh | bash
-f/--fail 连接失败时不显示http错误
-s/--silent 静音模式。不输出任何东西
-S, --show-error Show error. With -s, make curl show errors when they occur
-L, --location Follow redirects (H)
or
wget http://xxx/xxx.sh -O- | bash
downloads
curl -R -O http://www.lua.org/ftp/lua-5.4.3.tar.gz curl -L -s -O https://kuboard.cn/statics/learning/job/job-tmpl.yaml
curl获取状态码Getting curl to output HTTP status code?
curl -I http://www.example.org
Getting curl to output HTTP status code?
指定ip访问url
指定ip访问url curl -H 'Host:a.com' http://10.12.20.21
每一秒 不停的 curl 一个特定页面
#!/bin/bash
cuoun=0
while true
do
me=`curl -N k0.frg.kaixin001.com.cn/curl.php`
#echo $me
if [ "$me" != "evan" ]
then
echo "err $cuoun"
fi
cuoun=`expr $cuoun + 1`
sleep 1
done
cat /data/mon/curl_website.sh
#!/bin/bash
# Usage */1 * * * * /bin/bash /data/mon/curl_website.sh
DATE_TIME=`date +"%Y-%m-%d %H:%M.%S"`
echo "${DATE_TIME}" >> /data/mon/webcurl.log
curl -s -o /dev/null -I -w "%{http_code}" https://www.cailu.net >> /data/mon/webcurl.log
echo -e '\n' >>/data/mon/webcurl.log
cat get.sh
#!/bin/bash
#author: linuxsa.org
DATE_TIME=`date +"%Y-%m-%d %H:%M.%S"`
echo "${DATE_TIME}" >> /home/evan/getes.log
zabbix_get -s prod-sns-node-ng-web02 -k"agent.ping" >>/home/evan/getes.log
#zabbix_get -s prod-sns-es01 -k"agent.ping" >>/home/evan/getes.log
trouble shooting
在容器里 curl 里有问题 最终加了-k 叫程序自己去搞
root@b92094bd49ed:/var/www# curl -H "Content-Type:application/json" -X POST https://market-api.cailuw.com/api/v1/coin/list
curl: (60) SSL certificate problem: certificate has expired
More details here: https://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
/var/www# curl -V
curl 7.52.1 (x86_64-pc-linux-gnu) libcurl/7.52.1 OpenSSL/1.0.2l zlib/1.2.8 libidn2/0.16 libpsl/0.17.0 (+libidn2/0.16) libssh2/1.7.0 nghttp2/1.18.1 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL
root@b92094bd49ed:/var/www# exit
[root@prod-sns-php02 ~]# curl -V
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.36 zlib/1.2.7 libidn/1.28 libssh2/1.4.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz unix-sockets
-k, --insecure Allow insecure server connections when using SSL
加-k
/etc/pki 加了也不行呀
volumes:
- ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}
- ${APP_PACKAGE_PATH}:${APP_PACKAGE_PATH_CONTAINER}
- ./php-worker/supervisord.d:/etc/supervisor/conf.d
- /etc/pki/:/etc/pki/
date -s "2020-10-14 15:35:01"
sudo ln -sf /usr/share/zoneinfo/America/Monterrey /etc/localtime
时间同步
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
1,先修改host使域名解析到正确的IP(至于为什么会解析到一个错误的IP我还没搞清楚,可能是之前的缓存)
2,然后根据https://curl.haxx.se/docs/sslcerts.html,下载证书openssl s_client -showcerts -servername server -connect server:443 > cacert.pem
之前从错误的服务器(IP)上下载的证书所以不行,指定了正确的IP之后就正常了
ntpdate pool.ntp.org
docker容器内访问https问题,curl: (60) SSL certificate problem: self signed certificate
see also
https://www.cnblogs.com/gbyukg/p/3326825.html
http://www.21andy.com/new/20080602/1154.html
http://doiido.blog.51cto.com/5503054/1564631
https://www-31.ibm.com/support/techdocs/cn/faqhtmlfaq/2311073I23001.htm