Strive_tan shell programming actual combat 2-distribution system

1. Expect applications

1) Transfer files

2) Execute commands remotely, no interaction, no need to enter a password

3) The core of the online shell script (tool) is expect, which is the distribution system.

2. Expect installation

yum install -y expect

3. Expect language Example 1: Automatically log in to a server remotely

#! /usr/bin/expect

set host “192.168.133.132”

set passwd “123456”

spawn ssh [emailprotected]$host

expect {

“yes /no” {send “yes
“; exp_continue}

“password:” {send “$passwd
” }

}

interact< /p>

4. Expect language example 2: After automatically logging in to a server remotely, and executing the command

#!/usr/bin/expect

set user “root”

set passwd “123456”

spawn ssh [email protected]

expect {

“yes/no” {send “yes
“; exp_continue}

“password: “{send “$passwd
” }

}

expect “]*”

send “touch /tmp/12.txt
“< /p>

expect “]*”

send “echo 1212> /tmp/12.txt

expect “]*”

send ” exit

5. Passing parameters of expect script

#!/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
“}

“password:” {send “$passwd
” }

}

expect “]*”

send “$cm

expect “]*”

send “exit

$ argv0 Indicates the parameter to be passed to the variable user; $argv1, $argv2 are the same.

6. Sync file of expect script

Share picture

7. Build file distribution system for expect script

Share pictures

8.

< p>#!/usr/bin/expect

set host [lindex $argv 0]

set passwd “123456”

set cm [lindex $argv 1]

spawn ssh [email protected]$host

expect {

“yes/no” {send “yes
“}

“password:” {send “$passwd
” }

}

expect “]*”

send “$cm

expect “]*”

send “exit

Leave a Comment

Your email address will not be published.