It's time to write a script and this time we will create a script that pings a remote host and checks whether a certain port is opened or not. The result is mailed to user's email ID and displayed on the screen at the same time.
- Create a variable 'Host' to store the details of the host to be pinged (Name or it's IP address).
- Create another variable 'Port' to store the port number to be checked.
- Create variables 'email_id' and 'email_sub' to store email ID of the user and the subject of the email respectively.
- When script is executed, it will expect the user to enter two arguments, Host and Port Number. If the user does not enter these entities, flash an error message on the screen.
- Ping the 'Host' and store the result in a variable called 'result_host'.
- Use nc command to check whether the 'Port' is opened or not. Store the result in a variable 'result_port'.
- Combine there two results to create a message 'mesg' which will be dispatched to 'email_id' using mail command and displayed on the terminal screen.
sudo apt-get install mailutils