15. Linux: IP and port check command

Local IP: 10.129.49.102, connection test host: 10.129.49.104

1. Query IP

1. Query the IP information of the current server

ifconfig
---或使用:
ip addr

Please note that when the ifconfig command is used for query, it mainly queries three types of information such as IP address, subnet mask and MAC address. Of course, the ifconfig command can also be used to temporarily modify the IP information, but once the server is restarted, the configuration will become invalid.

Temporarily configure the IP address, the format is as follows:

ifconfig 网卡名称 IP地址

Temporary configuration of IP address, broadcast address, subnet mask and other information, the format is as follows:

ifconfig 网卡名称 192.168.xx.xx broadcast 192.168.1.255 netmask 255.255.255.0

2. Query the export IP address

curl cip.cc

The egress IP is also called the external network IP (public network IP). It is the IP of the router connected to other network ports. If you want to send information to the outside and be identified on the internal network, you need to configure the public network IP address.

Second, check the port number

1. lsof command

lsof (list open files) is a tool to list open files in the current system. This command can be used for system monitoring, network information viewing, etc., but requires root user authority to execute.

Common commands used to view port numbers include:

# 【按端口查】查询指定端口号情况
lsof -i :端口号
# 【按用户查】查询该用户下的端口情况
lsof -a -u 用户名 -i
# 【按协议类型查】查询IPv4协议有关的端口情况
lsof -i 4
# 【按tcp查】查看所有tcp端口情况
lsof -i tcp
# 【按tcp查】查看tcp端口9001情况
lsof -i tcp:9001
# 【按udp查】查看所有udp端口情况
lsof -i udp
# 【按udp查】查看udp端口121情况
lsof -i udp:121

2. netstat command

The netstat command is used to display the network status, and the Linux server is equipped with this command by default. Common commands used to view port numbers include:

# 查看所有端口情况
netstat -tunplp
# 查看22端口情况
netstat -anp | grep ":22"
netstat -tunplp | grep 22
# 或
netstat -tunplp | grep ":22"
netstat -tunplp | grep 22

3. Connectivity detection command

1. ping command

ping [选项] IP地址或主机名

Detect host IP connectivity, examples are as follows:

[wxx@localhost ~]$ ping 10.129.49.102
PING 10.129.49.102 (10.129.49.102) 56(84) bytes of data.
64 bytes from 10.129.49.102: icmp_seq=1 ttl=64 time=0.042 ms
64 bytes from 10.129.49.102: icmp_seq=2 ttl=64 time=0.053 ms
64 bytes from 10.129.49.102: icmp_seq=3 ttl=64 time=0.063 ms

Please note that the ping command can only detect the connectivity of the host IP, not the port.

The ping command can add some option parameters to meet different detection requirements, such as [-c data packet reception times]; [-s set sending packet size]; [-i set sending cycle] and so on.

2. telnet command

The telnet protocol is a member of the TCP/IP protocol family, and it is the main method and standard protocol for Internet remote login service. It provides users with the ability to do remote host work on their local computer, using the telnet program on the end user's computer to connect to the server.

Through the telnet command, you can test the connectivity of IP and port. The commonly used command format is as follows:

telnet 域名 port
telnet ip port

If you just start using the telnet command, you will get a "-bash: telnet: command not found" error message because the telnet package is not installed.

Next, go install it. It should be noted that telnet is hung under xinetd. It is necessary to confirm whether the xinetd service is installed. The check and installation steps are as follows:

# 检查 telnet 和 xinetd
rpm -qa | grep telnet
rpm -qa | grep xinetd
# 二者都没安装的话,通过yum安装
yum install xinetd telnet telnet-server -y

Here, the following error is encountered:

Error downloading packages:
  1:telnet-server-0.17-66.el7.x86_64: [Errno 256] No more mirrors to try.
  1:telnet-0.17-66.el7.x86_64: [Errno 256] No more mirrors to try.
  2:xinetd-2.3.15-14.el7.x86_64: [Errno 256] No more mirrors to try.

Just replace the image source. How to configure, activate and prevent the firewall from blocking the telnet service will not be described here.

Test example:

telnet 10.129.49.104 9200

Trying 10.129.49.104...
Connected to 10.129.49.104.
Escape character is '^]'.

The last line is a prompt, use ctrl + ] to enter the telnet interactive command line.

Please note that the telnet command is used to log in and manage remote hosts, and is suitable for remote connections of LAN network devices, but it uses plain text to transmit messages, which lacks security. Therefore, in practice, many Linux servers do not open the telnet service, but use the more secure ssh method instead.

3. ssh command

Compared with telnet service, ssh is an encrypted connection, which is more suitable for remote server connection and management.

Used to test the connectivity of IP and port, the command format is as follows:

ssh [选项] 端口号 用户名@ip地址
# 或者
ssh [选项] 端口号 ip地址

Among the options, the commonly used parameters are -v, -p, where -v means to debug and print logs, and -p means to specify the port number.

Use ssh to test the connectivity of the target host, as follows:

ssh -v -p 9200 10.129.49.104

OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 10.129.49.104 [10.129.49.104] port 9200.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /home/wxx/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/wxx/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/wxx/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/wxx/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/wxx/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/wxx/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/wxx/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/wxx/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: ssh_exchange_identification: HTTP/1.0 400 Bad Request


debug1: ssh_exchange_identification: content-type: application/json; charset=UTF-8


debug1: ssh_exchange_identification: content-length: 203


debug1: ssh_exchange_identification: 


ssh_exchange_identification: Connection closed by remote host

It can be seen that the target host has not set up and developed the ssh service, the connection is closed, and it cannot be connected temporarily.

4. curl command

The curl command was used to query the export IP earlier. It is a file transfer tool that uses URL rules to work on the command line. It supports many protocols including HTTP, HTTPS, FTP, etc. It also supports POST, cookies, authentication, downloading part of the file from the specified offset, user agent string, limit Features such as speed, file size, progress bar, etc.

Used to test the connectivity of IP and port, the command format is as follows:

curl ip:port

If the tested port number does not exist, the displayed information is as follows:

curl 10.129.49.104:9002

curl: (56) Recv failure: Connection reset by peer

If the tested IP and port number are normal, the displayed information is as follows:

curl 10.129.49.104:9200
{
  "name" : "node-1",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "iHXx-eIsTTinhm5sY-S9_A",
  "version" : {
    "number" : "7.8.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "757314695644ea9a1dc3tecd26d1a43856725e65",
    "build_date" : "2020-06-14T15:35:50.234039Z",
    "build_snapshot" : false,
    "lucene_version" : "8.5.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

5. wget command

wget is a free software under the GPL license. It supports download methods such as HTTP and FTP, supports proxy servers, and supports functions such as resuming uploads from breakpoints. wget is powerful and easy to use. It is often used to download files, but it can also be used to test the connectivity of the host.

Test host connectivity, the command format is as follows:

wget ip:port

If the tested IP or port number does not exist, it will keep retrying, as shown below:

wget 10.129.49.194:9002
--2023-02-27 16:05:38--  http://10.129.49.194:9002/
Connecting to 10.129.49.194:9002... connected.
HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers.
Retrying.

--2023-02-27 16:05:39--  (try: 2)  http://10.129.49.194:9002/
Connecting to 10.129.49.194:9002... connected.
HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers.
Retrying.

If the tested IP and port number are normal, the displayed information is as follows:

wget 10.129.49.104:9200
--2023-02-27 16:09:27--  http://10.129.49.104:9200/
Connecting to 10.129.49.104:9200... connected.
HTTP request sent, awaiting response... 200 OK
Length: 531 [application/json]
Saving to: ‘index.html’

100%[=========================================================================>] 531         --.-K/s   in 0s      

2023-02-27 16:09:27 (74.7 MB/s) - ‘index.html’ saved [531/531]

The above commands for detecting the connectivity of IP and port are very practical, so hereby summarize and record them.

Guess you like

Origin blog.csdn.net/qq_29119581/article/details/129173756