【spawn】远程登录并以root权限执行命令
#!/bin/bash
#A:S
pro_file=${1}
file_day=$(date "+%Y%m%d%H")
#comd="chown sms:sms /utxt/soft/scmd/scmd"
#comd="echo '* * * * * root sh /utxt/soft/scripts/restart_make_cdr.sh' >> /etc/crontab "
#comd=" ps -ef |grep rtds |grep -v grep |awk '{print \\\$2}' |xargs kill "
comd="cat /etc/crontab "
restart_fun(){
cat ${pro_file}.txt | while read ipline;
do
ip=`echo $ipline | awk -F',' '{print $1}'`
echo ${ip}
/usr/bin/expect << EOF
spawn /usr/bin/ssh -t -p 22088 -oStrictHostKeyChecking=no sms@${ip} "su "
expect {
"*assword*" { send "Eastcom@!@#\n" }
}
expect {
"Password:" { send "Eastcomnfv@2018\n" }
}
expect "*]#"
send " ${comd} \n"
expect "*]#"
send "exit\r"
expect eof
EOF
done
}
restart_fun