Shell interaction method for batch change of host name _linux Shell

Source: Internet
Author: User
Tags ssh

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.