-
#!/usr/bin/expect
-
#
-
# This script can be used to login remote server(first parameter) with passwords in LIST and then execute below tasks.
-
#
-
-
-
set hostname [lindex $argv 0]
-
set passwords { abc. changeme changeme.0 changeme.1 }
-
set try 0
-
-
if { $argc == 0 } {
-
send_error "Please use this command as ./enable_snmp.sh HOSTNAME\n"
-
exit
-
}
-
-
spawn ssh admin@$hostname
-
expect {
-
"(yes/no)?*"
-
{send "yes\r"; exp_continue;}
-
-
"*?assword:*"
-
{
-
if { $try >= [llength $passwords] } {
-
send_error ">>> wrong passwords\n"
-
exit 1
-
}
-
set ind $try
-
send [lindex $passwords $try]\r
-
incr try
-
exp_continue
-
}
-
-
# To fix the screen running servers.
-
"Enter Screen Name : "
-
{send "\r"; exp_continue;}
-
-
# Normally, after login, prompt likes this "admin@*:~/>: "
-
"admin@*/>*"
-
{send "echo '[lindex $passwords $ind]' | su - root -c \"service snmpd restart;\"\r";send exit\r }
-
-
# Abnormally, after login, prompt likes this "admin@* #: "
-
"admin@*#:**"
-
{send "echo '[lindex $passwords $ind]' | su - root -c \"service avfirewall stop \"\r";send exit\r }
-
-
# Abnormally, after login, prompt likes this "[admin@* ~]$ "
-
"*$*"
-
{send "echo '[lindex $passwords $ind]' | su - root -c \"service avfirewall stop ;\"\r";send exit\r }
-
-
}
-
interact
===================
#bin/bash
#
# This script can be used to enable SNMP one by one which in the file_list(first parameter).
# Then return the results into output log file.
#
OUTPUT_LOG=`pwd`"/snmp_log"
OUTPUT_RESULT=`pwd`"/snmp_result"
HOSTKEY_FILE="/root/.ssh/known_hosts"
HOST_LIST_FILE=$1
if [[ -e $OUTPUT_LOG ]] ; then
cp -p $OUTPUT_LOG $OUTPUT_LOG`date +%Y%m%d%H%M`
>$OUTPUT_LOG
fi
if [[ -e $OUTPUT_RESULT ]] ; then
cp -p $OUTPUT_RESULT $OUTPUT_RESULT`date +%Y%m%d%H%M`
>$OUTPUT_RESULT
fi
if [[ -e $HOSTKEY_FILE ]] ; then
cp -p $HOSTKEY_FILE $HOSTKEY_FILE`date +%Y%m%d%H%M`
>$HOSTKEY_FILE
fi
if [[ $# != 1 ]]; then
echo "Usage is ./batch_enable_snmp.sh + HOST_LIST_FILE"
exit;
fi
exec >$OUTPUT_LOG
for i in `cat ${HOST_LIST_FILE}`;
do
sleep 1
/root/enable_snmp_expect.sh $i
if [[ $? == "0" ]]; then
echo "Host: $i configured SNMP" >>$OUTPUT_RESULT;
else
echo "Host: $i is not configured SNMP" >>$OUTPUT_RESULT;
fi
done