Shell编程(分发系统介绍、expect远程登录、expect远程执行命令、expect传递参数)

为什么80%的码农都做不了架构师?>>>   hot3.png

分发系统介绍expect

分发系统expect即分发脚本,是一种脚本语言;通过他可以实现传输,输入命令(上线代码)
应用场景:业务越来越大,网站app,后端,编程语言是php,所以就需要配置lamp或者lnmp,最好还需要吧代码上传到服务器上;但是因为业务增加,代码增加,多台机器,就会非常麻烦;这是只需要一个分发系统,就可以把每次更新的代码发布到需要更新的服务器上

expect脚本远程登录

实验需求:自动远程登录
准备工作:

  • A机器ip:192.168.248.130
  • B机器ip:192.168.248.129

脚本编写:

1.A机器yum安装expect

yum install -y expect

2.A机器编写脚本文件1.expect,脚本内容如下:

#! /usr/bin/expect
set host "192.168.248.129" #expect脚本中定义变量语法
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" } #需要密码时发送变量值过去
}
interact #脚本结束,停留在远程的机器上,不会退出。

此文件保证登录信息的,清空的话,重新远程登录ssh 会有提示 /root/.ssh/known_hosts exp_continue 表示继续 \r 表示换行 interact 继续停留在这台机器,不退出

3.添加执行权限

[root@yolks3 ~]# ./1.expect
-bash: ./1.expect: 权限不够
[root@yolks3 ~]# chmod a+x 1.expect

4.执行脚本:成功登录

[root@yolks3 ~]# ./1.expect
spawn ssh [email protected]
The authenticity of host '192.168.248.129 (192.168.248.129)' can't be established.
ECDSA key fingerprint is SHA256:cYZ9a5uLeFpKmGT7U7X3BIwLdUoZXcRFMSYCgXOkuLA.
ECDSA key fingerprint is MD5:9a:1a:ca:60:8a:bf:ed:b4:45:13:f8:c8:aa:1a:92:f5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.248.129' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Last login: Mon Sep 24 20:45:13 2018 from 192.168.248.1
[root@yolks2 ~]#

expect脚本远程执行命令

实验需求:远程登录之后退出,即A机器执行脚本之后还是A机器的状态
脚本实现:

1.新建脚本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"

expect "]*" : 匹配 ]后边所有

2.修改执行权限

[root@yolks3 ~]# ./2.expect
-bash: ./2.expect: 权限不够
[root@yolks3 ~]# chmod a+x 2.expect

3.A机器执行脚本

[root@yolks3 ~]# ./2.expect 
spawn ssh [email protected]
[email protected]'s password: 
Last login: Mon Sep 24 20:57:24 2018 from 192.168.248.130
[root@yolks2 ~]# touch /tmp/12.txt
[root@yolks2 ~]# echo 1212 > /tmp/12.txt
[root@yolks2 ~]# [root@yolks3 ~]#

4.B机器查看验证文件是否创建成功

[root@yolks2 ~]# ls -l /tmp/12.txt 
-rw-r--r-- 1 root root 5 9月  24 21:10 /tmp/12.txt
[root@yolks2 ~]# cat !$
cat /tmp/12.txt
1212

expect脚本传递参数

传递参数

1.编写脚本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 $user@$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

2.添加x权限 : chmod a+x 3.expect 3.执行脚本:./3.expect [参数1] [参数2] [参数3]

[root@yolks3 ~]# ./3.expect root 192.168.248.129 ls
spawn ssh [email protected]
[email protected]'s password: 
Last login: Mon Sep 24 21:10:31 2018 from 192.168.248.130
[root@yolks2 ~]# ls
2.expect  aming.txt  anaconda-ks.cfg
[root@yolks2 ~]# [root@yolks3 ~]# 

参数3支持多条命令执行:"命令1;命令2;..."

[root@yolks2 ~]# [root@yolks3 ~]# ./3.expect root 192.168.248.129 "ls;w"
spawn ssh [email protected]
[email protected]'s password: 
Last login: Mon Sep 24 21:32:18 2018 from 192.168.248.130
[root@yolks2 ~]# ls;w
2.expect  aming.txt  anaconda-ks.cfg
 21:33:58 up 50 min,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.248.1    20:45   20:46   0.00s  0.00s -bash
root     pts/1    192.168.248.130  21:33    0.00s  0.00s  0.00s w

转载于:https://my.oschina.net/yolks/blog/2208157

你可能感兴趣的:(Shell编程(分发系统介绍、expect远程登录、expect远程执行命令、expect传递参数))