Open In App

How to display nc return value in Linux shell script?

Last Updated : 24 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

When you need to monitor network connections and understand their status, creating a custom bash script can be an invaluable solution. This article demonstrates how to build a bash script that checks network connections using the nc command and displays the corresponding exit status. By providing a user-friendly menu-driven interface, this script enables users to enter the remote server’s details and port number, allowing them to verify the connection status. The script uses the nc command to establish the connection and then interprets the exit status to determine success or failure. In this guide, you’ll learn how to create this script and execute it step by step, gaining valuable insights into network connection testing and status reporting.

Prerequisites: If we want to display the value that is returned from the nc command execution, then we can create a custom bash script that will perform the connection check and return the exit status. We will print this exit status in the script itself.

  1. Linux Environment: This tutorial assumes you are working in a Linux environment. The provided instructions and script are tailored for Linux systems.
  2. Terminal Access: You should have access to a terminal or command-line interface on your Linux system. Most Linux distributions provide this by default.
  3. nc Command: Ensure that the ‘nc' command (netcat) is installed on your Linux system. It is commonly available on Linux distributions. You can check for its presence by running ‘nc --version' in the terminal. If it’s not installed, you can typically install it using your system’s package manager. For example, on Debian/Ubuntu-based systems, you can install it with:
sudo apt-get install netcat
Screenshot-2023-10-17-121413

Netcat installation

Example:

Choose an option:
1. Check Connection
2. Quit
Enter your choice (1/2):

Input: 1
Enter the remote server: www.geeksforgeeks.org
Enter the remote port: 80

Output:
Connection to www.geeksforgeeks.org (23.201.47.10) 80 port [tcp/http] succeeded!
netcat exit status: 0
Connection to www.geeksforgeeks.org on port 80 was successful.

Here our main task is to write a shell script that uses the nc command to check the connection with the remote server. We need to provide the remote server address along with the port. The nc command will check for the connection and return the exit status. If the connection is successful, then the exit status will be “0”. If the connection is failed, then the exit status will be “1”.

Creating the Script

Step 1: Firstly, we have to create the new file as nc.sh by using any of the text editors available on our system. Once we have created the file, our first task is to create the menu-driven options using the while loop. This will be shown in the loop till the user quits the script.

#!/bin/bash

while true; do
clear
echo "Netcat (nc) Connection Tester"
echo "----------------------------"
echo "1. Check Connection"
echo "2. Quit"
echo -n "Enter your choice (1/2): "

read choice

Step 2: After creating the option menu, we need to handle this option menu with the case block in the bash script. As per the user input, the associated case will be executed. When the user selects the 1. Check the Connection option, then case 1 will be excuted, where the user needs to provide the remote address and the port. The nc command will check for the connection and return the exit status code using the special variable “$?“.

    case $choice in
1)
echo -n "Enter the remote server: "
read remote_server

echo -n "Enter the remote port: "
read remote_port

# Run netcat to connect to the remote server on the specified port
nc -zv "$remote_server" "$remote_port"

# Get the exit status of the netcat command
exit_status=$?

# Display the exit status
echo "netcat exit status: $exit_status"

# Check the exit status and provide feedback
if [ $exit_status -eq 0 ]; then
echo "Connection to $remote_server on port $remote_port was successful."
else
echo "Connection to $remote_server on port $remote_port failed with an exit status of $exit_status."
fi
;;
2)
echo "Goodbye!"
exit
;;
*)
echo "Invalid choice. Please enter 1 or 2."
;;
esac

Steps to create and execute a bash script

Step 1: Open the terminal window using the keyboard shortcut “Ctrl + Alt + T“.

1

Step 2: Using any text editor like Vim, vi, or Nano, open a new blank file in the text editor.

nano nc.sh

2

Step 3: Now, we need to write the below script in the created file nc.sh

#!/bin/bash

while true; do
clear
echo "Netcat (nc) Connection Tester"
echo "----------------------------"
echo "1. Check Connection"
echo "2. Quit"
echo -n "Enter your choice (1/2): "

read choice

case $choice in
1)
echo -n "Enter the remote server: "
read remote_server

echo -n "Enter the remote port: "
read remote_port

# Run netcat to connect to the remote server on the specified port
nc -zv "$remote_server" "$remote_port"

# Get the exit status of the netcat command
exit_status=$?

# Display the exit status
echo "netcat exit status: $exit_status"

# Check the exit status and provide feedback
if [ $exit_status -eq 0 ]; then
echo "Connection to $remote_server on port $remote_port was successful."
else
echo "Connection to $remote_server on port $remote_port failed with an exit status of $exit_status."
fi
;;
2)
echo "Goodbye!"
exit
;;
*)
echo "Invalid choice. Please enter 1 or 2."
;;
esac

echo -n "Press Enter to continue..."
read
done

4-min

Step 4: Now, as we have created the script, we need to make the script executable by running the chmod command in the terminal.

chmod +x nc.sh

3

Step 5: Finally we need to execute the script by using the below command:

./nc.sh

Output 1:

Screenshot-(955)-min

The above screenshot is the execution of the bash script, when the script was executed we selected option 1 to Check the Connection. Then we provided the remote server address along with the port number. The nc command or tool started to check the connection with the remote server address on the port specified. When the connection succeeded the netcat exit status: 0 was returned. This indicated that there was no failure while building the connection with the remote server.

Output 2:

Screenshot-(954)-min

The above screenshot is also the execution of the bash script in this example, we have given the remote address and the port number. The nc command started to build the connection with the remote server on the port specified. While building the connection, the nc command got connection failed as port 121 is not active on the server. As the connection got failed the nc returned the value 1 to indicate the failure while building the connection with he remote server.

Conclusion

In this article, we have seen how we can create the Linux shell script to display the nc return value. We created the menu-driven program, in which the user can provide the remote server address along with the port number. The netcat or nc command checks for the connection, if it gets success then by using the special variable as “$?” the value 0 is been returned as exit status else the value of 1 is been returned in case of failure.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads