Requirements Analysis:
1, SSH Public key copy, provide no password management.
2, batch synchronization hosts files to more than one host.
3. Batch modification of host name.
Realize:
First edit a Hosts file for synchronization to multiple hosts
Vi/etc/hosts
192.168.0.1 Server1
192.168.0.2 server2
192.168.0.3 server3 192.168.0.4 server4 192.168.0.5 server5
192.168.0.6 server6
192.168.0.7 server7
192.168.0.8 server8
192.168.0.9 Server9
192.168.0.10 Server10
Then edit the shell script:
VI changename.sh
#!/bin/bash
# This was a shell script to change hostname
# version 0.1
# Created in 2015.8 .
# Creator Edison
export path= $PATH
export user=root
export snamepre=server
export passwd= test01 #定义密码 for
i in {1..10};
Do/usr/bin/expect << EOF # #这里用到了expect完成了确认yes和密码输入交互
spawn ssh-copy-id-i/root/.ssh/id_rsa.pub $ user@ $SNAMEPRE $i
expect {
"(yes/no)?" {send "yes\r"; exp_continue}
" Password: "{send" $PASSWD \ r "}
}
interact
expect EOF
EOF
ssh $USER @ $SNAMEPRE $i" Sed-i s/^ Host.*/hostname= $SNAMEPRE $i//etc/sysconfig/network ";
Scp/etc/hosts $USER @ $SNAMEPRE $i:/etc/hosts;
Done
Here we use the expect to complete the automatic interactive confirmation and password input.
Expect is a free programming language used to automate and interact with tasks without human intervention. Expect's author, Don Libes, began writing Expect in 1990 with the following definition of Expect: Expect is a software suite for automatic interaction (Expect [is a] software suite for automating Interactive tools). It is used by the system administrator to create scripts that provide input to commands or programs that are expected to be entered from the terminal (terminal), which are generally required to be entered manually.
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, if friendship you can message exchange.