📅  最后修改于: 2023-12-03 14:39:19.528000             🧑  作者: Mango
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.
free-ports.sh
using your favorite text editor.chmod +x free-ports.sh
./free-ports.sh
#!/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
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.