测试TCP和UDP端口连接

news/2024/11/20 4:45:02/

文章目录

      • 1.测试TCP和UDP端口连接状态
        • 1.1.查找命令是由那个软件包提供的
        • 1.2.安装测试端口所需的命令
        • 1.3.安装所需测试的应用
        • 1.4.启动服务
        • 1.5.查看端口
        • 1.6.测试TCP端口
        • 1.7.测试UDP端口
        • 1.8.关闭 nginx 服务
        • 1.9.开启防火墙测试161端口
        • 报错信息

1.测试TCP和UDP端口连接状态

准备环境

系统IP测试端口机器主机名
CentOS Linux192.168.192.11主机node1
CentOS Linux192.168.192.1280[TCP]
161[UDP]
3306[TCP]
69[UDP]
目标主机node2

测试主机与目标端口的网络是否通畅

两台主机的防火墙跟SELinux已关闭


1.1.查找命令是由那个软件包提供的

# 查找 telnet 命令有那个软件包提供的
[root@node1 ~]# yum -y provides telnet  # 或者 yum -y provides *bin/telnet
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile* base: ftp.sjtu.edu.cn* epel: ftp.riken.jp* extras: ftp.sjtu.edu.cn* updates: ftp.sjtu.edu.cn
1:telnet-0.17-65.el7_8.x86_64 : The client program for the Telnet remote login protocol
Repo        : base1:telnet-0.17-66.el7.x86_64 : The client program for the Telnet remote login protocol
Repo        : updates[root@node1 ~]# # 查找 nc 命令有那个软件包提供的
[root@node1 ~]# yum -y provides nc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile* base: ftp.sjtu.edu.cn* extras: ftp.sjtu.edu.cn* updates: ftp.sjtu.edu.cn
2:nmap-ncat-6.40-19.el7.x86_64 : Nmap's Netcat replacement
Repo        : base
Matched from:
Provides    : nc[root@node1 ~]# 

1.2.安装测试端口所需的命令

[root@node1 ~]# yum -y install telnet nmap

1.3.安装所需测试的应用

[root@node2 ~]# yum -y install nginx net-snmp  tftp  tftp-server
# mysql 的安装不在这里写了,如果需要请别我的安装mysql文档

1.4.启动服务

[root@node2 ~]# systemctl start snmpd
[root@node2 ~]# 
[root@node2 ~]# systemctl status snmpd
● snmpd.service - Simple Network Management Protocol (SNMP) Daemon.Loaded: loaded (/usr/lib/systemd/system/snmpd.service; disabled; vendor preset: disabled)Active: active (running) since Sat 2023-08-19 13:54:47 CST; 5s agoMain PID: 2208 (snmpd)CGroup: /system.slice/snmpd.service└─2208 /usr/sbin/snmpd -LS0-6d -fAug 19 13:54:47 node2 systemd[1]: Starting Simple Network Management Protocol (SNMP) Daemon....
Aug 19 13:54:47 node2 snmpd[2208]: NET-SNMP version 5.7.2
Aug 19 13:54:47 node2 systemd[1]: Started Simple Network Management Protocol (SNMP) Daemon..
[root@node2 ~]# [root@node2 ~]# systemctl start nginx
[root@node2 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)Active: active (running) since Sat 2023-08-19 13:35:21 CST; 20min agoMain PID: 1900 (nginx)CGroup: /system.slice/nginx.service├─1900 nginx: master process /usr/sbin/nginx├─1901 nginx: worker process├─1902 nginx: worker process└─1903 nginx: worker process
......省略[root@node2 ~]# systemctl start mysqld
[root@node2 ~]# systemctl status mysqld
● mysqld.service - MySQL ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)Active: active (running) since Sat 2023-08-19 13:44:51 CST; 11min agoDocs: man:mysqld(8)
......省略N[root@node2 ~]# systemctl start tftp
[root@node2 ~]# 
[root@node2 ~]# systemctl status tftp
● tftp.service - Tftp ServerLoaded: loaded (/usr/lib/systemd/system/tftp.service; indirect; vendor preset: disabled)Active: active (running) since Sat 2023-08-19 13:57:06 CST; 3s agoDocs: man:in.tftpdMain PID: 2221 (in.tftpd)CGroup: /system.slice/tftp.service└─2221 /usr/sbin/in.tftpd -s /var/lib/tftpbootAug 19 13:57:06 node2 systemd[1]: Started Tftp Server.
[root@node2 ~]# 

1.5.查看端口

[root@node2 ~]# ss -antlp | grep 3306
LISTEN     0      70     [::]:33060   [::]:*  users:(("mysqld",pid=2107,fd=22))
LISTEN     0      128    [::]:3306    [::]:*  users:(("mysqld",pid=2107,fd=25))
[root@node2 ~]# [root@node2 ~]# ss -antlp | grep 80
LISTEN   0   128    *:80    *:*     users:(("nginx",pid=1903,fd=6),("nginx",pid=1902,fd=6),("nginx",pid=1901,fd=6),("nginx",pid=1900,fd=6))
LISTEN   0   128   [::]:80  [::]:*  users:(("nginx",pid=1903,fd=7),("nginx",pid=1902,fd=7),("nginx",pid=1901,fd=7),("nginx",pid=1900,fd=7))
[root@node2 ~]#[root@node2 ~]# ss -anulp | grep 161
UNCONN   0   0     *:161    *:*      users:(("snmpd",pid=2208,fd=6))
[root@node2 ~]# 
[root@node2 ~]# ss -anulp | grep 69
UNCONN  0    0    [::]:69   [::]:* users:(("in.tftpd",pid=2221,fd=0),("systemd",pid=1,fd=27))
[root@node2 ~]# [root@node2 ~]# ss -antlu
Netid  State      Recv-Q Send-Q Local Address:Port         Peer Address:Port              
udp    UNCONN     0      0             *:161                 *:*                  
udp    UNCONN     0      0          [::]:69                  [::]:*                  
tcp    LISTEN     0      128    127.0.0.1:199                *:*                  
tcp    LISTEN     0      128           *:80                  *:*                  
tcp    LISTEN     0      128           *:22                  *:*                  
tcp    LISTEN     0      70         [::]:33060               [::]:*                  
tcp    LISTEN     0      128        [::]:3306                [::]:*                  
tcp    LISTEN     0      128        [::]:80                  [::]:*                  
tcp    LISTEN     0      128        [::]:22                  [::]:*                  
[root@node2 ~]# 

1.6.测试TCP端口

[root@node1 ~]# telnet 192.168.192.12 80
Trying 192.168.192.12...
Connected to 192.168.192.12.  # 看到 Connected 表示192.168.192.12开通了80端口
Escape character is '^]'.
Connection closed by foreign host.
[root@node1 ~]# # 如果以上的命令看得不是很理解可以使用nmap命令
# nmap命令 – 网络探测及端口扫描工具[root@node1 ~]# nmap -p 80,3306,443 192.168.192.12 # 表示扫描80跟443端口Starting Nmap 6.40 ( http://nmap.org ) at 2023-08-19 14:08 CST
Nmap scan report for 192.168.192.12
Host is up (0.00030s latency).
PORT    STATE  SERVICE
80/tcp  open   http   # open 表示打开了80 端口
443/tcp closed https  # closed 表示关闭
3306/tcp open   mysql
MAC Address: 00:0C:29:6A:EF:87 (VMware)Nmap done: 1 IP address (1 host up) scanned in 13.46 seconds
[root@node1 ~]# [root@node1 ~]# nc -vz 192.168.192.12 80
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.192.12:80.
Ncat: 0 bytes sent, 0 bytes received in 0.00 seconds.
[root@node1 ~]# # Ncat: 7.50版本(https://nmap.org/ncat)
# Ncat:连接192.168.192.12:80。
# Ncat:发送0字节,在0.00秒内接收0字节。

1.7.测试UDP端口

[root@node1 ~]# nmap -sU -p 161,69 192.168.192.12Starting Nmap 6.40 ( http://nmap.org ) at 2023-08-19 14:21 CST
Nmap scan report for 192.168.192.12
Host is up (0.00029s latency).
PORT    STATE         SERVICE
69/udp  open|filtered tftp
161/udp open          snmp
MAC Address: 00:0C:29:6A:EF:87 (VMware)Nmap done: 1 IP address (1 host up) scanned in 14.47 seconds
[root@node1 ~]# [root@node1 ~]# nc -vuz 192.168.192.12 161
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.192.12:161.
Ncat: UDP packet sent successfully  # 看到successfully 表示成功
Ncat: 1 bytes sent, 0 bytes received in 2.01 seconds.
[root@node1 ~]# 

以上都是测试成功的,现在模拟服务没有开启或者防火墙阻挡



1.8.关闭 nginx 服务

[root@node2 ~]# systemctl stop nginx# 到192.168.192.11 主机上测试
[root@node1 ~]# nc -vz 192.168.192.12 80
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connection refused. # 看到refused 表示拒绝连接
[root@node1 ~]# 
[root@node1 ~]# nmap -p80,3306 192.168.192.12Starting Nmap 6.40 ( http://nmap.org ) at 2023-08-19 14:32 CST
Nmap scan report for 192.168.192.12
Host is up (0.00033s latency).
PORT     STATE  SERVICE
80/tcp   closed http  # 表示失败
3306/tcp open   mysql # 表示成功
MAC Address: 00:0C:29:6A:EF:87 (VMware)Nmap done: 1 IP address (1 host up) scanned in 13.45 seconds
[root@node1 ~]# 

1.9.开启防火墙测试161端口

[root@node2 ~]# systemctl start firewalld
[root@node2 ~]# 
[root@node2 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: active (running) since Sat 2023-08-19 14:36:34 CST; 1min 27s agoDocs: man:firewalld(1)
......# 在node1测试 161 端口
[root@node1 ~]# nc -vuz 192.168.192.12 161
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.192.12:161.
Ncat: No route to host.  # 表示失败
[root@node1 ~]# 
[root@node1 ~]# nmap -sU -p 161 192.168.192.12Starting Nmap 6.40 ( http://nmap.org ) at 2023-08-19 14:39 CST
Nmap scan report for 192.168.192.12
Host is up (0.00028s latency).
PORT    STATE    SERVICE
161/udp filtered snmp  
MAC Address: 00:0C:29:6A:EF:87 (VMware)Nmap done: 1 IP address (1 host up) scanned in 13.45 seconds
[root@node1 ~]# # 放行 161 端口
[root@node2 ~]# firewall-cmd --zone=public --add-port=161/udp --permanent
success
[root@node2 ~]# firewall-cmd --reload  # 更新防火墙规则,不然不会生效
success# 查看端口是否开放
[root@node2 ~]# firewall-cmd --zone=public --query-port=161/udp
yes
[root@node2 ~]## 再次测试 161 端口
[root@node1 ~]# nc -vuz 192.168.192.12 161
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.192.12:161.
Ncat: UDP packet sent successfully  # 成功
Ncat: 1 bytes sent, 0 bytes received in 2.01 seconds.
[root@node1 ~]#[root@node1 ~]# nmap -sU -p 161 192.168.192.12Starting Nmap 6.40 ( http://nmap.org ) at 2023-08-19 14:51 CST
Nmap scan report for 192.168.192.12
Host is up (0.00048s latency).
PORT    STATE SERVICE
161/udp open  snmp
MAC Address: 00:0C:29:6A:EF:87 (VMware)Nmap done: 1 IP address (1 host up) scanned in 13.46 seconds
[root@node1 ~]#

报错信息

安装 net-snmp 报错信息:

Error: Package: 1:net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64 (xxx)Requires: libmysqlclient.so.18()(64bit)
Error: Package: 1:net-snmp-5.7.2-49.el7_9.2.x86_64 (xxx)Requires: libmysqlclient.so.18()(64bit)
Error: Package: 1:net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64 (xxx)Requires: libmysqlclient.so.18(libmysqlclient_18)(64bit)You could try using --skip-broken to work around the problemYou could try running: rpm -Va --nofiles --nodigest

解决安装 net-snmp 报错

[root@node2 ~]# yum -y install https://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64/mysql-community-libs-compat-8.0.25-1.el7.x86_64.rpm

http://www.ppmy.cn/news/1044210.html

相关文章

第二章MyBatis入门程序

入门程序 创建maven程序 导入MyBatis依赖。pom.xml下导入如下依赖 <dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency><dependen…

Android kotlin系列讲解(入门篇)使用Intent在Activity之间穿梭

<<返回总目录 上一篇:Android kotlin系列讲解(入门篇)Activity的理解与基本用法 文章目录 1、使用显式Intent2、使用隐式Intent3、更多隐式Intent的用法4、向下一个Activity传递数据5、返回数据给上一个Activity1、使用显式Intent 你应该已经对创建Activity的流程比较…

C++学习笔记总结练习:primer 学习日志

文章目录 针对自己的引言学习内容c语言基础知识1.为什么要声明变量2.cout ,cin3.c 不容许一个函数定义嵌套到另一个函数的定义中。4.编译指令using5.c基本类型长度6.在定义常量时尽可能使用const 关键字而不用#define9.前缀递增符与后缀递增符的区别10.c中的cctype库11.c 中的s…

linux学习(文件描述符)[13]

所以fork的时候函数执行完毕&#xff0c;但是数据还在缓冲区中未刷新。 所以会有父子两份数据 在fork&#xff08;&#xff09;之前ffush&#xff08;&#xff09;&#xff08;c语言的接口&#xff0c;刷新缓冲区&#xff09;fflush(stdout)&#xff0c;就不会有重复 缓冲区的…

Android PowerManager的使用

唤醒锁定 如果需要使 CPU 保持运行状态&#xff0c;以便在设备进入休眠模式之前完成某项工作&#xff0c;可以使用“唤醒锁定” 。 添加权限 <uses-permission android:name"android.permission.WAKE_LOCK" />设置唤醒锁定 PowerManager powerManager (Po…

【C#学习笔记】C#特性的继承,封装,多态

文章目录 封装访问修饰符静态类和静态方法静态构造函数 继承继承原则sealed修饰符里氏替换原则继承中的构造函数 多态接口接口的实例化 抽象类和抽象方法抽象类和接口的异同 虚方法同名方法new覆盖的父类方法继承的同名方法 运行时的多态性编译时的多态性 照理继承封装多态应该…

TDSQL赤兔管理台无管理员用户密码解决方案

解决方案 问题描述&#xff1a; tdsql使用过程中&#xff0c;可能会遇到控制台用户密码忘记的情况&#xff0c;用户登录次数过多被锁的情况&#xff0c;没有管理员的用户密码又急需某些权限的情况。 解决过程&#xff1a; 获取配置库信息&#xff1a; 在浏览器上打开如下命…

机器学习重要内容:特征工程之特征抽取

目录 1、简介 2、⭐为什么需要特征工程 3、特征抽取 3.1、简介 3.2、特征提取主要内容 3.3、字典特征提取 3.4、"one-hot"编码 3.5、文本特征提取 3.5.1、英文文本 3.5.2、结巴分词 3.5.3、中文文本 3.5.4、Tf-idf ⭐所属专栏&#xff1a;人工智能 文中提…