1. 程式人生 > >expect非交互式功能實戰

expect非交互式功能實戰

linux

非交互式工具:expect,sshpass,pash

在管理機m01上面安裝expece

[[email protected] ~]# rpm -qa expect #檢查有沒有安裝expect
[[email protected] ~]# yum install expect -y #用yum安裝expect

安裝完後再查看是否有

[[email protected] ~]# rpm -qa expect
expect-5.44.1.15-5.el6_4.x86_64

檢查已經安裝


非交互式生成密鑰及實現批量管理:

1、所有機器創建用戶及密碼

useradd oldgirl888

echo 123456|passwd --stdin oldgirl888

id oldgirl888

su - oldgirl888

2、生成密鑰對

[[email protected] ~]$ ssh-keygen -t dsa -P ‘‘ -f ~/.ssh/id_dsa >/dev/null 2>&1

3、分發密鑰

ssh-copy-id -i .ssh/id_dsa.pub "-p 52113 [email protected]"

[[email protected] ~]# yum install lrzsz -y

[[email protected]
/* */ ~]$ pwd /home/oldgirl888 在家目錄寫入如下腳本 [[email protected] ~]$ cat fenfa_sshkey.exp #!/usr/bin/expect if { $argc !=2 } { send_user "usage: expect fenfa_sshkey.exp file host\n" exit } #define var set file [lindex $argv 0] set host [lindex $argv 1] set password "123456" #spawn scp /etc/hosts [email protected]
/* */:/etc/hosts #spawn scp -P52113 $file oldboy@$host:$dir spawn ssh-copy-id -i $file "-p 22 oldgirl888@$host" expect { "yes/no" {send "yes\r" ;exp_continue} "password" {send "$password\r"} } expect cof exit -onexit { send_user "oldboy say good bye to you!\n" } #script usage #expect oldboy-6.exp file host dir #example #expect fenfa_sshkey.exp file host dir #expect fenfa_sshkey.exp ~/etc/hosts 10.0.0.41:~

然後運行:

[[email protected] ~]$ expect fenfa_sshkey.exp .ssh/id_dsa.pub 172.16.1.31

在nfs服務器上面可以看到分發的公鑰了

[[email protected] ~]$ ls
[[email protected] ~]$ ls .ssh
authorized_keys


在m01機器上驗證公鑰是否生效:

[[email protected] ~]$ ssh -p22 [email protected] /sbin/ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:EB:DA:9F  
          inet addr:10.0.0.31  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:feeb:da9f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:345 errors:0 dropped:0 overruns:0 frame:0
          TX packets:255 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:29478 (28.7 KiB)  TX bytes:24332 (23.7 KiB)

這就說明自動分發公鑰成功了。

批量搞:

[[email protected] ~]$ cat fenfa_sshkey.sh 
#!/bin/sh
. /etc/init.d/functions
for ip in 8 31 41
do
 expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub 172.16.1.$ip >/dev/null 2>&1
 if [ $? -eq 0 ];then
  action "$ip" /bin/true
 else
   action "$ip" /bin/false
 fi
done

運行腳本:

[[email protected] ~]$ sh fenfa_sshkey.sh 
8                                                          [  OK  ]
31                                                         [FAILED] #報錯是因為第一次測試已經傳遞完了公鑰了。
41                                                         [  OK  ]

再次驗證:在m01機器上面ssh遠程41機器執行ifconfig命令沒有出現提示輸入密碼,驗證成功。

[[email protected] ~]$ ssh -p22 [email protected] /sbin/ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:2D:96:D8  
          inet addr:10.0.0.41  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe2d:96d8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:329 errors:0 dropped:0 overruns:0 frame:0
          TX packets:255 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:27769 (27.1 KiB)  TX bytes:23496 (22.9 KiB)


然後在管理機上面就可以分發軟件了

本文出自 “sandshell” 博客,請務必保留此出處http://sandshell.blog.51cto.com/9055959/1956902

expect非交互式功能實戰