Advertisement

Shell Script Code For System Information Script

Linux Shell Script For System Information Script

System Information Script 

#!/bin/bash
output_file="system_info.txt"
# Gather system information and save to a file
echo "System Information:" > "$output_file"
echo "-------------------" >> "$output_file"
echo "Hostname: $(hostname)" >> "$output_file"
echo "OS: $(uname -a)" >> "$output_file"
echo "Memory: $(free -h)" >> "$output_file"
echo "Disk Space: $(df -h)" >> "$output_file"
echo "System info saved to $output_file."

Shell script to gather system information can be quite useful for system administrators, troubleshooting, or monitoring purposes.

#!/bin/bash

# Function to gather system information
get_system_info() {
    echo "System Information:"
    echo "-------------------"
    echo "Hostname: $(hostname)"
    echo "Manufacturer: $(sudo dmidecode -s system-manufacturer)"
    echo "Product Name: $(sudo dmidecode -s system-product-name)"
    echo "Version: $(sudo dmidecode -s system-version)"
    echo "Serial Number: $(sudo dmidecode -s system-serial-number)"
    echo "Processor: $(grep -m 1 "model name" /proc/cpuinfo | cut -d ':' -f 2 | sed 's/^[ \t]*//')"
    echo "Architecture: $(uname -m)"
    echo "Kernel Version: $(uname -r)"
    echo "Uptime: $(uptime -p)"
    echo "Logged In Users:"
    who
    echo
    echo "Memory Usage:"
    free -h
    echo
    echo "Disk Usage:"
    df -h
    echo
    echo "Network Interfaces:"
    ip a
    echo
    echo "List of Processes:"
    ps aux --sort -%cpu,%mem
}

# Execute function to get system information
get_system_info

Explanation:

  1. Shebang: #!/bin/bash specifies that this script should be interpreted using the Bash shell.

  2. get_system_info Function: Gathers various system information using built-in Linux commands:

    • hostname: Prints the system hostname.
    • sudo dmidecode -s ...: Retrieves system manufacturer, product name, version, and serial number using dmidecode (requires sudo for some systems).
    • grep -m 1 "model name" /proc/cpuinfo | cut -d ':' -f 2 | sed 's/^[ \t]*//': Gets processor model from /proc/cpuinfo.
    • uname -m: Prints the system architecture.
    • uname -r: Displays the kernel version.
    • uptime -p: Shows system uptime.
    • who: Lists currently logged in users.
    • free -h: Displays memory usage in a human-readable format.
    • df -h: Shows disk usage in a human-readable format.
    • ip a: Lists network interfaces and their configurations.
    • ps aux --sort -%cpu,%mem: Provides a list of running processes sorted by CPU and memory usage.
  3. Execution: Calls the get_system_info function to gather and display all system information.

Usage:

  • Save this script to a file, e.g., system_info.sh.
  • Make it executable with chmod +x system_info.sh.
  • Run the script with ./system_info.sh.

Notes:

  • Customization: Modify the script to include additional information or customize the output format according to your specific needs.

  • Security: Some commands (dmidecode, sudo) may require appropriate permissions or adjustments depending on your system configuration. Ensure you have necessary privileges to execute them.

  • Error Handling: This script assumes basic error handling. You may want to add more robust error checking and reporting based on your specific requirements.

This script provides a basic framework for gathering and displaying essential system information in a Unix-like environment using standard Linux commands. Depending on your needs, you can expand upon it to include more detailed monitoring, logging, or integration with other system management tools.



Post a Comment

0 Comments