How to Check Service Ports and Run Commands Like a Pro
As a system administrator, you know that checking service ports and running commands is an essential part of your job. But what happens when you need to run a command that requires a certain port, and that port is not found? In this blog post, we'll show you how to become a pro at checking service ports and running commands, and teach you how to automatically execute a command if a required port is not found.
Checking Service Ports
Before you can run a command that requires a certain port, you need to make sure that the port is open and available. One way to do this is to use the telnet
command. For example, if you want to check if port 80 (the HTTP port) is open on a server with IP address 192.168.1.100, you can use the following command:
1telnet 192.168.1.100 80
If the port is open, you'll see a blank screen. If the port is closed, you'll see an error message. You can use this method to check any port on any server.
Another way to check service ports is to use the netstat
command. For example, if you want to check if port 22 (the SSH port) is open on a server, you can use the following command:
1netstat -an | grep :22
If the port is open, you'll see a line of output that includes the local address, foreign address, and state of the connection. If the port is closed, you won't see any output.
Running Commands
Once you've verified that a required port is open, you can run the command that requires that port. For example, if you want to test a web server on port 80, you can use the curl
command to make an HTTP request:
1curl http://localhost
If the web server is working correctly, you'll see the HTML code for the website. If the web server is not working correctly, you'll see an error message.
Automating Commands
But what happens if a required port is not found? You can automate the process of checking for a port and running a command with a simple Bash script. Here's an example:
1#!/bin/bash
2
3# Set the host and port to check
4host="localhost"
5port="8080"
6
7# Try to connect to the port
8timeout 2 bash -c "</dev/tcp/${host}/${port}" >/dev/null 2>&1
9
10# Check if the port is open
11if [ $? -eq 0 ]; then
12 echo "Port ${port} is open."
13 # Run the command that requires the port
14 command_to_run
15else
16 echo "Port ${port} is closed."
17 # Run a command if the port is not open
18 fallback_command
19fi
In this script, we first set the host and port to check. We then use the timeout
command to try to connect to the port with a timeout of 2 seconds. If the connection is successful, we run the command that requires the port. If the connection is not successful, we run a fallback command. You can replace command_to_run
and fallback_command
with the actual commands that you want to run.
Now you can also add the same in crontab:
Type be command to edit crontab:
1
2crontab -e
then right:
1
2* * * * * /home/auto-script-path/check-service.sh
Note: You have to provide execute permission to the script.
Conclusion
Checking service ports and running commands are essential tasks for system administrators. By mastering these skills and automating the process of checking for ports and running commands, you