LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   remote servers script to check health (https://www.linuxquestions.org/questions/linux-server-73/remote-servers-script-to-check-health-4175668138/)

aymenmeer 01-21-2020 07:42 AM

remote servers script to check health
 
hello all I have a script which lets you see server states but which runs locally I want to make it executable remotely for several servers from a Txt file where there are IP addresses

Code:

#!/bin/bash
 
out_file_path=//home/aymenmeer/Bureau/sysinfo.txt

echo -e "******************************************* SYSTEM INFO ******************************************" >> $out_file_path
echo -e "**************************************************************************************************\n" >> $out_file_path
echo -e "********************************* Gemalto Maintenance Tools ****************************************" >> $out_file_path
echo -e "*********************************End of Execution*************************************************\n"
>> $out_file_path
echo -e "\n\n========================================= HOST INFO ==========================================\n" >> $out_file_path
echo -e "\tHostname:\t"`hostname` >> $out_file_path
 
echo -e "\n\n========================================== OS INFO ===========================================\n" >> $out_file_path
echo -e "\tOS Info:\t"`cat /etc/system-release` >> $out_file_path
 
echo -e "\n\n======================================== KERNEL INFO =========================================\n" >> $out_file_path
echo -e "\tKernel Version:\t"`uname -r` >> $out_file_path
 
echo -e "\n\n========================================= CPU INFO ===========================================\n" >> $out_file_path
echo -e "\tTotal Processor:\t"`grep -c 'processor' /proc/cpuinfo` >> $out_file_path
echo -e "\tCPU Processor Model:\t"`awk -F':' '/^model name/ { print $2 }' /proc/cpuinfo` >> $out_file_path
echo -e "\tCPU Processor Speed:\t"`awk -F':' '/^cpu MHz/ { print $2 }' /proc/cpuinfo` >> $out_file_path
echo -e "\tCPU Cache Size:\t"`awk -F':' '/^cache size/ { print $2 }' /proc/cpuinfo`  >> $out_file_path
 
echo -e "\n\n========================================== RAM INFO ==========================================\n" >> $out_file_path
echo -e "\tMemory(RAM) Info:\t"`free -mht| awk '/Mem/{print " \tTotal: " $2 "\tUsed: " $3 "\tFree: " $4}'`  >> $out_file_path
echo -e "\tSwap Memory Info:\t"`free -mht| awk '/Swap/{print " \t\tTotal: " $2 "\tUsed: " $3 "\tFree: " $4}'` >> $out_file_path
 
echo -e "\n\n========================================== IP INFO ===========================================\n" >> $out_file_path
ifconfig >> $out_file_path
 
echo -e "\n\n====================================== ROUTE TABLE INFO ======================================\n" >> $out_file_path
route -n >> $out_file_path
 
echo -e "\n\n====================================== MOUNT POINT INFO ======================================\n" >> $out_file_path
cat /etc/fstab|grep -v "#" >> $out_file_path
 
echo -e "\n\n==================================== DISK PARTATION INFO =====================================\n" >> $out_file_path
df -h >> $out_file_path
 
echo -e "\n\n==================================== PHYSICAL VOLUME INFO ====================================\n" >> $out_file_path
pvs >> $out_file_path
 
echo -e "\n\n===================================== VOLUME GROUP INFO ======================================\n" >> $out_file_path
vgs >> $out_file_path
 
echo -e "\n\n===================================== LOGICAL VOLUME INFO ====================================\n" >> $out_file_path
lvs >> $out_file_path
 
echo -e "\n\n==================================== RUNNING SERVICE INFO ====================================\n" >> $out_file_path
systemctl list-units | grep running|sort >> $out_file_path
 
echo -e "\n\n==================================== TOTAL RUNNING SERVICE ===================================\n" >> $out_file_path
echo -e "\tTotal Running service:\t"`systemctl list-units | grep running|sort| wc -l` >> $out_file_path
 
echo -e "\n\n========================================= GRUB INFO ==========================================\n" >> $out_file_path
cat /etc/default/grub >> $out_file_path
 
echo -e "\n\n========================================= BOOT INFO ==========================================\n" >> $out_file_path
ls -l /boot|grep -v total >> $out_file_path
 
echo -e "\n\n====================================== ACTIVE USER INFO ======================================\n" >> $out_file_path
echo -e "\tCurrent Active User:\t"`w | cut -d ' ' -f 1 | grep -v USER | sort -u` >> $out_file_path


berndbausch 01-22-2020 12:07 AM

Assuming TXTFILE contains the addresses and YOURSCRIPT is your script. First, ensure your ssh key is on all the servers:
Code:

ssh-keygen            # only needed if you have no key
for address in $(cat TXTFILE)
do
    ssh-copy-id $address
done

This requires you entering the password at each remote server. It's not required, but it allows you to log on to the servers in the future without entering a password.

Next, copy the script to the servers. For example:
Code:

for address in $(cat TXTFILE)
do
    scp YOURSCRIPT ${address}:
    ssh chmod +x ${address}:YOURSCRIPT
done

At any time execute it:
Code:

for address in $(cat TXTFILE)
do
    ssh ${address}:YOURSCRIPT
done

There are more sophisticated ways to do this. Ansible comes to mind, but it requires more preparation and has a certain learning curve.

aymenmeer 01-23-2020 08:59 AM

hello tnakyou for your reply when i test your code i have this error "ssh: Could not resolve hostname 192.168.3.29:list.txt: Name or service not known" can you help me to resolve them.

thankyou

berndbausch 01-23-2020 09:13 AM

It's a typo, sorry. I suggest you learn about ssh.

Try
Code:

ssh 192.168.3.29 list.txt
Or you may have to use the absolute pathname of the script, such as
Code:

ssh 192.168.3.29 /home/WHATEVERYOURUSERNAMEIS/list.txt
Can't check it right now.

EDIT: I checked it, and in my environment, the latter solution works. The former doesn't.

MadeInGermany 01-26-2020 08:32 PM

You do not need to scp and chmod your script.
Directly pass+run the script to/on the remote host:
Code:

for address in $(grep '^[^#]' TXTFILE)
do
    ssh "$address" /bin/bash -s < YOURSCRIPT
done

The grep skips empty lines and #comments.
The -s allows script arguments to follow.

boughtonp 01-27-2020 08:39 AM

Quote:

Originally Posted by MadeInGermany (Post 6083291)
The grep skips empty lines and #comments.

That's not a completely accurate description - that grep outputs lines starting with a non-# character, which allows the maintainers of the IP address file to comment lines using hashes if they put such comments on a line by themselves, and not as suffixes to the ip addresses.

Allowing comments on their own line and as suffixes can be achieved with:
Code:

for address in $(sed 's/#.*//' TXTFILE)
...

(There's no need to specifically remove blank lines in this example, since the for loop word expansion deals with that already, but (for reference) it can be achieved by adding "/^$/d" to the sed script, giving "sed 's/#.*//;/^$/d'")



All times are GMT -5. The time now is 04:16 AM.