strive_tan shell编程实战2-分发系统

1.  expect的应用

 1)传输文件

 2)远程执行命令,无需交互,无需输入密码

 3)上线的shell脚本(工具),核心是expect,即分发系统

2. expect的安装

   yum install  -y expect

3.  expect语言实例1:自动远程登陆某台服务器

#! /usr/bin/expect

set host "192.168.133.132"

set passwd "123456"

spawn ssh root@$host

expect {

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

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

}

interact

4. expect语言实例2:自动远程登陆某台服务器后,并执行命令

#!/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"

5. expect脚本之传递参数

#!/usr/bin/expect

set user [lindex $argv 0]

set host [lindex $argv 1]

set passwd "123456"

set cm [lindex $argv 2]

spawn ssh $user@$host

expect {

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

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

}

expect "]*"

send "$cm\r"

expect "]*"

send "exit\r"

       $argv0   表示要传递给变量user的参数;$argv1,$argv2同理

6. expect脚本之同步文件

   

7.  expect脚本之构建文件分发系统

   

8. 

#!/usr/bin/expect

set host [lindex $argv 0]

set passwd "123456"

set cm [lindex $argv 1]

spawn ssh root@$host

expect {

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

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

}

expect "]*"

send "$cm\r"

expect "]*"

send "exit\r"

猜你喜欢

转载自www.cnblogs.com/tanzhirong/p/11421533.html