Advanced Port Access with Curl
Now that you understand the basics of using Curl with different ports, let's explore more advanced techniques for port access and testing.
Testing Port Availability
Curl can be used to check if a specific port is open on a server. When a port is open, Curl will attempt to establish a connection and potentially receive data. When a port is closed, Curl will report an error.
Let's test some common ports:
## Test if port 80 (HTTP) is open
curl -s -o /dev/null -w "%{http_code}\n" http://example.com:80
This command will display the HTTP status code (typically 200 if the port is open and the server is responding correctly). The -s
flag makes Curl silent, -o /dev/null
redirects the output to nowhere, and -w "%{http_code}\n"
prints just the HTTP status code.
Let's try a few more ports:
## Test if port 443 (HTTPS) is open
curl -s -o /dev/null -w "%{http_code}\n" https://example.com:443
This should return 200
or another HTTP status code, indicating that port 443 is open.
Now, let's test a port that is likely closed:
## Test if port 81 (uncommon) is open
curl -s --connect-timeout 5 http://example.com:81
This command will likely fail with an error message like:
curl: (7) Failed to connect to example.com port 81: Connection refused
or it might time out after 5 seconds (as specified by --connect-timeout
):
curl: (28) Connection timed out after 5001 milliseconds
Accessing FTP Servers
Curl supports multiple protocols, including FTP. Let's see how to access an FTP server:
curl ftp://ftp.gnu.org/gnu/
This command lists the contents of the directory at ftp.gnu.org. The output might look like:
drwxr-xr-x 8 1003 1003 4096 Dec 16 2020 0ad
drwxr-sr-x 5 1003 1003 4096 Nov 09 2020 8sync
drwxr-xr-x 2 1003 1003 4096 Jun 05 2015 GNUinfo
drwxr-xr-x 3 1003 1003 4096 Jan 23 2022 GNUnet
...
Creating a Simple Port Scanner
Let's create a simple Bash script to scan a range of ports on a server. Create a new file called port_scanner.sh
in your project directory:
nano ~/project/port_scanner.sh
Add the following content to the file:
#!/bin/bash
## Simple port scanner using curl
## Usage: ./port_scanner.sh <hostname> <start_port> <end_port>
hostname=$1
start_port=$2
end_port=$3
echo "Scanning ports $start_port to $end_port on $hostname..."
echo
for port in $(seq $start_port $end_port); do
## Try to connect with a short timeout
curl -s --connect-timeout 1 "$hostname:$port" > /dev/null
## Check if the connection was successful
if [ $? -eq 0 ]; then
echo "Port $port is OPEN"
else
echo "Port $port is closed"
fi
done
echo
echo "Scan complete!"
Save the file by pressing Ctrl+X
, then Y
, and then Enter
.
Make the script executable:
chmod +x ~/project/port_scanner.sh
Now, run the script to scan ports 80-85 on example.com:
~/project/port_scanner.sh example.com 80 85
The output will show which ports are open and which are closed:
Scanning ports 80 to 85 on example.com...
Port 80 is OPEN
Port 81 is closed
Port 82 is closed
Port 83 is closed
Port 84 is closed
Port 85 is closed
Scan complete!
This simple script demonstrates how Curl can be used as a basic port scanning tool. In a real-world scenario, you would want to use specialized tools like nmap
for more comprehensive network scanning, but this example shows the versatility of Curl.