Automation Linux Build Using Shell Script

 Using Linux Scripting, we can build the n of Linux machine as much faster rather than the manual steps. So for this activity what are the important things we need to know I have made into the points which will help better write the shell script and automate the task.

Shell Scripting Automation Core Element

  • Tidal symbol usage
  • Airthematic operation easy way
  • Converting Excel sheet to normal vim file how to see the hidden character
  • From the parent server How to update, etc hosts entry on the n no of the remote machine
  • Run script on remote n no of machines using for loop

Tidal Symbol Usage

    if we want to run any command and get the output we can use the Tidal Symbol `
you can refer to this example on subsequent codes. 

Airthematic Operation easy way

    while doing air thematic operation in shell script we may need to use expr that is a little bit complicated to avoid this, we can use simple character open and close bracket ( )
((h=$h+1));

Converting Excel sheet to normal vim file how to see the hidden character

While converting an excel sheet to a normal vim file, there will be a lot of hidden characters present, which we can not see on the normal mode, to see this character we need to go 
Esc mode and :set list
Excel sheet two-column can be divided by \t option vim. This cannot view normally even with the set list option also, we can get some info with:set list which will display like 
Hostname ^I IP $

From the Jump server How to update, etc hosts entry on the n no of the remote machines

    For this activity, we need to have file which contains a)hostname b)IP details using this information with help of for, awk, ssh we can write the hosts entry to the specified hosts.
h=0;
for i in `cat /tmp/host-ip`;
do
        ((h=$h+1));
        ip=`echo $i | awk -F "," '{ print $2}'`;fqdn=`echo $i | awk -F "," '{ print $1}'`;short=`echo $fqdn | awk -F "." '{ print $1}'`;
        echo $ip $fqdn $short;
        ssh s$h "sh -c 'echo "$ip $fqdn $short">>/etc/hosts'";
done

From this example, we have learned How to run commands on the remote host also.

Run script on remote n no of machines using for loop

    Using the below command takes the input from the local file and run it on the n of the machine, without copying it to the remote host
script=/tmp/runcom;for i in {1..7}; do ssh s$i bash -s < $script; done

Here there are challenges are there, while running the sequence of commands when
  • when the first command runs 8 minutes
    • The second command depends on the first command output
we can't simply wait for 8 minutes for every single system, which took a long time. 

Solution
  • Inside the script, using nohup + & we can run the process in the background. But still, the next command which needs present command output will fail. 
    • So better while trigger from the parent server we need to push the running script into background. 
      • Parent script needs to generate a script which needs to run on the remote host
      • Once generate child script, From the parent script using nohup + & run the child script backgroud
cat <<EOF> /tmp/Installation_software/input.txt
y
y
hostname
y
y
EOF
cat << EOF> /tmp/app.sh
cd /tmp
tar -zxvf app_binary.tar.gz
cd app_binary/
cat "input.txt" | ./install #> /tmp/app.out 2> /tmp/app.err < /dev/null
EOF
chmod u+x app.sh
nohup sh -x /tmp/app.sh > scr.app.out 2> scr.app.err < /dev/null &

Here you have to observe I have command redirection, inside the child script, which won't work. the redirection will work only on the parent script.

Sed Important Points

1)sed -i '10,28d' /etc/hosts -> Delete range of lines
2)sed '5,7 s/./#&/' /etc/hosts -> Add command line (special character) for specific range of line
3)sed -i '17 i \\n' /etc/hosts -> Add the new line for the specific line number
   

Fdisk Creating Partition Automatically Through command

Using fdisk creating partition automate
echo -e "n\nn\ne\n1\n\n\nw" | fdisk /dev/sdb
    New -> Extended Partition -> 1 -> Enter -> Enter -> Write
echo -e "n\n5\n\n\nw" | fdisk /dev/sdb
    New -> Create Partition 5 -> Enter -> Enter -> Write
Enter -> Default value

Post a Comment

0 Comments