命令行学习笔记:网络相关命令

学习内容

网络相关:ping, telnet, curl, netstat,修改hosts

个人总结

  • ping
    简述:
    Linux 系统的ping 命令是常用的网络命令,它通常用来测试与目标主机的连通性。比如:我们经常说的ping一下某主机看是否能够访问到、不能打开网页的时候先ping 网关地址192.168.1.1尝试默认网关是否能够联通。它通过发送ICMP ECHO_REQUEST数据包到网络主机(send ICMP ECHO_REQUEST to network hosts),并显示响应情况,这样我们就可以根据它输出的信息来确定目标主机是否可访问(但这不是绝对的)。有些服务器为了防止通过ping探测到,通过防火墙设置了禁止ping或者在内核参数中禁止ping,这样就不能通过ping确定该主机是否还处于开启状态.

命令格式:
ping [参数] [主机名或IP地址]

功能:

  • 确定网络和各外部主机的状态;
  • 跟踪和隔离硬件和软件问题;
  • 测试、评估和管理网络。

如果主机正在运行并连在网上,它就对回送信号进行响应。每个回送信号请求包含一个网际协议(IP)和 ICMP 头,后面紧跟一个 tim 结构,以及来填写这个信息包的足够的字节。缺省情况是连续发送回送信号请求直到接收到中断信号(Ctrl-C)。

参数:

-d 使用Socket的SO_DEBUG功能。
-f  极限检测。大量且快速地送网络封包给一台机器,看它的回应。
-n 只输出数值。
-q 不显示任何传送封包的信息,只显示最后的结果。
-r 忽略普通的Routing Table,直接将数据包送到远端主机上。通常是查看本机的网络接口是否有问题。
-R 记录路由过程。
-v 详细显示指令的执行过程。
<p>-c 数目:在发送指定数目的包后停止。
-i 秒数:设定间隔几秒送一个网络封包给一台机器,预设值是一秒送一次。
-I 网络界面:使用指定的网络界面送出数据包。
-l 前置载入:设置在送出要求信息之前,先行发出的数据包。
-p 范本样式:设置填满数据包的范本样式。
-s 字节数:指定发送的数据字节数,预设值是56,加上8字节的ICMP头,一共是64ICMP数据字节。
-t 存活数值:设置存活数值TTL的大小。

实例:

  • ping 不通的情况
➜  ~ ping 192.168.1.99
PING 192.168.1.99 (192.168.1.99) 56(84) bytes of data.
From 192.168.1.196 icmp_seq=1 Destination Host Unreachable
From 192.168.1.196 icmp_seq=2 Destination Host Unreachable
From 192.168.1.196 icmp_seq=3 Destination Host Unreachable
From 192.168.1.196 icmp_seq=4 Destination Host Unreachable
From 192.168.1.196 icmp_seq=5 Destination Host Unreachable
From 192.168.1.196 icmp_seq=6 Destination Host Unreachable
From 192.168.1.196 icmp_seq=7 Destination Host Unreachable
From 192.168.1.196 icmp_seq=8 Destination Host Unreachable
From 192.168.1.196 icmp_seq=9 Destination Host Unreachable
^C
--- 192.168.1.99 ping statistics ---
12 packets transmitted, 0 received, +9 errors, 100% packet loss, time 11064ms
pipe 3
  • ping 通的情况
➜  ~ ping 192.168.1.123
PING 192.168.1.123 (192.168.1.123) 56(84) bytes of data.
64 bytes from 192.168.1.123: icmp_seq=1 ttl=128 time=1.65 ms
64 bytes from 192.168.1.123: icmp_seq=2 ttl=128 time=0.844 ms
64 bytes from 192.168.1.123: icmp_seq=3 ttl=128 time=0.959 ms
64 bytes from 192.168.1.123: icmp_seq=4 ttl=128 time=0.736 ms
64 bytes from 192.168.1.123: icmp_seq=5 ttl=128 time=0.732 ms
^C
--- 192.168.1.123 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4002ms
rtt min/avg/max/mdev = 0.732/0.984/1.653/0.346 ms
  • ping 网关
➜  ~ ping -b 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.211 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.225 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.222 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.182 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=0.464 ms
64 bytes from 192.168.1.1: icmp_seq=6 ttl=64 time=0.224 ms
^C
--- 192.168.1.1 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 4999ms
rtt min/avg/max/mdev = 0.182/0.254/0.464/0.096 ms

  • ping 指定次数
➜  ~ ping -c 3 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.22 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.360 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.382 ms

--- 192.168.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.360/0.654/1.221/0.401 ms
  • ping 指定时间间隔
➜  ~ ping -i 0.5 192.168.1.123
PING 192.168.1.123 (192.168.1.123) 56(84) bytes of data.
64 bytes from 192.168.1.123: icmp_seq=1 ttl=128 time=1.14 ms
64 bytes from 192.168.1.123: icmp_seq=2 ttl=128 time=0.999 ms
64 bytes from 192.168.1.123: icmp_seq=3 ttl=128 time=0.822 ms
64 bytes from 192.168.1.123: icmp_seq=4 ttl=128 time=0.952 ms
64 bytes from 192.168.1.123: icmp_seq=5 ttl=128 time=0.847 ms
64 bytes from 192.168.1.123: icmp_seq=6 ttl=128 time=0.957 ms
64 bytes from 192.168.1.123: icmp_seq=7 ttl=128 time=0.848 ms
^C
--- 192.168.1.123 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 0.822/0.938/1.141/0.103 ms

  • ping 指定时间间隔和指定次数
➜  ~ ping -c5 -i 0.5 192.168.1.123
PING 192.168.1.123 (192.168.1.123) 56(84) bytes of data.
64 bytes from 192.168.1.123: icmp_seq=1 ttl=128 time=0.775 ms
64 bytes from 192.168.1.123: icmp_seq=2 ttl=128 time=0.941 ms
64 bytes from 192.168.1.123: icmp_seq=3 ttl=128 time=0.888 ms
64 bytes from 192.168.1.123: icmp_seq=4 ttl=128 time=0.761 ms
64 bytes from 192.168.1.123: icmp_seq=5 ttl=128 time=0.991 ms

--- 192.168.1.123 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.761/0.871/0.991/0.092 ms

  • ping 公网上站点
➜  ~ ping -c 5 www.sina.com
PING cernetnews.sina.com.cn (121.194.0.239) 56(84) bytes of data.
64 bytes from 121.194.0.239: icmp_seq=1 ttl=50 time=21.3 ms
64 bytes from 121.194.0.239: icmp_seq=2 ttl=50 time=18.7 ms
64 bytes from 121.194.0.239: icmp_seq=3 ttl=50 time=17.8 ms
64 bytes from 121.194.0.239: icmp_seq=4 ttl=50 time=19.7 ms
64 bytes from 121.194.0.239: icmp_seq=5 ttl=50 time=18.3 ms

--- cernetnews.sina.com.cn ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 17.860/19.218/21.344/1.248 ms

  • ping 多参数使用
➜  ~ ping -i 0.5 -c 5 -s 512 -t 255 192.168.1.123
PING 192.168.1.123 (192.168.1.123) 512(540) bytes of data.
520 bytes from 192.168.1.123: icmp_seq=1 ttl=128 time=1.64 ms
520 bytes from 192.168.1.123: icmp_seq=2 ttl=128 time=1.05 ms
520 bytes from 192.168.1.123: icmp_seq=3 ttl=128 time=1.68 ms
520 bytes from 192.168.1.123: icmp_seq=4 ttl=128 time=1.45 ms
520 bytes from 192.168.1.123: icmp_seq=5 ttl=128 time=1.66 ms

--- 192.168.1.123 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 1.051/1.500/1.683/0.239 ms

  • telnett
    简述:
      telnet命令通常用来远程登录。
    telnet程序是基于TELNET协议远程登录客户端程序Telnet协议TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式。它为用户提供了在本地计算机上完成远程主机工作的 能力。在终端使用者的电脑上使用telnet程序,用它连接到服务器。终端使用者可以在telnet程序中输入命令,这些命令会在服务器上运行,就像直接在服务器的控制台上输入一样。可以在本地就能控制服务器。要开始一个 telnet会话,必须输入用户名和密码来登录服务器。Telnet是常用的远程控制Web服务器的方法
      但是,telnet因为采用明文传送报文,安全性不好,很多Linux服务器都不开放telnet服务,而改用更安全的ssh方式了。但仍然有很多别的系统可能采用了telnet方式来提供远程登录,因此弄清楚telnet客户端的使用方式仍是很有必要的。
      telnet命令还可做别的用途,比如确定远程服务的状态,比如确定远程服务器的某个端口是否能访问

命令格式
telnet [参数][主机]

功能
执行 telnett 命令开启终端机器阶段作业,并登入远端主机。

参数

-8 允许使用8位字符资料,包括输入与输出。
-a 尝试自动登入远端系统。
-b<主机别名> 使用别名指定远端主机名称。
-c 不读取用户专属目录里的.telnetrc文件。
-d 启动排错模式。
-e<脱离字符> 设置脱离字符。
-E 滤除脱离字符。
-f 此参数的效果和指定"-F"参数相同。
-F 使用Kerberos V5认证时,加上此参数可把本地主机的认证数据上传到远端主机。
-k<域名> 使用Kerberos认证时,加上此参数让远端主机采用指定的领域名,而非该主机的域名。
-K 不自动登入远端主机。
-l<用户名称> 指定要登入远端主机的用户名称。
-L 允许输出8位字符资料。
-n<记录文件> 指定文件记录相关信息。
-r 使用类似rlogin指令的用户界面。
-S<服务类型> 设置telnet连线所需的IP TOS信息。
-x 假设主机有支持数据加密的功能,就使用它。
-X<认证形态> 关闭指定的认证形态。

实例

zhyingjia@zhyingjia-Inspiron-5437:~$ telnet 192.168.1.243 22
Trying 192.168.1.243...
Connected to 192.168.1.243.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.3

Protocol mismatch.
Connection closed by foreign host.

  • curl
    简述:curl是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称url为下载工具。
    参数:
    参数太多啦, 用的时候在搜~
    实例:
  • 抓取页面内容
    curl -o/--output filename link 把输出写到该文件中。
    curl -O url 后面的url要具体到某个文件,不然抓不下来。我们还可以用正则来抓取东西
➜  newDir ls
➜  newDir curl -o shouhui.html http://www.jianshu.com/p/5dcce74fc415
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 41120  100 41120    0     0  82488      0 --:--:-- --:--:-- --:--:-- 82404
➜  newDir ls
shouhui.html
➜  newDir 

➜  newDir curl -O http://www.jianshu.com/p/5ead3cfb630b                 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 47028  100 47028    0     0    97k      0 --:--:-- --:--:-- --:--:--   97k
➜  newDir ls
5ead3cfb630b  shouhui.html
➜  newDir 

  • 模拟登录
    curl -c <file> 操作结束后把cookie写入到这个文件中
    curl -F <name=content> 模拟http表单提交数据
➜  newDir curl -c ./cookie_c.txt -F log=aaaa -F pwd=****** http://www.2cto.com /wp-login.php
<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx</center>
</body>
</html>
curl: (3) <url> malformed

  • 显示下载进度条
    curl -#
➜  newDir ls
5ead3cfb630b  shouhui.html
➜  newDir curl -O -# http://www.jianshu.com/p/230efbd68016
######################################################################## 100.0%
➜  newDir ls
230efbd68016  5ead3cfb630b  shouhui.html
➜  newDir 

  • 断点续传
    curl -C -O url 下载比较打的文件可以使用 -C.
  • netstat
    简述 :
    netstat命令 用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告.
    常用参数:
    netstat -a 显示端口信息
    netstat -nu 显示当前UDP连接情况
    netstat -apu 显示当前UDP端口号使用情况
    netstat -i 显示网卡列表
    netstat -g显示组播关系
    netstat -l显示监听的套接口
    netstat -n显示多有已建立的连接
    netstat -e显示关于以太网的统计数据
    netstat -r显示路由表信息
    netstat -at列出所有TCP端口

实例:

  • 无参数使用

netstat

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      1 192.168.1.197:38870     www.google.com.gr:https SYN_SENT   
tcp        1      1 192.168.1.197:48054     121.194.7.192:http      LAST_ACK   
tcp        0      1 192.168.1.197:38854     www.google.com.gr:https SYN_SENT   
tcp        0      1 192.168.1.197:38860     www.google.com.gr:https SYN_SENT   
tcp        0      1 192.168.1.197:38856     www.google.com.gr:https SYN_SENT   
tcp        0      1 192.168.1.197:34186     prg03s05-in-f14.1:https SYN_SENT   
tcp        1      1 192.168.1.197:36790     121.194.7.19:http       LAST_ACK   
tcp        0      1 192.168.1.197:38858     www.google.com.gr:https SYN_SENT   
tcp        0      1 192.168.1.197:38880     www.google.com.gr:https SYN_SENT   
tcp        0      1 192.168.1.197:38852     www.google.com.gr:https SYN_SENT   
tcp        0      1 192.168.1.197:38864     www.google.com.gr:https SYN_SENT   
tcp        0      1 192.168.1.197:38884     www.google.com.gr:https SYN_SENT   
tcp        1      1 192.168.1.197:45486     23.248.163.147:http     LAST_ACK   
tcp        0      0 192.168.1.197:50202     hn.kd.ny.adsl:82        ESTABLISHED
 
   ... ... 
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    22602    /run/user/1000/systemd/notify
unix  8      [ ]         DGRAM                    568      /run/systemd/journal/socket
unix  17     [ ]         DGRAM                    11639    /run/systemd/journal/dev-log
unix  2      [ ]         DGRAM                    11642    /run/systemd/journal/syslog
unix  3      [ ]         SEQPACKET  CONNECTED     31602    @0001b
unix  2      [ ]         DGRAM                    17911    /run/wpa_supplicant/wlp6s0
unix  3      [ ]         DGRAM                    556      /run/systemd/notify
unix  3      [ ]         SEQPACKET  CONNECTED     27350    
unix  3      [ ]         STREAM     CONNECTED     26055    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     24124    
unix  3      [ ]         STREAM     CONNECTED     23298    @/tmp/dbus-sYzUcwM148
unix  3      [ ]         STREAM     CONNECTED     25062    @/tmp/dbus-sYzUcwM148
unix  3      [ ]         STREAM     CONNECTED     22310    
unix  3      [ ]         STREAM     CONNECTED     29561    
unix  3      [ ]         STREAM     CONNECTED     23338    @/tmp/dbus-NFWQkgLokd
unix  3      [ ]         STREAM     CONNECTED     24598    @/tmp/.X11-unix/X0
... ... 

说明:
netstat 命令输出结果分为两部分:

1. Active Internet connections : 有源 TCP 连接 
          Recv-Q : 接受队列
          Send-Q : 发送队列 (这两列一般情况下为0,如果为1,则表示有软件包在队列中堆积)
2.Active UNIX domain sockets : 有源UNIX域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)
       Proto  : 显示连接使用的协议
      RefCnt  :    表示连接到本套接口上的进程号
       Types  :    显示套接口的类型
       State   : 显示套接口当前的状态
         Path  : 表示连接到套接口的其它进程使用的路径名

状态字段说明

LISTEN:侦听来自远方的TCP端口的连接请求
SYN-SENT:再发送连接请求后等待匹配的连接请求(如果有大量这样的状态包,检查是否中招了)
SYN-RECEIVED:再收到和发送一个连接请求后等待对方对连接请求的确认(如有大量此状态,估计被flood攻击了)
ESTABLISHED:代表一个打开的连接
FIN-WAIT-1:等待远程TCP连接中断请求,或先前的连接中断请求的确认
FIN-WAIT-2:从远程TCP等待连接中断请求
CLOSE-WAIT:等待从本地用户发来的连接中断请求
CLOSING:等待远程TCP对连接中断的确认
LAST-ACK:等待原来的发向远程TCP的连接中断请求的确认(不是什么好东西,此项出现,检查是否被攻击)
TIME-WAIT:等待足够的时间以确保远程TCP接收到连接中断请求的确认
CLOSED:没有任何连接状态
  • 显示端口信息
    netstat -a
➜  ~ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:mysql         *:*                     LISTEN     
tcp        0      0 zhyingjia-Inspir:domain *:*                     LISTEN     
tcp        0      0 *:ssh                   *:*                     LISTEN     
tcp        0      1 192.168.1.197:39094     www.google.com.gr:https SYN_SENT   
tcp        0      0 192.168.1.197:56872     106.75.2.241:http       ESTABLISHED
tcp        0      1 192.168.1.197:39088     www.google.com.gr:https SYN_SENT   
tcp        0      1 192.168.1.197:39092     www.google.com.gr:https SYN_SENT   
tcp        1      1 192.168.1.197:45848     23.248.163.147:http     LAST_ACK   
tcp        0      1 192.168.1.197:39098     www.google.com.gr:https SYN_SENT  
  • ssh
    简述:
    安全外壳协议(SSH)是一种在不安全网络上提供安全远程登录及其它安全网络服务的协议。
    命令格式:
    ssh [-l login_name] [-p port] [user@]hostname
    常用参数:
    ssh -p port [user@]hostname 远程登录指定 IP 主机的端口
    ssh -u login_name [user@]hostname 指定用户

实例:

  • 指定用户
    ssh -l long_name ip
    ssh login_name@ip
➜  ~ ssh -l root 192.168.1.222
root@192.168.1.222's password: 


BusyBox v1.20.2 () built-in shell (ash)
Enter 'help' for a list of built-in commands.

this board is EasyARM-iMX283
root@EasyARM-iMX28x ~# 

➜  ~ ssh root@192.168.1.222
root@192.168.1.222's password: 


BusyBox v1.20.2 () built-in shell (ash)
Enter 'help' for a list of built-in commands.

this board is EasyARM-iMX283
root@EasyARM-iMX28x ~# 

  • 指定端口(若不指定端口,ssh 命令默认登录 22 端口)
    ssh -l long_name -p port ip
    ssh -p port login_name@ip
➜  ~ ssh -l root -p 22 192.168.1.222
root@192.168.1.222's password: 


BusyBox v1.20.2 () built-in shell (ash)
Enter 'help' for a list of built-in commands.

this board is EasyARM-iMX283
root@EasyARM-iMX28x ~# 

➜  ~ ssh -p 22 root@192.168.1.222
root@192.168.1.222's password: 


BusyBox v1.20.2 () built-in shell (ash)
Enter 'help' for a list of built-in commands.

this board is EasyARM-iMX283
root@EasyARM-iMX28x ~# 

  • 更改 ssh 默认端口
    1.修改配置文件 :/etc/ssh/sshd_config
    2.重启服务 :service sshd restart

➜  ~ sudo vim /etc/ssh/sshd_config
//  修改端口
修改 Port 22  ->  Port 23   :wq 保存并退出
➜  ~ cat /etc/ssh/sshd_config
# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 23
# Use these options to restrict which interfaces/protocols sshd will bind to
  ... ... 
➜  ~ service sshd restart           
➜  ~ ssh root@192.168.1.222
root@192.168.1.222's password: 


BusyBox v1.20.2 () built-in shell (ash)
Enter 'help' for a list of built-in commands.

this board is EasyARM-iMX283
root@EasyARM-iMX28x ~# 

  • scp
    scp <file-name> <user-name>@<des-ip>:[des-path]  远程传输文件
    scp -r <dir-name> <user-name>@<des-ip>:[des-path] 远程传输文件夹
 //  发送方
[root@hadoop1 ~]# scp client1.c zhyingjia@192.168.1.196:
The authenticity of host '192.168.1.196 (192.168.1.196)' can't be established.
RSA key fingerprint is b4:8a:27:96:37:e5:27:4b:67:9f:6f:bc:51:c3:9e:f3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.196' (RSA) to the list of known hosts.
zhyingjia@192.168.1.196's password: 
client1.c                                     100% 5320     5.2KB/s   00:00  


[root@hadoop1 ~]# scp -r testDir/ zhyingjia@192.168.1.196:
zhyingjia@192.168.1.196's password: 
[root@hadoop1 ~]# ls

// 接收前
➜  /home ls 
zhyingjia
➜  /home cd zhyingjia 
➜  ~ ls
Book              IdeaProjects        Public            Videos
client1.c         logs                Ruby              WebstormProjects
Desktop           Music               RubymineProjects  wince3+1
Documents         newDir              sshd.txt
Downloads         newRamlProject      Templates
Pictures     
     
// 接收后
➜  /home ls 
zhyingjia
➜  /home cd zhyingjia 
➜  ~ ls
Book              IdeaProjects        Public            Videos
client1.c         logs                Ruby              WebstormProjects
Desktop           Music               RubymineProjects  wince3+1
Documents         newDir              sshd.txt
Downloads         newRamlProject      Templates
Pictures          testDir

// 文件夹已经传送过来。

参考

    原文作者:z嘉嘉嘉
    原文地址: https://www.jianshu.com/p/8dbdeac38907
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞