标签:can chm tty spawn man 网站 perm rsync 配置
分发系统-expect讲解[root@akuilinux01 sbin]# vim 1.expect
[root@akuilinux01 sbin]# vim 1.expect
#! /usr/bin/expect
set host "192.168.21.129"
set passwd "s5381561"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
interact
[root@akuilinux01 sbin]# chmod a+x 1.expect
root@akuilinux01 sbin]# ./1.expect
spawn ssh root@192.168.21.129
The authenticity of host ‘192.168.21.129 (192.168.21.129)‘ can‘t be established.
ECDSA key fingerprint is SHA256:r2y+hZVvvomXE4d3uRSM0cO+kMdqyWAOMqpTtA2qp6I.
ECDSA key fingerprint is MD5:ee:da:3b:ea:27:56:38:82:bc:e8:73:10:18:a0:c2:bd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.21.129‘ (ECDSA) to the list of known hosts.
root@192.168.21.129‘s password:
Last login: Wed Jul 11 21:59:23 2018 from 192.168.21.1
[root@akuilinux02 ~]#
[root@akuilinux01 sbin]# vim 2.expect
#!/usr/bin/expect
set user "root"
set passwd "s5381561"
spawn ssh $user@192.168.21.129
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@akuilinux01 sbin]# chmod a+x 2.expect
[root@akuilinux01 sbin]# ./2.expect
spawn ssh root@192.168.21.129
root@192.168.21.129‘s password:
Last login: Sun Jul 22 22:07:20 2018 from 192.168.21.128
[root@akuilinux02 ~]# touch /tmp/12.txt
[root@akuilinux02 ~]# echo 1212 > /tmp/12.txt
[root@akuilinux02 ~]# [root@akuilinux01 sbin]#
[root@akuilinux02 ~]# ls /tmp/
12.txt
mysql.sock
systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-chronyd.service-5DHBEF
systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-vgauthd.service-sG39NN
systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-vmtoolsd.service-CVypi4
[root@akuilinux02 ~]# cat /tmp/12.txt
1212
[root@akuilinux01 sbin]# vim 3.expect
#!/usr/bin/expect
set user [lindex $argv 0] #第一个参数赋值给user
set host [lindex $argv 1] #第二个参数
set passwd "s5381561"
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"
[root@akuilinux01 sbin]# chmod a+x 3.expect
[root@akuilinux01 sbin]# ./3.expect root 192.168.21.129 ls
spawn ssh root@192.168.21.129
root@192.168.21.129‘s password:
Last login: Sun Jul 22 22:14:12 2018 from 192.168.21.1
[root@akuilinux02 ~]# ls
111 anaconda-ks.cfg
[root@akuilinux02 ~]# [root@akuilinux01 sbin]#
[root@akuilinux02 ~]# [root@akuilinux01 sbin]# ./3.expect root 192.168.21.129 "ls;w;vmstat 1"
spawn ssh root@192.168.21.129
root@192.168.21.129‘s password:
Last login: Sun Jul 22 22:19:35 2018 from 192.168.21.128
[root@akuilinux02 ~]# ls;w;vmstat 1
111 anaconda-ks.cfg
22:19:51 up 41 min, 2 users, load average: 0.00, 0.02, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.21.1 22:14 55.00s 0.01s 0.01s -bash
root pts/1 192.168.21.128 22:19 0.00s 0.01s 0.00s 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 1142532 2076 173112 0 0 15 14 51 44 0 0 99 0 0
0 0 0 1142780 2076 173208 0 0 0 6 257 149 0 0 100 0 0
0 0 0 1142780 2076 173208 0 0 0 294 188 181 0 0 100 0 0
0 0 0 1142780 2076 173208 0 0 0 0 122 112 0 0 100 0 0
0 0 0 1142780 2076 173208 0 0 0 0 116 105 0 0 100 0 0
0 0 0 1142780 2076 173208 0 0 0 0 107 99 0 0 100 0 0
0 0 0 1142780 2076 173208 0 0 0 0 160 136 0 0 100 0 0
0 0 0 1142780 2076 173208 0 0 0 5 124 119 0 0 100 0 0
0 0 0 1142780 2076 173208 0 0 0 168 157 157 0 0 100 0 0
0 0 0 1142780 2076 173208 0 0 0 0 102 99 0 0 100 0 0
0 0 0 1142780 2076 173208 0 0 0 0 119 113 0 0 100 0 0
[root@akuilinux01 sbin]#
分发系统-expect讲解,expect脚本远程登录,expect脚本远程执行命令,expect脚本
标签:can chm tty spawn man 网站 perm rsync 配置
原文地址:http://blog.51cto.com/akui2521/2148777