本文共 3662 字,大约阅读时间需要 12 分钟。
1.安装命令:
[root@weixing01 ~]# yum install -y expect已加载插件:fastestmirrorbase | 3.6 kB 00:00:00 epel/x86_64/metalink | 8.1 kB 00:00:00 epel
2.自动远程登录并执行:interact表示留在远程,不退出
[root@weixing01 sbin]# vi 1.expect#! /usr/bin/expectset host "192.168.188.129"set passwd "w914"spawn ssh root@$hostexpect {"yes/no" { send "yes\r"; exp_continue}"password:" { send "$passwd\r" }}interact
[root@weixing01 sbin]# chmod a+x 1.expect [root@weixing01 sbin]# ./1.expect spawn ssh root@192.168.188.129The authenticity of host '192.168.188.129 (192.168.188.129)' can't be established.ECDSA key fingerprint is SHA256:SL6ZOvpHHtEoro8AzeNBXqrM3mr2oXbPaSeXO+LQr1U.ECDSA key fingerprint is MD5:26:0e:7a:96:d6:3a:c5:57:57:2b:6d:1a:1e:42:c9:01.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.188.129' (ECDSA) to the list of known hosts.root@192.168.188.129's password: Last login: Wed Apr 25 21:24:50 2018 from 192.168.188.1
3.远程登录并执行命令,然后退出:
[root@weixing01 sbin]# vi 2.expect#!/usr/bin/expectset user "root"set passwd "w4"spawn ssh $user@192.168.188.129expect {"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@weixing01 sbin]# chmod a+x 2.expect [root@weixing01 sbin]# ./2.expect spawn ssh root@192.168.188.129root@192.168.188.129's password: Last login: Wed Apr 25 21:30:12 2018 from 192.168.188.130[root@weixing-02 ~]# touch /tmp/12.txt[root@weixing-02 ~]# echo 1212 > /tmp/12.txt
检测运行
[root@weixing-02 ~]# [root@weixing01 sbin]# ./1.expect spawn ssh root@192.168.188.129root@192.168.188.129's password: Last login: Wed Apr 25 21:36:07 2018 from 192.168.188.130[root@weixing-02 ~]# ls -l /tmp/12.txt -rw-r--r-- 1 root root 5 4月 25 21:36 /tmp/12.txt[root@weixing-02 ~]# cat /tmp/12.txt 1212
4.传递参数:
[root@weixing01 sbin]# vim 3.expect#!/usr/bin/expectset user [lindex $argv 0]set host [lindex $argv 1]set passwd "w4"set cm [lindex $argv 2]spawn ssh $user@$hostexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect "]*"send "$cm\r"expect "]*"send "exit\r"
[root@weixing01 sbin]# chmod a+x 3.expect [root@weixing01 sbin]# ./3.expect root 192.168.188.129 lsspawn ssh root@192.168.188.129root@192.168.188.129's password: Last login: Wed Apr 25 21:36:46 2018 from 192.168.188.130[root@weixing-02 ~]# ls1.txt 2.txt anaconda-ks.cfg a.txt BugReport.txt grep
执行多条命令
[root@weixing-02 ~]# [root@weixing01 sbin]# ./3.expect root 192.168.188.129 "ls;w;vmstat 1"spawn ssh root@192.168.188.129root@192.168.188.129's password: Last login: Wed Apr 25 21:44:15 2018 from 192.168.188.130[root@weixing-02 ~]# ls;w;vmstat 11.txt 2.txt anaconda-ks.cfg a.txt BugReport.txt grep 21:44:43 up 20 min, 2 users, load average: 0.00, 0.01, 0.03USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.188.1 21:24 17:31 0.02s 0.02s -bashroot pts/1 192.168.188.130 21:44 0.00s 0.01s 0.00s wprocs -----------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 305904 2076 142944 0 0 102 9 111 162 0 1 99 0 0 0 0 0 305904 2076 142936 0 0 0 0 59 75 0 0 100 0 0 0 0 0 305904 2076 142936 0 0 0 16 68 80 0 0 100 0 0 0 0 0 305904 2076 142936 0 0 0 0 54 65 0 0 100 0 0
转载于:https://blog.51cto.com/13517254/2107866