您的位置:首页 > 运维架构 > Shell

shell脚本中常用的命令:wget、curl、ss、lsof、nmap、nc、netstat、telnet

2017-07-13 22:54 781 查看
shell脚本中常用的命令:wget、curl、ss、lsof、nmap、nc、netstat、telnet
实验环境说明:
(1)远程nginx服务器IP:192.169.5.136,nginx服务使用的端口是80;
(2)本地ceshiji的IP: 192.169.5.121

在服务器本地监控服务端口常见命令:netstat、ss、lsof(简称三‘S’)
举例说明常用命令的选项:
(1)[root@nginx ~]# netstat -lnp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1292/nginx
(2)[root@nginx ~]# ss -lnp|grep nginx
LISTEN 0 128 *:80 *:*
-t (tcp)仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化成数字。
-l 仅列出有在 Listen (监听) 的服务状态
-p 显示建立相关链接的程序名
(3)[root@nginx ~]# lsof -i :80|grep nginx
nginx 1292 root 6u IPv4 8868 0t0 TCP *:http (LISTEN)
nginx 1293 www 6u IPv4 8868 0t0 TCP *:http (LISTEN)
nginx 1294 www 6u IPv4 8868 0t0 TCP *:http (LISTEN)
说明:lsof -i :80 知道80端口被哪个进程占用

从远程监控服务器本地端口的命令:telnet、nmap、nc(简称三‘n’)
说明:主要用来验证远端端口是否开启
(1)[root@ceshiji ~]# nc -v -z -w2 192.169.5.136 80

Connection to 192.169.5.136 80 port [tcp/http] succeeded!
[root@ceshiji ~]# echo $?
0
-v 显示指令执行过程。
-w<超时秒数> 设置等待连线的时间。
-z 使用0输入/输出模式,只在扫描通信端口时使用。所以,上面也可以写成如下:
[root@ceshiji ~]# nc -v -w2 192.169.5.136 80
Connection to 192.169.5.136 80 port [tcp/http] succeeded!
[root@ceshiji ~]# echo $?
0
(2)[root@ceshiji ~]# nmap 192.169.5.136 -p 80



(3)[root@ceshiji ~]# telnet 192.169.5.136 80|grep Connected



shell脚本中运用举例:
[root@ceshiji ~]# echo -e "\n"|telnet 192.169.5.136 80 2>/dev/null|grep Connected |wc -l
1
服务器常用测试命令:wget、curl
(1)[root@ceshiji ~]# wget -T 5 baidu.com



说明:-T 表示超时时间,也可以用--timeout表示。
[root@ceshiji ~]# wget --timeout=5 baidu.com
(2)[root@ceshiji ~]# wget -q -T 5 baidu.com
[root@ceshiji ~]# echo $?
0
说明:-q 表示安静模式(无信息输出),相当于:
[root@ceshiji ~]# wget -T 5 baidu.com &>/dev/null
[root@ceshiji ~]# echo $?
0
(3)[root@ceshiji ~]# wget -T 5 --spider baidu.com



说明:--spider 表示只检测不下载信息。
(4)[root@ceshiji ~]# wget -O /root/test1 http://baidu.com
[root@ceshiji ~]# cat test1
<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>
说明:
-O(大写字母)下载并以不同的文件名保存
-P(大写字母)后面跟的是将http内容下载到的路径,也就是说使用-O可以自定义文件下载路径。
(5)[root@ceshiji ~]# curl baidu.com ##获取指定网页



(6)[root@ceshiji ~]# curl -I baidu.com



说明:-I/--head 仅返回头部信息,使用HEAD请求
(7)[root@ceshiji ~]# curl -s -I baidu.com|wc -l
12
[root@ceshiji ~]# curl -I baidu.com|wc -l



说明:-s/--silent 表示安静模式
(8)[root@ceshiji ~]# curl -s -o /dev/null baidu.com
[root@nginx ~]# curl -s -o /root/shiyan baidu.com
[root@nginx ~]# cat shiyan
<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>
说明:-o/--output <file> 表示将baidu.com下载内容输出到/root/shiyan文件中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  脚本 shell 常用