Something that I like in Linux (and in Unix-like systems in general) is that configurations and properties are contained in text files. This allows an administrator with the right permissions to examine the files and make changes if required. Text files are also simple and convenient data sources for a sysadmin's typical operations. In certain situations, you can use text files as an output to be shared with regular users as well. I cover examples of both cases in this article.
Note: Writing to stdout and reading from stdin using pipes is like using a virtual text file. In many cases, you do have a text file, but in others, you simply use the output of some previous command as if it were a text file.
[ You might also enjoy: My 8 favorite practical Linux commands ]
So, when should you use one method or the other? Well, in some cases, the text file already exists, like the /etc/hosts
file, for instance. In other cases, you don't need to have the file physically written because you're only interested in the result (and the data structure is really simple). Different scenarios might require that you store the information to a file for reasons such as clarity, troubleshooting, auditing, or being able to analyze the information's structure and other types of content that are there.
Check the reachability and name resolution for a list of servers
Suppose you have a list of new machines and need to verify that they:
- Are reachable from your server
- Have name resolution working for them
- Are listening on port 22 (for SSH)
You also need to report the status to the project team for which you're required to submit in a spreadsheet format. If you're working with dozens of servers and you need to repeat these tests on different days, it is definitely useful to think about an automated way of doing so.
The input file
This is the spreadsheet where I got the CSV (comma-separated value) file used in the following examples.
And this is the CSV file:
ServerName,IP
m2.example.com,192.168.2.99
xtower.example.com,192.168.2.111
win2k16.example.com,192.168.101.41
control.example.com,192.168.101.200
node1.example.com,192.168.101.201
node2.example.com,192.168.101.202
node3.example.com,192.168.101.203
node4.example.com,192.168.101.204
node5.example.com,192.168.101.205
(Converting the spreadsheet to/from CSV was done manually and will not be covered in this article.)
The script
The following is the script I use to test the servers:
1 #!/bin/bash
2
3 input_file=hosts.csv
4 output_file=hosts_tested.csv
5
6 echo "ServerName,IP,PING,DNS,SSH" > "$output_file"
7
8 tail -n +2 "$input_file" | while IFS=, read -r host ip _
9 do
10 if ping -c 3 "$ip" > /dev/null; then
11 ping_status="OK"
12 else
13 ping_status="FAIL"
14 fi
15
16 if nslookup "$host" > /dev/null; then
17 dns_status="OK"
18 else
19 dns_status="FAIL"
20 fi
21
22 if nc -z -w3 "$ip" 22 > /dev/null; then
23 ssh_status="OK"
24 else
25 ssh_status="FAIL"
26 fi
27
28 echo "Host = $host IP = $ip" PING_STATUS = $ping_status DNS_STATUS = $dns_status SSH_STATUS = $ssh_status
29 echo "$host,$ip,$ping_status,$dns_status,$ssh_status" >> $output_file
30 done
The following line items explain the script entries above:
Line 6: Initialize the output file with the header and three new fields to represent the status of reachability via ping
and name resolution
Line 8: Read input file line by line using a while
loop, ignoring the first line (header). It also creates the variables for host and ip, extracting the values using the separator (comma) and ignoring the rest.
Lines 10 to 26: Run the ping
, nslookup
, and nc
commands, sending the output to null because we are just interested in the return status from the commands
Line 28: Send the output to stdout for the person running the script
Line 29: Send the data to the output file with the three new columns (ping_status, dns_status, and ssh_status)
Open the output file as a spreadsheet
Send the output file to a workstation where you can open it in your favorite spreadsheet application. If you're sending this to non-technical people, you might want to save it in the default spreadsheet format used in the company to make their life easier.
[ Free cheat sheet: Get a list of Linux utilities and commands for managing servers and networks. ]
Wrap up
In this article, I applied some common and simple tools that are available on all Linux systems to automate the test for reachability, name resolution, and connectivity via SSH. In some projects, this type of validation would have to be done for dozens of servers and repeated a significant number of times due to the change processes that involve other teams (Network and Firewall, for example).
The principles can be extended to other types of tests. For example, you could test connectivity for a different port. If the tests become more complex, such as run a command on the host if it is reachable via SSH, then you're dealing with a different type of problem, which requires a different tool. And for a situation like that, I strongly recommend that you look at Ansible.
About the author
Roberto Nozaki (RHCSA/RHCE/RHCA) is an Automation Principal Consultant at Red Hat Canada where he specializes in IT automation with Ansible. He has experience in the financial, retail, and telecommunications sectors, having performed different roles in his career, from programming in mainframe environments to delivering IBM/Tivoli and Netcool products as a pre-sales and post-sales consultant.
Roberto has been a computer and software programming enthusiast for over 35 years. He is currently interested in hacking what he considers to be the ultimate hardware and software: our bodies and our minds.
Roberto lives in Toronto, and when he is not studying and working with Linux and Ansible, he likes to meditate, play the electric guitar, and research neuroscience, altered states of consciousness, biohacking, and spirituality.
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech
Products
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud services
- See all products
Tools
- Training and certification
- My account
- Customer support
- Developer resources
- Find a partner
- Red Hat Ecosystem Catalog
- Red Hat value calculator
- Documentation
Try, buy, & sell
Communicate
About Red Hat
We’re the world’s leading provider of enterprise open source solutions—including Linux, cloud, container, and Kubernetes. We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.
Select a language
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit