Shell-script - Search string and add in file, Add data in the file
Search string and add in file
The below script helps you to search a string and add it in to the file, if the string is not found in it. Here in the below script we search for a ipaddress is found on the server or not, it the Ip address is found then the script will not add ipaddress into the file. Else it will add it in the fileAssume that you have a file called server_name and it has list of hostname or server name in it.
server-1 server-2 server-3 server-4 server-5
#!/bin/bash file="server_name" ip="127.0.0.1" while read line do echo "$line" ssh root@$line "ff=\$(grep -lr \"$ip\" /etc/hosts) if (grep -lr \"$ip\" /etc/hosts>/dev/null) then echo \"$ip exists!\" else echo \"127.0.0.1 localhost localhost.localdomain\" >> /etc/hosts fi" <<EOF EOF done<"$file"Execute the script based to search for the ip or sting and add it in multiple hosts.
Ping the server name and fetch its IPAddress
#!/bin/bash file="server_name" while read line do echo "#$line" >> result out=$(ping -c 2 $line | grep PING | awk '{print $3}' | tr -d '()') echo "$out" >> result done<"$file"
Try to login to multiple servers using below code
#ssh -t -t root@$line <<EOF #echo "hI.." >> testecho #EOF done<"$File" #ssh root@$i #<<EOF #$pass #echo "hI.." # put all commands to execute here #EOF
The topic on Shell-script - Search string and add in file is posted by - Guru
Hope you have enjoyed, Shell-script - Search string and add in fileThanks for your time