需求分析:
1、ssh公鑰拷貝,提供無密碼管理。
2、批量同步hosts文件到多臺主機(jī)。
3、批量修改主機(jī)名。
實(shí)現(xiàn):
首先編輯一份用于同步到多臺主機(jī)的hosts文件
vi /etc/hosts192.168.0.1 server1192.168.0.2 server2192.168.0.3 server3192.168.0.4 server4192.168.0.5 server5192.168.0.6 server6192.168.0.7 server7192.168.0.8 server8192.168.0.9 server9192.168.0.10 server10
然后編輯shell腳本:
vi changename.sh#!/bin/bash# This is a shell script to change hostname# version 0.1# Created in 2015.8.11# Creator Edisonexport PATH=$PATHexport USER=rootexport SNAMEPRE=serverexport PASSWD=test01 #定義密碼for i in {1..10};do /usr/bin/expect << EOF ##這里用到了expect完成了確認(rèn)yes和密碼輸入交互spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $USER@$SNAMEPRE$iexpect {"(yes/no)?" {send "yes/r";exp_continue}"password:" {send "$PASSWD/r"}}interactexpect eofEOFssh $USER@$SNAMEPRE$i "sed -i s/^HOST.*/HOSTNAME=$SNAMEPRE$i/ /etc/sysconfig/network";scp /etc/hosts $USER@$SNAMEPRE$i:/etc/hosts;done;這里用到了expect完成自動交互確認(rèn)和密碼輸入。
Expect是一個免費(fèi)的編程工具語言,用來實(shí)現(xiàn)自動和交互式任務(wù)進(jìn)行通信,而無需人的干預(yù)。Expect的作者Don Libes在1990年 開始編寫Expect時對Expect做有如下定義:Expect是一個用來實(shí)現(xiàn)自動交互功能的軟件套件 (Expect [is a] software suite for automating interactive tools)。使用它系統(tǒng)管理員 的可以創(chuàng)建腳本用來實(shí)現(xiàn)對命令或程序提供輸入,而這些命令和程序是期望從終端(terminal)得到輸入,一般來說這些輸入都需要手工輸入進(jìn)行的。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果友誼大家可以留言交流。
新聞熱點(diǎn)
疑難解答