20.27 分发系统介绍
分发系统: 可以把每段时间的最新代码分别发布到各服务器上去
上线: 就是把开发的代码发布到线上环境去
yum install -y expect
20.28 expect脚本远程登陆
vim /usr/local/sbin/1.expect
//exp_continue 表示继续 \r表示回车
interact 表示停留在远程机器上,不退出, 若是去掉这句 则登陆后会立刻退出
expect eof 会停留在远程机器上几秒后退出
#自动远程登陆
#! /usr/bin/expect
set host "192.168.192.135"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
interact
chmod a+x 1.expect
./1.expect 测试是否自动登陆
若是你删除了 vim /root/.ssh/known_hosts 中的内容, 第一次ssh登陆会有提示yes/no
20.29 expect脚本远程执行命令
]* 表示 出来的提示符是 ] 后面是任意符号
#自动远程登陆后,执行命令并退出
#! /usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh $user@192.168.192.135
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"
20.30 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"
#send timeout -1 若是加上这句就永远不超时, 也能够设定几秒超时, 如5秒 send timeout 5
expect "]*"
send "exit\r"
./3.expect root 192.168.192.135 ls
./3.expect root 192.168.192.135 "ls;w;pwd" //执行多个命令