ping 命令

ping 命令通过 ICMP (Internet Control Message Protocol) 中的 echo 分组测试两台主机的连通性。向某台主机发送 echo 分组时,如果能够送达目标主机,可以返回一条 reply 消息;如果没有到目标主机的路由或其它原因,则 ping 命令失败。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# -c n 限制发送的分组数量
# icmp_seq icmp 序号
# ttl time to live 剩余路由器转发的次数,次数为 0 时还没有到达目标主机,消息将被丢弃
# time RTT(Round Trip Time) 分组的往返时间
guo@DESKTOP-4L69AND:/mnt/e/learning-dir/shell-learning$ ping baidu.com -c 5
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=52 time=35.3 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=52 time=35.4 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=52 time=35.4 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=52 time=35.8 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=5 ttl=52 time=35.8 ms

--- baidu.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 5053ms
rtt min/avg/max/mdev = 35.322/35.588/35.876/0.331 ms

ping 命令执行顺利,目标主机可达,退出状态为 0;否则非 0,目标主机不可达。

测试局域网下所有可达的主机:

1
2
3
4
5
6
7
8
9
10
#!/bin/bash

for ip in 192.168.31.{1..255}
do
echo "testing $ip"
ping $ip -c 1 &> /dev/null ;
if [ $? -eq 0 ];then
echo $ip is alive
fi
done

创建套接字

TCP/IP 网络中用于传输数据的套接字,可以使用 netcatnc 命令。

基本使用

  1. 首先创建一个监听本地端口 1234 的套接字
1
2
# -l 指定监听一个要连接接到本地某个端口的套接字,而不是初始化一个到远程主机的连接
ph@guo-lenovo:~$ nc -l 1234
  1. 本地或其它主机连接到上述监听套接字
1
ph@guo-lenovo:~$ nc localhost 1234
  1. 任意一端中输入信息并按下回车键,信息会出现在另一端中,完成了一次通信
1
2
3
4
5
6
7
8
ph@guo-lenovo:~$ nc -l 1234
hello
a


ph@guo-lenovo:~$ nc localhost 1234
hello
a

快速复制文件

主要利用的时 shell 的重定向

  1. 监听端
1
nc -l 1234 > nc_redirect_out
  1. 发送端
1
nc 192.168.31.188 1234 < yihuo.sh
  1. 文件内容
1
2
3
4
5
6
7
8
9
10
11
12
ph@guo-lenovo:~$ cat nc_redirect_out
#!/bin/bash

orig=(01101101 01101001 01100100 01101110 01101001 01100111 01101000 01110100)
key=(01001101 01001101 01010100 01111110 01101111 01100001 01000000 00010100)

for i in "${!orig[@]}";do
o=$(echo -n $((2#${orig[$i]})))
k=$(echo -n $((2#${key[$i]})))
echo $(($o ^ $k)) | xargs -n 1 | while read dec; do echo "ibase=10;obase=2;$dec" | bc | tr "\n" " " | sed 's/^/0/g'; done

done

搭建网桥

image-20211114121038694

主要使用到了 ip link 命令, 主机有两个网卡, eth0 配置连接到子网 192.168.1.0,eth1 通过网桥连接到子网 10.0.0.0, 并为网桥配置 ip 地址为 10.0.0.2,如果以太网适配器加入了网桥,该适配器不再配置 ip 地址,需要配置地址的时网桥。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 创建名为 br0 的网桥
ip link add br0 type bridge

# 将以太网适配器添加到网桥
ip link sed dev eht1 master br0

# 配置网桥的 ip 地址
ifconfig br0 10.0.0.2

# 启用分组转发
echo 1 > /proc/sys/net/ipv4/ip_forward

# 10.0.0.0/24 中的主机添加路由
route add -net 192.168.1.0/16 gw 10.0.0.2

# 192.168.1.0/16 中的主机添加路由
route add -net 10.0.0.0/24 gw 192.168.1.2

Internet 连接共享

Internet 连接共享其实也是利用的分组转发的功能,类似上面的网桥部分,起到了路由器的功能,可以提供防火墙以及连接共享。假设有线网络连接 eth0 连接到了 internet,可以创建无线连接,并创建一个共享的无线网络,是其它设备连接到这个无线网络,其中的分组通过虚拟路由器,最终转发到互联网

使用 iptables 架设简易防火墙

防火墙的目的主要时过滤、阻止不需要的网络流量,允许正常的网络流量通过。

  • 阻止到特定 IP 地址的流量
1
2
3
4
5
# -A Append to chain 链就是若干规则的集合,追加到 OUTPUT 链,控制所有的出站流量 (outgoing traffic)
# -I Insert in chain as rulenum (default 1=first)
# -d 匹配分组的目的地址
# -j iptables 执行特定的处理:DROP,ACCEPT,REJECT
iptables -A OUTPUT -d 8.8.8.8 -j DROP
  • 阻止到特定端口的流量
1
2
3
4
# -P 指定规则仅适用于 TCP
# -dport 指定了对应的端口
# 这里来相当于阻止了所有出站的 FTP 流量
iptables -A OUTPUT -p tcp -dport 21 -j DROP
  • 阻止进入的特定流量
1
2
3
4
# -I 新的规则将插入到规则集的开头
# INPUT INPUT 链,控制所有的入站流量 (incoming traffic)
# -s 指定了分组的源地址
iptables -I INPUT -s 1.2.3.4 -j DROP
1
2
# -flush 清除对 iptables 链所作出的所有改动
iptables -flush