Expect bulk copy of key files and execute simple scripts in bulk

Source: Internet
Author: User
Tags ssh port

I. Introduction of Expect

Expect is a program that allows you to "session" with an interactive program in a way that is set in the script content. Depending on the content of the script, expect can tell what content the program prompts or feeds and what is the correct answer.

It is an interpreted scripting language that provides "branching and nesting Structures" to guide program flow. The shell is powerful, but it is not possible to implement multi-machine operations that have interactivity, such as SSH and FTP. And expect can help us to achieve.


Second, install the expect package

#yum Install Expect-y


Note: Use expect bulk copy id_rsa.pub if the SSH port must be the default port 22, otherwise it will error:

Ssh:connect to host 10.0.18.95 Port 22:connection refused

If the SSH port is not 22, if it is 22000

The command line copy is as follows:

#ssh-copy-id-i/root/.ssh/id_rsa.pub '-p 22000 [email protected] '--Prompt for root password, enter

[email protected] ' s password:

Now try logging to the machine, with "ssh '-p 22000 [email protected] '", and check in:


. Ssh/authorized_keys


To make sure we haven ' t added extra keys so you weren ' t expecting.


Login test:

#ssh-p22000 [email protected]--can log in normally

If you are executing in a script, you need to create a config file in the root directory

#cd/root/.ssh

#vim Config

Port 22000

Then the batch will be id_rsa.pub to other SSH port to 22000 server on it!



Third, the SSH port by default is 22, the server-side key is copied to all the defined client script

The script is as follows:

#cat test.sh


#!/bin/bash

File= ' Cat/root/testip.txt '

For i in $FILE;d o

Ip=$ (echo "$i" |cut-d ":"-f1)

Password=$ (echo "$i" |cut-d ":"-f2)


Expect-c "

Spawn/usr/bin/ssh-copy-id-i/root/.ssh/id_rsa.pub [email protected] $ip

Expect {

\ "*yes/no*\" {send \ "yes\r\"; Exp_continue}

\ "*password*\" {send \ "$password \r\"; Exp_continue}

\ "*password*\" {send \ "$password \r\";}

}

"

Done


Execute script: #bash test.sh


Note: The Testip.txt file format is as follows:

192.168.1.450:mima123


You can then login to the client server without entering a password, as follows:

#ssh [email protected]

You can also log in by customizing the hostname in the/etc/hosts (if the client is more than one, the host name is well-written)

Like what:

#vim/etc/hosts

192.168.1.450 Testserver1

Test:

#ssh Testserver1--landed on 1.450 this server!!!


Add: I tested the client root password for Liguang, copy the id_rsa.pub to this client, SSH hostname login to the client, and then modify the root of the client

Password for liguang123, can also be normal ssh hostname login! A little puzzled.


Note: Executing this script appends the server-side/root/.ssh/id_rsa.pub file to the Authorized_keys file in the client/root/.ssh/directory (the first execution is to create the Authorized_keys file)!!!


Second, through the expect Batch Management client server

A simple small script that modifies DNS in bulk, as follows:

#!/bin/bash

# # #modify/etc/resolv.conf # # #


For I in $ (cat/root/testip.txt|cut-d ":"-f1);d O

SSH [email protected] $i "Sed-i ' s/10.0.90.1/10.0.900.1/g '/etc/resolv.conf"

Done





Scripts seen on the web

#cat ssh.sh

#!/usr/bin/expect

RM-RF root/.ssh/known_hosts

Expect-c "

Spawn ssh-keygen-t RSA

Expect {

\ "*id_rsa*\" {Send \r;exp_continue}

\ "*passphrase*\" {Send \r;exp_continue}

\ "*again*\" {Send \r;exp_continue}

}

"

For P in $ (cat/script/ip.txt)

Do

Ip=$ (echo "$p" |cut-f1-d ":")

Password=$ (echo "$p" |cut-f2-d ":")

Expect-c "

Spawn ssh-copy-id-i/root/.ssh/id_rsa.pub [email protected] $ip

Expect {

\ "*yes/no*\" {send \ "yes\r\"; Exp_continue}

\ "*password*\" {send \ "$password \r\"; Exp_continue}

\ "*password*\" {send \ "$password \r\";}

}

"

Done

For h in $ (cat/script/ip.txt|cut-f1-d ":")

Do

SSH [email protected] $h "ls $dire"

Dire= "/tmp/test"

If [$?-eq 0];

Then

SSH [email protected] $h RM-RF "$dire"

Set Timeout 300

SSH [email protected] $h mkdir-p/tmp/test

Fi

SSH [email protected] $h Touch lgl.txt

Scp/root/centos-5.3-x86_64-bin-dvd.iso [Email Protected]:/home

Set Timeout 300

Done


Expect bulk copy of key files and execute simple scripts in bulk

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.