2018.4.25 18周1次课

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018.4.25 18周1次课相关的知识,希望对你有一定的参考价值。

十八周一次课(4月25日)

20.27 分发系统介绍

20.28 expect脚本远程登录

20.29 expect脚本远程执行命令

20.30 expect脚本传递参数

20.27 分发系统介绍

expect是一种能够按照脚本内容里面设定的方式与交互式程序进行“会话”的程序。根据脚本内容,Expect可以知道程序会提示或反馈什么内容以及什么是正确的应答。它是一种可以提供“分支和嵌套结构”来引导程序流程的解释型脚本语言。

我们熟知的shell编程功能虽然很强大,但是不能实现有交互功能的多机器之前的操作,例如ssh和scp等。而expect可以帮助我们来实现。

20.28 expect脚本远程登录

yum install -y expect

自动远程登录192.168.37.100

编辑expect文件:vi 1.expect

#! /usr/bin/expect      //说明用expect来执行

set host "192.168.37.100"       //set是设置变量,这里设置host变量的值,就是登陆主机的ip地址

set passwd "123456"        //set设置passwd变量的值,就是登陆主机的密码

spawn ssh [email protected]$host          //是进入expect环境后才可以执行的expect内部命令,用来执行它后面的命令。这里是登陆主机的语句

expect {          //用来解惑关键的字符串,如果有,就会立即返回下面设置的内容,如果没有就看是否设置了超时时间

"yes/no" { send "yes\r"; exp_continue}          //执行交互式动作,截取ssh连接主机时,"yes/no"这段,发送yes,\r:表示回车。exp_continue:表示继续

"assword:" { send "$passwd\r" }        //执行交互式动作,截取"assword:"这段,发送上面设置的passwd变量的值

}

Interact           //结束符。Interact:还停留在远程主机上;expect eof:退出远程主机。

用ssh登陆100主机:

ssh 192.168.37.100

[[email protected] /usr/local/sbin]# ssh 192.168.37.100

Last login: Tue Apr 24 15:27:32 2018 from 192.168.37.1

因为登陆过,所以没有提示就直接登陆了。清空/root/.ssh/known_hosts文件,让登陆时有提示

vi .ssh/known_hosts

ssh 192.168.37.100

[[email protected] ~]# ssh 192.168.37.100

The authenticity of host '192.168.37.100 (192.168.37.100)' can't be established.

ECDSA key fingerprint is SHA256:O/psyoi564EA1rmbUfUBPXikCFUf6bMTmwfAb8wwi7A.

ECDSA key fingerprint is MD5:47:ce:6b:84:7d:4f:e2:5a:cc:bb:0f:4b:61:be:ca:d6.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.37.100' (ECDSA) to the list of known hosts.

[email protected]'s password:

更改权限:chmod a+x 1.expect

执行脚本:./1.expect

[[email protected] /usr/local/sbin]# ./1.expect

spawn ssh [email protected]

The authenticity of host '192.168.37.100 (192.168.37.100)' can't be established.

ECDSA key fingerprint is SHA256:O/psyoi564EA1rmbUfUBPXikCFUf6bMTmwfAb8wwi7A.

ECDSA key fingerprint is MD5:47:ce:6b:84:7d:4f:e2:5a:cc:bb:0f:4b:61:be:ca:d6.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.37.100' (ECDSA) to the list of known hosts.

[email protected]'s password:

Last login: Tue Apr 24 16:12:28 2018 from 192.168.37.100

20.29 expect脚本远程执行命令

自动远程登录后,执行命令并退出

编辑expect文件:vi 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 "]*"      //登陆到01主机上后,最后一行显示的是“[[email protected] ~]#”,这里用*表示通配,这行要有关键字符串“]*”,就执行下面的命令

send "touch /tmp/12.txt\r"

expect "]*"

send "echo 1212 > /tmp/12.txt\r"

expect "]*"

send "exit\r"         //直接退出01主机

先退出01主机:logout

[[email protected] ~]# logout

Connection to 192.168.37.100 closed.

更改权限:chmod a+x 2.expect

执行:./2.expect

[[email protected] /usr/local/sbin]# ./2.expect

spawn ssh [email protected]

Last login: Wed Apr 25 09:56:25 2018 from 192.168.37.101

[[email protected] ~]# touch /tmp/12.txt

[[email protected] ~]# echo 1212 > /tmp/12.txt

[[email protected] ~]# [[email protected] /usr/local/sbin]#

[[email protected] /usr/local/sbin]#

查看01机器上的12.txt

[[email protected] ~]# ls /tmp/12.txt

/tmp/12.txt

[[email protected] ~]# cat !$

cat /tmp/12.txt

1212

20.30 expect脚本传递参数

编辑expect文件:vi 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 [email protected]$host

 

expect {

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

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

}

expect "]*"

send "$cm\r"

expect "]*"

send "exit\r"

更改权限:chmod a+x 3.expect

执行脚本:./3.expect root 192.168.37.100 ls

[[email protected] /usr/local/sbin]# ./3.expect root 192.168.37.100 ls

spawn ssh [email protected]

Last login: Wed Apr 25 10:39:59 2018 from 192.168.37.101

[[email protected] ~]# ls

123          1.txt  3.txt       anaconda-ks.cfg  data    sed        test1

1_heard.txt  234    aminglinux  awk              grep    soft_link

1_sorft.txt  2.txt  aming.txt   bb.txt           oldboy  test

如果要执行多条命令,要用双引号,双引号里面用分号分隔:./3.expect root 192.168.37.100 "ls;w"

[[email protected] /usr/local/sbin]# ./3.expect root 192.168.37.100 "ls;w"

spawn ssh [email protected]

Last login: Wed Apr 25 11:27:47 2018 from 192.168.37.101

[[email protected] ~]# ls;w

123          1.txt  3.txt       anaconda-ks.cfg  data    sed        test1

1_heard.txt  234    aminglinux  awk              grep    soft_link

1_sorft.txt  2.txt  aming.txt   bb.txt           oldboy  test

 11:32:43 up  1:41,  2 users,  load average: 0.00, 0.01, 0.05

USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT

root     pts/0    192.168.37.1     09:52    1:03m  0.08s  0.08s -bash

root     pts/1    192.168.37.101   11:32    3.00s  0.01s  0.00s w

[[email protected] ~]# [[email protected] /usr/local/sbin]#

 


以上是关于2018.4.25 18周1次课的主要内容,如果未能解决你的问题,请参考以下文章

2018.4.18 17周1次课

2017.12.18 2周1次课

2018.4.11 16周1次课

2018.4.10 15周5次课

2018.4.9 15周4次课

2018.4.26 18周2次课