"Distribution system" of shell programming

Source: Internet
Author: User
Tags rsync


First, expect explanation

Expect allows us to automatically log on to remote machines, and can implement automatic remote execution of commands. Of course, if you use key authentication without a password, you can also implement automatic logon and automatic remote command execution. But when we can't use key verification, we don't have a choice. So, just know the other machine's account number and password can be implemented through the expect script login and remote command.


1, installation expect

[email protected] ~]# Yum install-y expect


2. Automatic remote Login Script

Automatic Telnet, and execute commands, here are a few scripts:

First: Do not exit the script after landing;

The second one: after landing, execute the command and then exit the script;

Third: Pass the parameter log in, and then execute the command exit script;

Fourth: Automatic synchronization of file scripts;

Fifth: Specify host and files to synchronize


1) scripts that do not exit after logging in

[Email protected] ~]# cd/usr/local/sbin/

[Email protected] sbin]# mkdir expect

[Email protected] sbin]# CD expect/

[Email protected] expect]# vim 1.expect

#! /usr/bin/expect

Set Host "192.168.0.10"

Set passwd "123456"

Spawn ssh [email protected] $host

Expect {

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

"Assword:" {send "$passwd \ r"}

}

Interact

[Email protected] expect]# chmod a+x 1.expect

[[email protected] expect]#./1.expect//Run script log in to remote machine, logout can exit


2) script to exit after executing command

[Email protected] expect]# vim 2.expect

#!/usr/bin/expect

Set user "root"

Set passwd "123456"


Spawn ssh [email protected]


Expect {

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

"Password:" {send "$passwd \ r"}

}

Expect "]*"

Send "touch/tmp/12.txt\r"

Expect "]*"

Send "echo 1212 >/tmp/12.txt\r"

Expect "]*"

Send "exit\r"

[Email protected] expect]# chmod a+x 2.expect

[Email Protected]entos expect]#./2.expect


3) pass parameters Log in, script to exit after executing command

[Email protected] expect]# vim 3.expect

#!/usr/bin/expect

Set user [lindex $argv 0]

Set host [lindex $ARGV 1]

Set passwd "123456"

Set cm [lindex $argv 2]


Spawn ssh [email protected] $host


Expect {

"Yes/no" {send "yes\r"}

"Password:" {send "$passwd \ r"}

}

Expect "]*"

Send "$cm \ r"

Expect "]*"

Send "exit\r"

Note : The parameter format defined here is "$ARGV 0", which is not the same as our shell definition parameter format "$". CM defines the commands that need to be executed later.

[Email protected] expect]# chmod a+x 3.expect

[Email protected] expect]#/3.expect root 192.168.0.10 "CAT/ETC/PASSWD"


4) automatic synchronization of file scripts

[Email protected] expect]# vim 4.expect

#!/usr/bin/expect

Set passwd "123456"

Spawn Rsync-avzp [Email protected]:/tmp/12.txt/tmp/

Expect {

"Yes/no" {send "yes\r"}

"Password:" {send "$passwd \ r"}

}

Expect EOF

Note : EOF corresponds to the end meaning; In addition, for remote transfer of files, both the local and remote machines must have Rsync installed.

[email protected] expect]# Yum install-y rsync

[Email protected] expect]# chmod a+x 4.expect

[Email protected] expect]#./4.expect


5) Specify the host and the files to synchronize

[Email protected] expect]# vim 5.expect

#!/usr/bin/ Expect

set passwd "123456"

set host [lindex $argv 0]

spawn Rsync-avzp $ file [email protected] $host: $file

"yes/no" {send "yes\r"}

"password:" {send "$passwd \ r"}

Span style= "font-family: ' Microsoft Jas Black ', ' Microsoft Yahei ';" >

expect eof

[Email protected] expect]# chmod a+x 5.expect

[Email protected] expect]#/5.expect 192.168.0.10/tmp/12.txt

Description : To achieve synchronization, unification must be achieved, that is, all remote machine passwords are the same, and the file paths are consistent. The $file here is the same file path as the local and remote machines, so that synchronization can be achieved. To achieve multiple remote machine batch synchronization, we do the following:

[[email protected] expect]# touch/tmp/ip.txt//IP list

[[email protected] expect]# for IP in ' cat/tmp/ip.txt '; do echo $ip;./5.expect $ip/tmp/12.txt; Done



Ii. building a file distribution system

1.

2.

3.







This article is from the "M April Days" blog, please be sure to keep this source http://msiyuetian.blog.51cto.com/8637744/1712233

"Distribution system" of shell programming

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.