📜  archlinux free used port - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:39:19.528000             🧑  作者: Mango

ArchLinux Free Used Port - Shell Script

When using ArchLinux for development or server management, it's important to know which ports are being used to avoid conflicts. This shell script provides an easy way to find free and used ports on your ArchLinux system.

Installation Instructions
  1. Open the terminal on your ArchLinux system.
  2. Create a new file with the name free-ports.sh using your favorite text editor.
  3. Copy and paste the shell script code below into the new file.
  4. Save the file and close the text editor.
  5. Make the shell script executable with the following command:
    chmod +x free-ports.sh
    
  6. Run the shell script with the following command:
    ./free-ports.sh
    
Shell Script Code
#!/bin/sh
echo "Free ports:"
for i in $(seq 49152 65535); do
    if ! ss -ln | grep -q ":$i "; then
        echo "$i"
    fi
done

echo "Used ports:"
ss -ln | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]" | sort -u
Explanation

The shell script starts by looping through port numbers 49152 to 65535 using the seq command. For each port, it runs the ss -ln command to check if the port is being used or not. If the port is not being used, it is considered a free port and printed to the terminal.

Next, the shell script runs the ss -ln command again and uses awk and cut to extract the port numbers for all listening sockets. The grep command is used to filter out any non-numeric characters, and the sort command is used to remove duplicates. The resulting list shows all used ports on the system.

Overall, this shell script provides a quick and easy way to find free and used ports on your ArchLinux system.