ifconfig:查看当前系统的网卡和IP地址信息的常用命令。
ifconfig -s:显示网卡信息的精简列表
ifconfig 网卡名 up:激活某个网卡
ifconfig 网卡名 down:禁用某个网卡
ifconfig 网卡名 add IP地址:将指定的IP地址绑定到某个网卡
ifconfig 网卡名 del IP地址:从某个网卡上解除绑定指定的IP地址
ping ip地址/域名:用于侦测本机到目标主机的网络是否畅通。一直发检测包
ping -t 发送次数 ip地址/域名:用于侦测本机到目标主机的网络是否畅通。发指定数量检测包后停止发送
telnet:明文数据通信,用于检测指定IP地址和端口号的监听服务是否存在。如果不指定端口号,则telnet命令会使用默认的23号端口。
使用telnet不仅能连接某个服务器,还能与服务器交互。
如:
telnet www.baidu.com 80
hello //发送hello到www.baidu.com:80,不过因为它是非法的HTTP请求,所以被服务器关闭了连接。
Windows和Linux均提供了netstat命令,该命令经常用于查看网络连接状态。
netstat [选项与命令]
选项与命令:
-a: 表示显示所有选项,不使用该选项时,netstat默认不显示LISTEN相关选项。
-t: 表示仅显示tcp相关选项。
-u: 表示仅显示udp相关选项。
-n: 不显示别名,将能显示数字的全部转换为数字。
-l: 仅列出处于监听(listen)状态的服务。
-p: 显示建立相关链接的程序名。
-r: 显示路由信息、路由表。
-e: 显示扩展信息,例如uid等。
-s: 按各个协议进行统计。
-c: 每隔一个固定的时间执行该netstat命令。
如:
ronghui@ubuntu:~$ netstat -tun
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.184.130:22 192.168.184.1:49591 ESTABLISHED
tcp 0 0 192.168.184.130:445 192.168.184.1:49172 ESTABLISHED
tcp 0 0 192.168.184.130:22 192.168.184.1:63039 ESTABLISHED
lsof(list opened filedesciptor,列出已经打开的文件描述符)。
在Linux上,所有与资源句柄相关的内容都可以被统一抽象为fd(filedescriptor,文件描述符)。一个文件句柄就是一个fd,一个socket对象也可以叫作fd等。
使用lsof命令时需要注意以下三点:
lsof -i | grep myapp
。或者使用lsof -p pid
也能过滤指定的进程打开的fd信息。lsof +c 15
,即在最左侧的程序名最多显示15个字符。使用lsof命令恢复文件时需要注意:如果想成功恢复文件,则使用文件的进程必须处于存活状态,如果fileserver已经退出或被杀死,就无法用这种方式恢复;用这种方式恢复文件后,再次用lsof命令查看时,这个文件仍然时deleted状态。
nc即netcat命令。加密数据通信。
nc命令常用于模拟一个服务器程序被其他客户端连接,或者模拟一个客户端连接其他服务器,连接后就可以进行数据收发。nc命令默认使用TCP,加上-u后会使用UDP。
我们使用-l选项(单词listen的首字母)在某个IP地址和端口上开启一个监听服务,以便其他客户端连接。通常为了显示更详细的信息,会带上-v选项。
示例如下:
nc -v -l 127.0.0.1 6000
以上命令启动了一个监听服务器,监听IP地址为127.0.0.1,监听的端口为6000,如果机器可以被外网访问,则可以使用0.0.0.0这样的监听地址(也可以使用其他公网IP作为监听地址)。
使用nc命令模拟一个客户端程序时,我们不需要使用-l选项,直接将其写上IP地址(或域名,nc命令可以自动解析域名)和端口即可,示例如下:
nc -v www.baidu.com 80
我们知道,客户端连接服务器时一般都是操作系统随机分配一个可用的端口号连接到服务器,所以使用nc命令作为客户端时,可以使用-p选项指定使用哪个端口号连接服务器,例如我们希望通过本地的5555端口连接百度Wweb服务器时,则可以这么输入:
nc -v -p 5555 www.baidu.com 80
1. 使用nc命令与对端建立连接后,我们可以发送消息:
nc -v 127.0.0.1 6000
hello_world
如果使用nc命令发消息时不小心输入错误,则可以使用Ctrl+Backspace组合键删除输错内容,直接使用Backspace键是无法删除的。
2. nc命令也可以发送文件:
服务端(接收文件方)的命令如下:
nc -l IP地址 端口号 > 接收的文件名
客户端(发送文件方)的命令如下:
nc IP地址 端口号 < 发送的文件名
curl命令是Linux和Mac机器上可以模拟发送HTTP请求的一个很常用的命令。
curl的基础用法如下:
curl 某个URL
例如:
curl http://www.baidu.com
其默认行为是将目标页面的内容输出到Shell窗口:
ronghui@ubuntu:~$ curl http://www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
我们也可以把页面保存到本地(等价于-o选项),示例如下:
curl http://www.baidu.com > index.html
##等价于
curl -o index.html http://www.baidu.com
HTTP常采用的请求方式是GET和POST,我们可以使用-X选项显式指定请求是采用GET方式还是POST方式(不指定时,采用默认的GET方式):
curl -X GET http://www.baidu.com/index.php?s=9
如果采用GET方式,则curl提供了另一个专门的选项-G(或–get)进行设置。
如果采用POST方式,则除了需要使用-X选项(或–request)指定请求方法,还需要使用-d选项(或–data)指定POST的数据内容:
curl -X POST -d 'somepostdata' 'https://www.somesite.com/api/v1/chat'
我们有时候需要在发送HTTP请求时设置增加一些头部信息,这时可以使用-H选项(或–header)指定。如果有多个选项则使用-H选项来逐一设置,例如:
#模拟一个HTML表单上传文件功能,表单一共有两个字段uploadVideo、title
#并设置了Content-Type、Cookie两个HTTP头
curl -X POST https://www.somesite.com/api/upload -F "uploadVideo=@D:/videos/myvideo.mp4" -F "title=我的视频" -H ”Content-Type:multipart/form-data“ -H :"Cookie:sessionKey=;Device-Id=6905431231312414"
如果希望在HTTP的应答结果中包含HTTP头部信息,则可以使用-i选项(或–include),演示如下:
ronghui@ubuntu:~$ curl -i http://www.baidu.com/
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 2381
Content-Type: text/html
Date: Tue, 23 Nov 2021 14:49:02 GMT
Etag: "588604ec-94d"
Last-Modified: Mon, 23 Jan 2017 13:28:12 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
在某些情况,如果只想要显示HTTP应答的头部信息(不是HTML文档的头部),则可以使用-I选项(或–head),演示如下:
ronghui@ubuntu:~$ curl -I http://www.baidu.com/
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Tue, 23 Nov 2021 14:51:44 GMT
Etag: "575e1f6f-115"
Last-Modified: Mon, 13 Jun 2016 02:50:23 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
tcpdump命令是Linux提供的一个非常强大的抓包工具。
tcpdump命令需要有sudo权限。
tcpdump [选项与命令]
选项与命令:
-i: 指定要捕获的目标网卡名,网卡名可以使用前面介绍的ifconfig命令获得;如果要抓取所有网卡上的包,则可以使用any关键字。
-X: 以ASCII和十六进制形式输出捕获的数据包内容,减去链路层的包头信息。
-XX: 以ASCII和十六进制形式输出捕获的数据包内容,包括链路层的包头信息。
-n: 不要将IP地址显示成别名。
-nn: 不要将IP地址和端口显示成别名。
-S: 以绝对值显示包的ISN号(包括序列号),默认以上一包的偏移量显示。
-vv: 显示详细的抓包数据。
-vvv: 显示更详细的抓包数据。
-w: 将抓取的包的原始信息(不解析也不输入)写入到文件中,后跟文件名。如`tcpdump -i any -w filename`
-r: 从使用-w选项保存的包文件中读取数据包信息。
例子:
##仅显示经过8888端口的数据包(包括tcp:8888和udp:8888)
tcpdump -i any 'port 8888'
##仅显示经过tcp:8888端口的数据包
tcpdump -i any 'tcp port 8888'
##仅显示源端口是tcp:8888的数据包
tcpdump -i any 'tcp src port 8888'
##仅显示源端口是tcp:8888或目标端口是upd:9999的包
tcpdump -i any 'tcp src port:8888 or udp dst port 9999'
##仅显示源地址是127.0.0.1且源端口是tcp:9999的包,以ASCII和十六进制显示详细的输出
##不显示IP地址和端口号的别名
tcpdump -i any 'src host 127.0.0.1 anb tcp src port 9999' -XX -nn -vv
版权说明:如非注明,本站文章均为 扬州驻场服务-网络设备调试-监控维修-南京泽同信息科技有限公司 原创,转载请注明出处和附带本文链接。
请在这里放置你的在线分享代码