20.27 分发系统介绍 20.28 expect脚本远程登录 20.29 expect脚本远程执行命令 20.30 expect脚本传递参数

原创
2018/04/24 21:36
阅读数 70

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"    //执行多个命令
展开阅读全文
加载中

作者的其它热门文章

打赏
0
4 收藏
分享
打赏
0 评论
4 收藏
0
分享
返回顶部
顶部