CentOS 7 expect + Shell implementation batch file creation

Source: Internet
Author: User

The problem has been a long time, and has not been sorted out. Remember the last interview when asked how to bulk modify the host name of 1000 machines. Here is a file named 1.txt that is created in bulk for hello.

First, install the expect

Yum Install-y expect

Ii. preparatory work: (1) Prepare a list of IP addresses. Vim Ip.txt

192.168.1.88

192.168.1.89

192.168.1.90

(2) Prepare the command to be executed. Vim Cmd.txt

Mkdir/tmp/test

Third, the script part

#!/bin/bashpasswd= "123456" sc=$ (cat/tmp/cmd.txt) echo $sccat ip.txt |while Read Linedo/usr/bin/expect <<eofset Timeout 10spawn ssh [email protected] $lineexpect {"yes/no" {send "yes\r"; exp_continue} "password:" {send "$passwd \ r"}}ex Pect "]#" send "$SC \ r" Send "exit\r" expect Eofeofdone exit 0

Limitations: If the password is different, this scenario needs to be considered separately. A passwd file may be added.

This article is from the "Share,open source" blog, so be sure to keep this source http://liqilong2010.blog.51cto.com/3029053/1875114

CentOS 7 expect + Shell implementation batch file creation

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.