Bash script that extract system information
Create a bash script that extract system informaion and generate a summary.
Introduction
The information gathered from commands usually contains too much information, making it hard for human eyes to read. Therefore, making the output clean and human-readable is also one of the focuses of this script.
First, the script will gather network information such as public IP address - the address connected to the internet, private IP address - the address in the machine’s network, and MAC address (a.k.a physical address) - the unique identifier assigned to network interfaces for communications on the physical network segment.
Second, it will gather system information such as processes - to determine which process consumes the most CPU resources, services - to collect all installed services on the machine, and memory - to gather the system’s memory usage.
Last but not least, the file system. The script can find the largest file in the system with the proper permissions provided.
Upon completion of the data collection process, all the gathered information will be meticulously analyzed and synthesized. The results of this analysis will then be compiled into a comprehensive summary, which will be prominently displayed after the script.
Methodology
Public IP address
There are many ways to get the public IP of your network, one of the simple ways to get public IP through a terminal is by using curl.
- -s: Flag that indicates the command runs in silence mode.
But there is a possibility that there is no internet connection when you run the script. In that case, this command will not output any result.
This problem can be solved by adding a check for the output of the curl command.
Private IP address
To get our private IP address, we can utilize ifconfig command to output the information. However, some services might generate a new network interface and assign a private IP address for it.
The objective of my script is to get the private IP address that connected to the internet. In order to get that, we can use ip route command to route our device to a public server (I used Google IP, but can use any IP as long as that IP is routed to the internet) to determine which private IP are used to route to the internet.
- o: one-line output
- route get to: get the actual kernel route to a destination
- 8.8.8.8: Google IP, but can use any IP as long as that IP is routed to the internet
- 2>/dev/null: hide the error output
The internal IP is the ip after src, we then extract the IP from the previous output with awk '{print $(NF-4)}'
Finally, add a check to prevent empty output caused by the device not connected to any network.
MAC address
Similar to the way to get the internal IP, we will use a similar command to get the network interface that is connected to the internet.
Once we have the network interface that is connected to the internet, we are able to get that MAC address with the network interface by using ip link show command.
But what if none of the NICs is connected to the internet? This command will not output anything.
As I mentioned in “Private IP address”, some services might generate a new network interface and assign a private IP address for it. This applied to MAC address also. Those services will generate a randomized MAC address to those network interfaces. Without knowing which interface is connecting the internet, I decided to gather all the MAC addresses.
With ip link show command, we are able to get all the network interfaces together with their MAC address.
As we can see from the output, those network interfaces that can connect to a network are labeled as “ether”. From the research I get from the internet, wireless network interfaces are labeled as “HWaddr”. So we can filter out other output by focusing on “ether” and “HWaddr” labels.
At the end, I use awk to format the output.
Top 5 processes
To capture the top 5 processes when the script runs, we will use ps command. But it not output all the running processes, only the processes are run by me and have a terminal with it.
So we apply a few flags to the command.
- -e: will list all processes
- -o: specific the format of output
- –sort: specific sort with which column
- head -n6: This command is specific to show only the top 5 of the result and includes the header.
Memory usage
Linux has one powerful command to display the memory usage of the device, free command. This command will output memory in RAM and Swap. Swap is a temporary data file that is stored on the hard drive when the machine runs out of RAM.
But the memory is displayed in byte format. It is actually hard to read for human eyes. So I added -h flag to indicate the command to display in the form more human readable.
I also want to display the today of the memory of used and free. Instead of calculate by ourselves, just add -t flag and an additional total output will calculate it for us.
Active services
To get all running services, we will use service command with —status-all flag to list all services.
But —status-all will list all services included inactive one, so we use grep to filter out inactive services.
Then we create a for loop to display the status of each service.
Top 10 largest files
We can use find command to find all the files in the /home directory and format the output with the files’ size information.
- -type f: flag for find command to output file result only
- -printf: flag to format the output
Then use sort to to get the largest file from the output of find command, and get the top 10 results.
- -r: Flag that is used to reverse the sort result, and make the output in descending order.
- -n: Indicate the sort to compare numerical value, which is the size from our example.
Finally, format the output with awk to make it more readable, by converting the size from byte to MB.
Additionally, normally the /home directory contains folders that own by other users, find command might not have enough permission to search through other folder that beyond your user’s permission, unless you run it under sudo.
Summary
At the end of the script, it is good to have a summary of all the information gathered by this script. I create a variable to store all information and output it at the end of the action.
Conclusion
We created a script that was able to gather all the necessary information about a machine. Network information like public IP address, private IP address, and MAC address of the NICs. System information like running processes, memory usage, and active services and their status. Search through the /home folder to discover which file has the largest size.
By using this information, we can better defend our machine by checking if there are any malicious services running on the machine, or detecting suspicious large files that might contain viruses.
Last but not least, all information gathered by this script is not all the information we can gather from a machine. This script can be enhanced to be more powerful and gather more valuable information to defend the machine.
Reference
- How to get the primary IP address of the local machine on Linux and OS X? (n.d.). Stack Overflow.
- Introduction to if. (n.d.).
- Check if a string matches a regex in Bash script. (n.d.). Stack Overflow.
- How to find your wireless MAC address. (n.d.). Weill Cornell Medicine.
- How to sort a text file by column and keep the original order. (n.d.). Stack Overflow.
- How to sort lines in text files in Linux sort Command. GeeksforGeeks.
- Show top five CPU consuming processes with ps
- ps(1) Linux manual page
- ps show column name
- Classic SysAdmin: Linux 101: 5 Commands for Checking Memory Usage in Linux - Linux Foundation. (2022, September 13).
- Swap Space in Linux: What It Is & How It Works. Knowledge Base by phoenixNAP.
- How to check if user has sudo privileges inside the bash script? (n.d.). Stack Overflow.