How to connect to your server via SSH and check available resources using a shell script?

This simple Linux ssh connection shell script can be useful if you need to connect to and check resources on several remote servers.

This script was written to check the resources of several CentOS based servers.

 

Prerequisites:
  • Application sshpass must be installed on your system.

 

Summary:
  1. SSH connection shell script
  2. Usage.

 

1. SSH connection shell script.

Here is the content of the script. You can review and edit it according to your needs. The current example of this bash shell script automates the connection to the remote server and lists information about the hostname, CPU, HDD, RAM on the server.

 

Script:

#!/bin/bash
##################################################################
# Check Server Script v0.1 by TinyTumbleweed.com
##################################################################
# How to use? ./script_name [ip address] [password]
##################################################################
# Connects to a server via SSH
# Checks server Hostname
# Checks CPU type
# Checks HDD size
# Checks RAM amount
##################################################################
clear;
echo “############################################################”;
echo “# server: $1” “password: $2”;
echo “############################################################”;
sshpass -p $2 ssh -o StrictHostKeyChecking=no root@$1 ”
cat /etc/hostname;
head /proc/cpuinfo | grep ‘model name’;
fdisk -l | grep “Disk” | head -n 1;
free -m | grep “Mem:”;

2. Usage.

Here is an example of the script usage. Replace the [ip address] with the IP address or domain and [password] with of the password of the remote server.

Command: sh /script_name [ip address] [password]

 

Output:

The following commands display the information about the remote server hostname, CPU, HDD, RAM. 

Note: Different OS distributions may have different tools of displaying this information, therefore, you might need to edit the commands according to your OS distribution.

cat /etc/hostname;
head /proc/cpuinfo | grep “model name”;
fdisk -l | grep “Disk” | head -n 1;
free -mh | grep “Mem:”;

The sshpass command in the following line will insert the password into the console, while the ssh -o StrictHostKeyChecking=no will accept the SSH key. You can replace the commands from this example script with any other required commands by placing them in the double quotes.

sshpass -p $2 ssh -o StrictHostKeyChecking=no root@$1 " "

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.