Open In App

Run a Command Conditionally with netcat and grep

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore the combination of Netcat (nc) and Grep commands in a Unix-like environment to execute commands conditionally based on specific search criteria. Netcat enables network communication, serving as a tool for sending and receiving data across networks. Meanwhile, Grep excels at pattern matching within textual data. By integrating these commands, users can efficiently filter and process network data, running commands selectively based on the results of pattern matching. This article explores a script demonstrating the process to run a command conditionally with netcat and grep.

Creating the Script

Step 1: Set Target Host and Port

In this step, variables host and port are initialized with the target host and port values. Users should replace “localhost” and “80” with their specific web server details.

# Replace "localhost" and "80" with your target host and port
host="localhost"
port="80"

Step 2: Check if the Web Server is Running

Here, the nc -zv $host $port command checks the availability of the web server at the specified host and port. The output is then piped to grep to search for the keyword “succeeded“. If the keyword is found, it indicates that the web server is running, and the script prints a corresponding message. Otherwise, it notifies that the web server is not running.

# Check if the web server is running
if nc -zv $host $port 2>&1 | grep -q "succeeded"; then
echo "Web server is running on $host:$port"
else
echo "Web server is not running on $host:$port"
fi

Step 3: Create a Simple Web Server

To run a web server in the background on Ubuntu, we can use Nginx web server. To install the web server, execute the below command in the terminal.

sudo apt update
sudo apt install nginx

Once the installation is completed, to start the server, execute the below command.

sudo service nginx start

To check the status of the Nginx service, you can use the systemctl command:

sudo systemctl status nginx

Now how we had one page for testing on http://localhost:80.

Ngnix Web Server running

Steps to create and execute the bash script

Step 1: Create Script File

Open the terminal window using the keyboard shortcut “Ctrl + Alt + T“. Using any text editor like vim, vi, or nano, open a new blank file in the text editor.

vim web_server.sh

Step 2: Write Script Code

Once the file is been created, write the below script code into the file.

# Replace "localhost" and "80" with your target host and port
host="localhost"
port="80"

# Check if the web server is running
if nc -zv $host $port 2>&1 | grep -q "succeeded"; then
echo "Web server is running on $host:$port"
else
echo "Web server is not running on $host:$port"
fi

Step 3: Make the Script Executable

Use the chmod command to make the script executable. This command grants execute permission to the script.

chmod +x web_server.sh

Step 4: Run the Script

Execute the script by typing by executing the below command. Command runs the script and displays the output in the terminal.

./web_server.sh

Output:

Output

The script will check if a web server is running on localhost at Port 80. The script will print a message indicating whether the web server is running or not based on the success or failure of the connection attempt.

Conclusion

In conclusion, this article explores the integration of Netcat (nc) and Grep commands in Unix-like environments for conditional command execution based on specific search criteria. The script presented leverages these commands to check the availability of a web server on a specified host and port. Additionally, the article provides clear steps for creating a simple web server using Nginx on Ubuntu and outlines a step-by-step guide for writing, making executable, and running the bash script. This combination of commands proves valuable for efficiently managing and monitoring network-related tasks in a Unix environment.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads