LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-21-2020, 07:42 AM   #1
aymenmeer
LQ Newbie
 
Registered: Jan 2020
Posts: 2

Rep: Reputation: Disabled
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
 
Old 01-22-2020, 12:07 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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.
 
Old 01-23-2020, 08:59 AM   #3
aymenmeer
LQ Newbie
 
Registered: Jan 2020
Posts: 2

Original Poster
Rep: Reputation: Disabled
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
 
Old 01-23-2020, 09:13 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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.

Last edited by berndbausch; 01-23-2020 at 05:33 PM.
 
Old 01-26-2020, 08:32 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,817

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
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.
 
1 members found this post helpful.
Old 01-27-2020, 08:39 AM   #6
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,617

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555
Quote:
Originally Posted by MadeInGermany View Post
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'")


Last edited by boughtonp; 01-27-2020 at 08:43 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
'Gender Differences in Twitter Use and Influence Among Health Policy and Health Services Researchers' RandomTroll General 14 12-14-2019 09:07 AM
LXer: Jamaican Ministry of Health is the first to adopt free and open source health system nationwid LXer Syndicated Linux News 0 11-28-2013 07:02 PM
Shell Script To Check The Health of Remote Servers??? Balvinder87 Programming 1 12-21-2012 09:59 PM
LXer: Strategy & Management: Issues and innovations: Our health is your health LXer Syndicated Linux News 0 07-13-2006 05:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration