Linux系统调用shell脚本对各IP机器进行升级

本文发布时间: 2019-Mar-22
执行main.sh脚本即可实现无手执对hostname中各IP机器进行updatefilename升级包的升级。tcl和expect压缩包放在相同路径下,$TSMIS_ROOT为环境变量。CopyFile.exp文件:#!/usr/bin/expect -f#输入分析仪ip以及需要上传的文件名和TSMIS_ROOT环境变量set ip [lindex $argv 0]set localfile [lindex $argv 1]set TSMIS_ROOT [lindex $argv 2]#scp拷贝需要传输的文件到指定目录,并且跳过手动输入部分spawn scp -r $localfile root@$ip:$TSMIS_ROOT/tmp/$localfileset timeout 300expect {"yes/no" {send "yes\r" ; exp_continue}"*assword:" {send "111111\n"}}set timeout 300send "exit\r"expect eofRunUpdate.exp文件:#!/usr/bin/expect -fset ip [lindex $argv 0]set updatefile [lindex $argv 1]set TSMIS_ROOT [lindex $argv 2]spawn ssh root@$ipset timeout 300expect "*assword:" { send "111111\n" }set timeout 300expect "#"send "cd $TSMIS_ROOT/tmp\r"set timeout 300expect "#"send "tar -zxvf $updatefile.tar.gz\r"set timeout 300expect "#"send "cd $updatefile\r"set timeout 300expect "#"send "sh $TSMIS_ROOT/bin/mykillproc.sh\r"set timeout 300expect "#"send "sh *pdate.sh\r"set timeout 300expect "#"send "reboot\r"set timeout 300expect "#"send "exit\r"expect eofmain.sh文件:#Input TS/VAR IP into ' ',use "," to seperate them. Ex: hostlist:'192.168.10.178,192.168.10.179,192.168.10.180'hostlist='192.168.10.176,192.168.10.171'#Input update packet into ' '. Ex : updatefilename='update120821.tar.gz'updatefilename='update120821.tar.gz'chmod 777 *#获取当前路径cur_dir=$(pwd)#判断expect是否已经安装if [ -f "/usr/bin/expect" ]; thenecho "/usr/bin/expect existed"else#安装expect环境tar -zxvf tcl8.4.19-src.tar.gztar -zxvf expect-5.43.0.tar.gzmv tcl8.4.19 $TSMIS_ROOT/tcl8.4.19 -fmv expect-5.43 $TSMIS_ROOT/expect-5.4.3 -f#tcl安装cd $TSMIS_ROOT/tcl8.4.19/unix./configuremakemake installsleep 1echo "Install tcl succeed !"#expect安装cd $TSMIS_ROOT/expect-5.4.3./configure --with-tclinclude=$TSMIS_ROOT/tcl8.4.19/generic --with-tclconfig=/usr/local/lib/makemake installsleep 1echo "Install expect succeed !"#将expect可执行文件复制到/usr/bin目录下cp /usr/local/bin/expect /usr/bin/ -ffi#进入之前路径cd $cur_dir#获取当前时间NOW=`date "+%Y-%m-%d %H:%M:%S"`echo "$NOW " >$TSMIS_ROOT/tmp/UpdateFailed.log#设置IFS分割字符IFS=','#循环升级for host in $hostlistdoecho "Sending updatepacket to $host ..."RESULT=`ping -c 2 $host|grep 'ttl'`if [ -z "$RESULT" ] ; then#网络不通时,打印相关日志echo "Can't connect to $host" >>$TSMIS_ROOT/tmp/UpdateFailed.logecho "##########Can't connect to $host!!!##########"else#网络畅通,则进行下一步操作./CopyFiles.exp $host $updatefilename $TSMIS_ROOTecho "Copy Files finished"#在各个分析仪上运行升级包./RunUpdate.exp $host ${updatefilename//.tar.gz/} $TSMIS_ROOTecho "$host update finished!!!"fisleep 1doneecho "All update finished"rm -rf $cur_dir/tcl8.4.19rm -rf $cur_dir/expect-5.43


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:11pm
栏目列表