The way to use a Bash script for-loop for system administration in Linux

Bash scripting is each Linux administrator’s Swiss Military knife. Learn the way utilizing a for-loop along with Bash scripts can produce highly effective outcomes.

Picture: jivacore/Shutterstock

Bash scripting stays a staple of my endeavors in Linux as a system administrator, and I take advantage of it day by day to ease my workload. One among my mostly used sort of shell scripts entails a for-loop which processes and applies instructions to servers primarily based on a textual content file I’ve created containing the goal host names. 

SEE: Kubernetes: A cheat sheet (free PDF)  (TechRepublic)

These scripts can copy recordsdata to servers or log into these methods through ssh to run numerous instructions. This course of is finest utilized with
SSH host keys in place

to allow passwordless entry to focus on methods in any other case you will must sort a password in for every system to which your script will join. You have to have the suitable permissions to run these instructions as properly.

It can’t be overstated that your checklist of goal hosts have to be correct and solely include methods upon which these instructions ought to be run, in any other case you’ll be able to in a short time wreak havoc by deleting recordsdata or rebooting hosts which should not be on this checklist. Rebooting manufacturing methods will be what we in IT name a CEE—career-ending occasion.

Be aware that these scripts should include a .sh extension and be set to executable utilizing chmod +x (script title). If the file title containing the checklist of servers just isn’t in the identical listing you are working the script from you will must specify the total path within the script. I normally run my scripts from a subfolder beneath “root.”

Right here is an instance of considered one of my easy scripts, “besrestart.sh.” This script is meant to start out the besclient service (BigFix, used for system patching) on all of the hosts listed in a textual content file known as “servers”:

for l in `cat servers`; do echo $l;ssh -q $l service besclient restart;echo $l; performed

The script makes use of “for I” as in a lowercase L (to not be confused with i or I), which means to cat the servers textual content file and return one-by-one every host title to make use of to conduct the instructions following the semi-colon. “I” merely refers to every host title entry. Every semi-colon separates the instructions within the chain.

Be aware that ` just isn’t a single quote; it’s the “again quote” character and shares the identical key because the tilde (~) within the higher left of a typical keyboard.

“do echo $l” will output the host title earlier than it runs the subsequent command.

Now comes the nitty-gritty, the place ssh -q $l tells the script to ssh to the goal host. It is now on the distant field and may kick off the “service besclient restart” command.

The “echo $I” shows the hostname concerned as a approach of confirming the command ran efficiently. 

SEE: From begin to end: The way to deploy an LDAP server (TechRepublic Premium)

Lastly, “performed” ends the command chain and strikes on to the subsequent system, or finishes working the script if there are not any extra methods to work on.

For example your servers file incorporates this stuff:

server1
server2
server3
server4
server5

After the script runs you’d see output much like the next, assuming the system may connect with all of the targets.

server1
server1
server2
server2
server3
server3
server4
server4
server5
server5

When you see a single entry for a bunch title, verify to make sure connectivity to that host is working and take a look at once more. 

Right here is one other pattern script which connects to a system, stops a service, deletes a file, copies a brand new file to the system through scp, then begins the associated service.

for l in `cat servers`; do echo $l;ssh -q $l service besclient cease;echo $l; performed 
for l in `cat servers`; do echo $l;ssh -q $l rm -rf /var/decide/BESClient/besclient.config;echo l; performed 
for l in `cat servers`; do scp besclient.config $l:/var/decide/BESClient/;echo $l; performed 
for l in `cat servers`; do echo $l;ssh -q $l service besclient begin;echo $l; performed

In no specific order, listed below are the principle scripts I depend upon for each day duties:

This script can cease a service, take away the associated rpms after which delete the app listing to wash up goal methods:

for l in `cat servers`; do echo $l;ssh -q $l service besclient cease;echo $l; performed 
for l in `cat servers`; do echo $l;ssh -q $l rpm -e BESAgent-9.5.10.79-rhe6.x86_64;echo $l; performed 
for l in `cat servers`; do echo $l;ssh -q $l rpm -e BESAgent-9.5.3.211-rhe5.x86_64;echo $l; performed 
for l in `cat servers`; do echo $l;ssh -q $l rm -rf /and so forth/decide/BESClient /var/decide/BESClient /decide/BESClient;echo $l; performed 

This script gathers some {hardware} particulars from the goal hosts which will be helpful for inventorying methods:

 for l in `cat servers`; do echo $l;ssh -q $l dmidecode | grep -i serial;echo $l; performed
 for l in `cat servers`; do echo $l;ssh -q $l lscpu | grep "CPU(s):";echo $l; performed
 for l in `cat servers`; do echo $l;ssh -q $l lscpu | grep "Core(s) per socket:";echo $l; performed
 for l in `cat servers`; do echo $l;ssh -q $l lscpu | grep "Socket(s):";echo $l; performed 
 for l in `cat servers`; do echo $l;ssh -q $l free -h;echo $l; performed 

This script copies one other script to focus on methods /and so forth/cron.each day folders so these scripts will probably be executed each 24 hours:

 for l in `cat servers`; do ssh -q $l /and so forth/cron.each day/update-tools.sh; performed 

This script units the proprietor of a sure file on track methods to be “prod1”:

 for l in `cat servers`; do echo $l;ssh -q $l chown prod1:prod /fileData/fileroot/certs/interac.jks;echo $l; performed 

This script tells all goal hosts to run a yum replace and never immediate to put in packages:

 for l in `cat servers`; do ssh -q $l yum -y replace; echo $l; performed 

This script backs up the /and so forth/shadow file on all goal hosts to the basis listing:

 for l in `cat servers`; do echo $l;ssh -q $l cp /and so forth/shadow /root/shadow20210604;echo $l; performed 

This script reboots all servers listed within the servers file:

 for l in `cat servers`; do ssh -q $l shutdown -r now; echo $l; performed 

This script can turn out to be useful to unmount an present NFS quantity then replace /and so forth/fstab with a brand new NFS mount title and remount all volumes:

 for l in `cat servers`; do echo $l;ssh -q $l umount /clover;echo $l; performed 
 for l in `cat servers`; do echo $l;ssh -q $l;sed -r -i 's|vnx-corp-nfs1|unity-corp-nfs01|' /and so forth/fstab;mount -a;echo $l;      performed 

This script can add a bunch file entry to focus on methods

 for l in `cat servers`; do ssh -q $l 'echo "10.18.12.33  github.devops.native" >> /and so forth/hosts'; echo $l; performed 

Lastly, this script can let you know the present uptime on all goal hosts:

 for l in `cat servers`; do echo $l;ssh -q $l uptime;echo $l; performed 

As you’ll be able to see, the performance of those scripts is restricted solely by your creativeness. I hope these are useful and that they ease your administrative workload.

Additionally see

Source link