第三十一天-两个例题


1、在A服务器上远程查看B服务器上的信息

[oldboy@A ~]$ ssh 192.168.1.113 hostname
B
[oldboy@A ~]$ ssh 192.168.1.113 uname -r
2.6.32-431.el6.x86_64
[oldboy@A ~]$ ssh 192.168.1.113 uname -a
Linux B 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
[oldboy@A ~]$ ssh 192.168.1.113 free
             total       used       free     shared    buffers     cached
Mem:       1020348     174508     845840          0      16092      61660
-/+ buffers/cache:      96756     923592
Swap:      1023992          0    1023992
[oldboy@A ~]$ ssh 192.168.1.113 uptime
 07:03:23 up 5 min,  1 user,  load average: 0.01, 0.08, 0.06
[oldboy@A ~]$ ssh 192.168.1.113 cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.5 (Santiago)

2、实现expect无交互式批量分发密钥到B、C,即在没有建立免密码认证之前分发密钥实现非交互批量发布。
执行:

[oldboy@B ~]$ ls
fenfa_sshkey.exp  fenfa_sshkey.sh  iplist
[oldboy@B ~]$ sh fenfa_sshkey.sh
[oldboy@B ~]$ cat fenfa_sshkey.sh
#!/bin/sh

./etc/init.d/functions

for ip in `cat iplist`
do
#   expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub $ip >/dev/null 2>&1
    expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub $ip
    if [ $? -eq 0 ]
    then
        action "$ip" /bin/true
    else
        action "$ip" /bin/false
    fi
done

[oldboy@B .ssh]$ 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 "lrz0412"
#spawn scp /etc/hosts root@10.0.0.142:/etc/hosts
#spawn scp -P52113 $file oldboy@$host:$dir
#spawn ssh-copy-id -i $file "-p 52113 oldboy@$host"

spawn ssh-copy-id -i $file "-p 22 oldboy@$host"
expect {
    "yes/no"      {send "yes\r";exp_continue}
    "*password"    {send "$password\r"}

}
expect eof
exit -onexit {
    send_user "bye!"
}

 

posted @ 2015-12-20 23:15  幻月0412  阅读(259)  评论(0编辑  收藏  举报