yum install -y expectphp
自動遠程登陸mysql
[root@aminglinux-02 sbin]# vim 1.expect #! /usr/bin/expect set host "192.168.133.132" set passwd "123456" spawn ssh root@$host expect { "yes/no" { send "yes\r"; exp_continue} // "password:" { send "$passwd\r" } } interact //腳本結束
加入執行權限linux
[root@aminglinux-02 sbin]# ./1.expect -bash: ./1.expect: 權限不夠 [root@aminglinux-02 sbin]# chmod a+x 1.expect
[root@aminglinux-02 sbin]# ./1.expect spawn ssh root@192.168.133.130 root@192.168.133.130's password: Last failed login: Wed Sep 20 23:31:03 CST 2017 from 192.168.133.131 on ssh:notty There was 1 failed login attempt since the last successful login. Last login: Wed Sep 20 22:29:52 2017 from 192.168.133.1 [root@aminglinux-01 ~]# ^C
#!/usr/bin/expect set user "root" set passwd "123123a" spawn ssh $user@192.168.133.130 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 "]" : 「」 通配]右邊全部字符,nginx
send 執行命令sql
A 機器執行腳本編程
[root@aminglinux-02 sbin]# ./2.expect -bash: ./2.expect: 權限不夠 [root@aminglinux-02 sbin]# chmod a+x 2.expect [root@aminglinux-02 sbin]# ./!$ ./2.expect spawn ssh root@192.168.133.130 root@192.168.133.130's password: Last login: Wed Sep 20 23:31:49 2017 from 192.168.133.131 [root@aminglinux-01 ~]# touch /tmp/12.txt [root@aminglinux-01 ~]# echo 1212 > /tmp/12.txt
[root@aminglinux-01 ~]# ls /tmp/ 12.txt systemd-private-7723d189c8d3469ab732edadbbc340cb-nginx.service-eKHAIK mysql.sock systemd-private-7723d189c8d3469ab732edadbbc340cb-vmtoolsd.service-1yxB4M [root@aminglinux-01 ~]# cat /tmp/12.txt 1212
#!/usr/bin/expect set user [lindex $argv 0] //第一個參數 set host [lindex $argv 1] //第二個參數 set passwd "123123a" 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@aminglinux-02 sbin]# ./3.expect root 192.168.133.130 ls spawn ssh root@192.168.133.130 root@192.168.133.130's password: Last failed login: Thu Sep 21 01:57:01 CST 2017 from 192.168.133.131 on ssh:notty There was 1 failed login attempt since the last successful login. Last login: Thu Sep 21 01:51:46 2017 from 192.168.133.1 [root@aminglinux-01 ~]# ls anaconda-ks.cfg
[root@aminglinux-02 sbin]# ./3.expect root 192.168.133.130 "ls;whoami;vmstat 1" spawn ssh root@192.168.133.130 root@192.168.133.130's password: Last login: Thu Sep 21 01:57:21 2017 from 192.168.133.131 [root@aminglinux-01 ~]# ls;whoami;vmstat 1 anaconda-ks.cfg root 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 1470532 876 189616 0 0 12 2 42 93 0 0 100 0 0 0 0 0 1470532 876 189616 0 0 0 0 41 91 0 1 99 0 0 0 0 0 1470532 876 189616 0 0 0 0 33 74 0 0 100 0 0 0 0 0 1470532 876 189616 0 0 0 0 37 83 0 0 100 0 0 0 0 0 1470532 876 189616 0 0 0 0 35 77 0 0 100 0 0 ^C