码迷,mamicode.com
首页 > 其他好文 > 详细

第三十一天-两个例题

时间:2015-12-21 00:13:17      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:


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!"
}

 

第三十一天-两个例题

标签:

原文地址:http://www.cnblogs.com/migongci0412/p/5062191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!