expect可讓咱們實現自動登陸遠程機器,而且能夠實現自動遠程執行命令。固然如果使用不帶密碼的密鑰驗證一樣能夠實現自動登陸和自動遠程執行命令。但當不能使用密鑰驗證的時候,咱們就沒有辦法了。因此,這時候只要知道對方機器的帳號和密碼就能夠經過expect腳本實現登陸和遠程命令。linux
安裝expectshell
[root@garytao-01 mon]# yum install -y expect
[root@garytao-01 shell]# vi 1.expect 增長如下腳本內容: #! /usr/bin/expect set host "172.16.111.110" set passwd "123456" spawn ssh root@$host expect { "yes/no" { send "yes\r"; exp_continue} "assword:" { send "$passwd\r" } } interact
這個文件是就保證登陸信息的,清空的話,從新遠程登陸ssh 會有提示/root/.ssh/known_hostsvim
#加入執行權限 [root@garytao-01 shell]# chmod a+x 1.expect #成功登陸 [root@garytao-01 shell]# ./1.expect spawn ssh root@172.16.111.110 The authenticity of host '172.16.111.110 (172.16.111.110)' can't be established. ECDSA key fingerprint is 09:6d:70:42:42:9a:12:69:51:9b:ad:e5:73:98:b9:c0. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '172.16.111.110' (ECDSA) to the list of known hosts. root@172.16.111.110's password: Last login: Mon Feb 26 18:22:32 2018 from 172.16.111.100 [root@garytao-02 ~]# 登出 Connection to 172.16.111.110 closed. [root@garytao-01 shell]#
[root@garytao-01 shell]# vi 2.expect 增長腳本以下內容: #!/usr/bin/expect set user "root" set passwd "123456" spawn ssh $user@192.168.133.132 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" #增長權限 [root@xietaolinux1 ~]# chmod a+x 2.expect #機器一執行腳本,遠程建立文件,寫入文件內容,回車退出 [root@garytao-01 shell]# ./2.expect spawn ssh root@172.16.111.110 root@172.16.111.110's password: Last login: Tue Feb 27 10:23:28 2018 from 172.16.111.100 [root@garytao-02 ~]# touch /tmp/12.txt [root@garytao-02 ~]# echo 1212 > /tmp/12.txt [root@garytao-02 ~]# [root@garytao-01 shell]# [root@garytao-01 shell]# ##從新執行自動登陸腳本 [root@garytao-01 shell]# ./1.expect spawn ssh root@172.16.111.110 root@172.16.111.110's password: Last login: Tue Feb 27 10:34:42 2018 from 172.16.111.100 #機器二查看遠程建立的文件 [root@garytao-02 ~]# ls -l /tmp/12.txt -rw-r--r-- 1 root root 5 2月 27 10:34 /tmp/12.txt ##查看遠程腳本建立的文件內容 [root@garytao-02 ~]# cat /tmp/12.txt 1212 [root@garytao-02 ~]#
[root@garytao-01 shell]# 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 $user@$host expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect "]*" send "$cm\r" set timeout -1 expect "]*" send "exit\r" [root@garytao-01 shell]# chmod a+x 3.expect [root@garytao-01 shell]# ./3.expect root 172.16.111.110 ls spawn ssh root@172.16.111.110 root@172.16.111.110's password: Last login: Tue Feb 27 10:39:46 2018 from 172.16.111.100 [root@garytao-02 ~]# ls 123.txt 1_heard.txt 1.txt aming anaconda-ks.cfg.1 shell zabbix-release-3.2-1.el7.noarch.rpm 123.txt~ 1_sorft.txt 2.txt aminglinux rsync yum.log #支持多條參數 [root@garytao-02 ~]# [root@garytao-01 shell]# ./3.expect root 172.16.111.110 "ls;w;vmstat 1" spawn ssh root@172.16.111.110 root@172.16.111.110's password: Last login: Tue Feb 27 10:53:39 2018 from 172.16.111.100 [root@garytao-02 ~]# ls;w;vmstat 1 123.txt 1_heard.txt 1.txt aming anaconda-ks.cfg.1 shell zabbix-release-3.2-1.el7.noarch.rpm 123.txt~ 1_sorft.txt 2.txt aminglinux rsync yum.log 10:56:21 up 6 days, 9:04, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 172.16.111.1 一18 16:31m 0.00s 0.00s -bash root pts/1 172.16.111.100 10:56 0.00s 0.01s 0.01s w procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 2 0 0 204888 876 239148 0 0 0 1 46 14 0 0 100 0 0 0 0 0 204904 876 239164 0 0 0 21 55 116 0 0 100 0 0 0 0 0 204904 876 239164 0 0 0 0 55 113 0 0 100 0 0 0 0 0 204904 876 239164 0 0 0 0 56 112 0 0 100 0 0 0 0 0 204904 876 239164 0 0 0 0 56 111 0 0 100 0 0 0 0 0 204904 876 239164 0 0 0 0 58 110 0 0 100 0 0 #由於vmstat 1 是持續運行的。因此腳本最後的exit 就沒有辦法執行,只能手動終止
[root@garytao-01 shell]# vi 4.expect 把另一臺機器的tmp下文件12.txt同步到本地tmp文件 增長以下腳本內容: #!/usr/bin/expect set passwd "123456" spawn rsync -av root@172.16.111.110:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof [root@garytao-01 shell]# chmod a+x 4.expect #若是機器上沒有安裝rsync請使用以下命令安裝 [root@garytao-02 ~]# yum -y install rsync
查看同步過程 安全
set timeout 定義超時時間(單位爲 秒) -1 爲永遠不超時bash
[root@garytao-01 shell]# vi 5.expect 增長以下內容: #!/usr/bin/expect set passwd "123456" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -av $file root@$host:$file expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof #file變量定義的文件地址,使用時,必須寫絕對路徑
把文件同步到遠程機器上ssh
[root@garytao-01 shell]# cat 5.expect #!/usr/bin/expect set passwd "123456" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -av $file root@$host:$file expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof [root@garytao-01 shell]# chmod a+x 5.expect [root@garytao-01 shell]# ./5.expect 172.16.111.110 "/tmp/12.txt" spawn rsync -av /tmp/12.txt root@172.16.111.110:/tmp/12.txt root@172.16.111.110's password: sending incremental file list sent 31 bytes received 12 bytes 28.67 bytes/sec total size is 5 speedup is 0.12 [root@garytao-01 shell]#
對於大公司而言,確定時不時會有網站或者配置文件更新,並且使用的機器確定也是好多臺,少則幾臺,多則幾十甚至上百臺。因此,自動同步文件是相當重要的。網站
首先要有一臺模板機器,把要分發的文件準備好,而後只要使用expect腳本批量把須要同步的文件分發到目標機器便可。ui
rsync -av --files-from=list.txt / root@host:/spa
## 建立rsync.expect執行腳本 [root@garytao-01 shell]# vi rsync.expect 增長以下腳本內容: #!/usr/bin/expect set passwd "123456" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -av --files-from=$file / root@$host:/ #這個地方定義了原目錄和目標目錄以跟目錄開 expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof
同步的路徑,須要保證對方機器也有這個相同的路徑,若是沒有路徑,須要使用 -R 建立路徑.net
## file.list內容,爲同步的文件路徑列表 [root@garytao-01 shell]# vi /tmp/file.list 增長以下須要同步的文件路徑: /tmp/12.txt /root/shell/1.sh /root/111/222/lll.txt
由於實現分發系統,確定是由於須要分發的機器數量過大,因此,定義好了 文件 的 list 列表文件之後, 還須要配置 ip 的列表文件
## ip.list內容,爲須要同步的遠程機器IP列表 [root@garytao-01 shell]# vi /tmp/ip.list 172.16.111.110 127.0.0.1
建立一個rsync.sh腳本
[root@garytao-01 shell]# vi rsync.sh #!/bin/bash for ip in `cat /tmp/ip.list` do ./rsync.expect $ip /tmp/file.list done #加權限執行腳本 [root@garytao-01 shell]# chmod a+x rsync.expect #執行過程 [root@garytao-01 shell]# sh -x rsync.sh ++ cat /tmp/ip.list + for ip in '`cat /tmp/ip.list`' + ./rsync.expect 172.16.111.110 /tmp/file.list spawn rsync -avR --files-from=/tmp/file.list / root@172.16.111.110:/ root@172.16.111.110's password: building file list ... rsync: link_stat "/root/shell/1.sh" failed: No such file or directory (2) done root/ root/111/ root/111/222/ root/111/222/lll.txt/ tmp/ sent 130 bytes received 27 bytes 314.00 bytes/sec total size is 5 speedup is 0.03 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9] + for ip in '`cat /tmp/ip.list`' + ./rsync.expect 127.0.0.1 /tmp/file.list spawn rsync -avR --files-from=/tmp/file.list / root@127.0.0.1:/ The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established. ECDSA key fingerprint is 89:19:99:8c:63:ff:d9:e6:19:0d:81:03:27:54:49:78. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts. root@127.0.0.1's password: [root@garytao-01 shell]#
備註:若是不能保證對方機器有相同的路徑就加上R,編輯rsync.expect
注意:作分發系統expect腳本的前提是須要保證機器密碼同樣,這樣會有一個問題就是若是密碼泄露的話就會有安全隱患,因此能夠作密鑰認證增長安全。
[root@garytao-01 shell]# passwd 更改用戶 root 的密碼 。 新的 密碼: 從新輸入新的 密碼: passwd:全部的身份驗證令牌已經成功更新。 [root@garytao-01 shell]#
[root@garytao-01 shell]# vim exe.expect 增長以下腳本內容: #!/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" [root@garytao-01 shell]# chmod a+x exe.expect ## 定義一個exe的sehll腳本 [root@garytao-01 shell]# vim exe.sh 增長以下腳本內容: #!/bin/bash for ip in `cat /tmp/ip.list` do ./exe.expect $ip "hostname" done ##執行腳本 [root@garytao-01 shell]# sh exe.sh spawn ssh root@172.16.111.110 root@172.16.111.110's password: Last login: Tue Feb 27 11:21:39 2018 from 172.16.111.100 [root@garytao-02 ~]# hostname garytao-02 [root@garytao-02 ~]# spawn ssh root@127.0.0.1 root@127.0.0.1's password: Last login: Tue Feb 27 16:52:17 2018 from 172.16.111.1 [root@garytao-01 ~]# hostname garytao-01 [root@garytao-01 ~]# [root@garytao-01 shell]#