Replacing value in a file on all remote systems.

Hello All,

I want to replace a value of a string in a file on all the remote systems. The file is /opt/adapter/Adapter/snmp/conf/snmpd.conf and the parameter to be replaces if "rocommunity" for which current value is "public" and wish to change it to "sp1der".

i did trying it through sed, but not sure how do i do it all systems. Also there are other strings in file which has a value as "public" which should not be replaced.

Please advice.

Setting up the public key authentication, then just perform your replacement within a loop!

The public key authorization is already there. can you please what needs to be used in script to achieve this. Thanks in advance.

-Siddhesh

Locating all your remote servers' ip addresses in ip_file one per line!

for ipaddr in $(cat ip_file); do
     ssh ${ipaddr} "sed -i '/rocommunity/s/public/sp1der/' /opt/adapter/Adapter/snmp/conf/snmpd.conf"
done

Hello,

Bases on the give code, i have written the script. is this correct?

#!/bin/bash

#########################################################################################################
# Shellscript   :       rocommunity_change.sh - To change the community string 
# Version       :       V1.0
# Owner         :       Siddhesh Khavnekar <siddhesh.khavnekar@mobixell.com>
# Date          :       2012-03-28
# Category      :       Files Management
#########################################################################################################
# Description   :       This scipt will change rocommuniy from snmpd.conf on all nodes
# IP_LIST       :     Locating all your remote servers' ip addresses in ip_file one per line!
#########################################################################################################

#Defining Variable
#------------------------
IP_LIST=<path_to_file>

for ipaddr in $(cat IP_FILE); do
     ssh ${ipaddr} "sed -i '/rocommunity/s/public/sp1der/' /opt/adapter/Adapter/snmp/conf/snmpd.conf"
done

exit 0

Could you please confirm.

-Siddhesh

---------- Post updated at 09:23 PM ---------- Previous update was at 08:31 PM ----------

---------- Post updated at 10:27 PM ---------- Previous update was at 09:23 PM ----------

Hello All,

The above mentioned script worked well. but i have few other requirements on this.

1.Our script should take the IP of servers from a text file (we already have the solution for this in above script)customer have all together 36 nodes.

2.Script should be able to copy the script file on all the other nodes, taking IP from the text file.(ip_list file)

3.rocommunity should be prompted, so that user can input their string, shouldn't be hardcoded. In the above script it directly replaces with sed.

4.Log should be generated out of this, so that they can track it for any errors. (We can include echo commands to get this)

  1. And at the end this script must have ability to perform the start/stop/restart from the single server.

Here is the basic code i have written till now which covers point 1, 4 and 5 points. please help

#!/bin/bash

#########################################################################################################
# Shellscript   :       rocommunity_change.sh - To change the community string 
# Version       :       V1.0
# Owner         :       Siddhesh Khavnekar <siddhesh.khavnekar@mobixell.com>
# Date          :       2012-03-28
# Category      :       Files Management
#########################################################################################################
# Description   :       This scipt will change rocommuniy from snmpd.conf on all nodes
# IP_LIST       :     Locating all your remote servers' ip addresses in ip_file one per line!
#########################################################################################################

#Defining Variable
#------------------------
IP_LIST=/root/IP_LIST
ADAPTER_HOME=/opt/adapter
BACKUP_DIR=/var/tmp
DATE=`date +%Y%m%d%H%M%S`
LOG=/root/community_logs

#Backing Up Orginal Files
#-------------------------
echo "Backing up the original configuration file to $BACKUP_DIR" > $LOG.$DATE
cp -pv $ADAPTER_HOME/Adapter/snmp/conf/snmpd.conf $BACKUP_DIR/snmpd.conf_$(date +%d%b%Y)
cp -pv $ADAPTER_HOME/ManagementTool/conf/MTInternalConfig.properties $BACKUP_DIR/MTInternalConfig.properties_$(date +%d%b%Y)
echo "Orignal Log file back-up done succesfully" >$LOG.$DATE


Need to add code to prompt to change the string in snmpd.conf file. Please advice on the same. here it should promt to change the rocommunity string to sp1der in 
/opt/adapter/Adapter/snmp/conf/snmpd.conf. To be more specific what i want her, It should replace the value of "rocommunity" from public to spider. 
I cannot use sed, bcoz they want to enter the value when it prompts to enter the value. it could be any other value too.

#Verification of changes done.
#-----------------------------
grep -w 'ServerSNMPGetArgs' $ADAPTER_HOME/ManagementTool/conf/MTInternalConfig.properties | grep -w sp1der >> /dev/null
if 
[ $(echo $?) = 0 ]; then
echo "rocommunity string set to sp1der sucessfully in MTInternalConfig.properties for ServerSNMPGetArgs." >> $LOG.$DATE
else
echo "Could not set the rocommunity string to sp1der in MTInternalConfig.properties for ServerSNMPGetArgs. Check the file permission." >> $LOG.$DATE
fi


#Restarting Processes.
#---------------------

snmpd stop 
echo "SNMP process Stopped" >> $LOG.$DATE
sleep 15

snmpd start
echo "SNMP process Started" >> $LOG.$DATE

adapter restart
echo "Adapter restarted" >> $LOG.$DATE

managermenttool restart 
echo "Managementtool Restarted" >> $LOG.$DATE

exit 0

Please advice as this is on priority for me. Thanks in advance.

-Siddhesh

---------- Post updated at 11:23 PM ---------- Previous update was at 10:27 PM ----------

Guys any idea on this. Please reply...

-Siddhesh