以下程序均来自《Python.UNIX和Linux系统管理指南》
首先要设置ssh无密码登陆
操作步骤:
1. 生成ssh密钥
[root@os1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
65:55:08:91:6b:7e:06:e6:17:e6:c3:6b:c6:db:94:3c root@os1
2. 将公共密钥拷贝到被远程的机器上
[root@os1 ~]# cd /root/.ssh/
[root@os1 .ssh]# scp id_rsa.pub root@10.33.0.223:/root/.ssh
3. 在被远程的机器上执行
[root@os2 .ssh]# cat id_rsa.pub > authorized_keys
4. 测试ssh登陆
[root@os1 .ssh]# ssh root@10.33.0.223
Last login: Tue Jul 9 11:26:29 2013 from 10.33.1.28
[root@os2 ~]# exit
logout
Connection to 10.33.0.223 closed.
一个简单的基于ssh的分发器
dispatch.py #!/usr/bin/env python import subprocess machines = ["192.168.137.2", "192.168.137.3"] cmd = "uname -a" for machine in machines: subprocess.call("ssh root@%s %s" % (machine, cmd), shell=True)
运行结果:
[root@centos python]# python dispatch.py
Linux centos 2.6.18-308.el5 #1 SMP Tue Feb 21 20:05:41 EST 2012 i686 i686 i386 GNU/Linux
Linux centos-zh 2.6.18-308.el5 #1 SMP Tue Feb 21 20:05:41 EST 2012 i686 i686 i386 GNU/Linux
一个高级的基于ssh的分发器,将要远程连接的机器IP和要执行的命令写在配置文件config.ini里,然后读取配置文件来执行
config.ini [MACHINES] CENTOS: 192.168.137.2 CENTOS2: 192.168.137.3 [COMMANDS] uname: uname -a advanced_dispatch.py #!/usr/bin/env python import subprocess import ConfigParser def readConfig(file="config.ini"): ips = [] cmds = [] Config = ConfigParser.ConfigParser() Config.read(file) machines = Config.items("MACHINES") commands = Config.items("COMMANDS") for ip in machines: ips.append(ip[1]) for cmd in commands: cmds.append(cmd[1]) return ips, cmds ips, cmds = readConfig() for ip in ips: for cmd in cmds: subprocess.call("ssh root@%s %s" %(ip, cmd), shell=True)
运行结果:
[root@centos python]# python advanced_dispatch.py
Linux centos 2.6.18-308.el5 #1 SMP Tue Feb 21 20:05:41 EST 2012 i686 i686 i386 GNU/Linux
Linux centos-zh 2.6.18-308.el5 #1 SMP Tue Feb 21 20:05:41 EST 2012 i686 i686 i386 GNU/Linux
基于ssh的多线程分发器
配置文件同上config.ini
threaded_dispatch.py #!/usr/bin/env python import subprocess import ConfigParser from threading import Thread from Queue import Queue import time """ A threaded ssh-based command dispatch system """ start = time.time() queue = Queue() def readConfig(file="config.ini"): ips = [] cmds = [] Config = ConfigParser.ConfigParser() Config.read(file) machines = Config.items("MACHINES") commands = Config.items("COMMANDS") for ip in machines: ips.append(ip[1]) for cmd in commands: cmds.append(cmd[1]) return ips, cmds def launcher(i, q, cmd): while True: ip = q.get() print "Thread %s: Running %s to %s" % (i, cmd, ip) subprocess.call("ssh root@%s %s" % (ip, cmd), shell=True) q.task_done() ips, cmds = readConfig() if len(ips) < 25: num_threads = len(ips) else: num_threads = 25 for i in range(num_threads): for cmd in cmds: worker = Thread(target=launcher, args=(i, queue, cmd)) worker.setDaemon(True) worker.start() print "Main Thread waiting" print ips for ip in ips: queue.put(ip) queue.join() end = time.time()print "Dispatch Completed in %s seconds" % str(end - start)
运行结果:
[root@centos python]# python threaded_dispatch.py
Main Thread waiting
['192.168.137.2', '192.168.137.3']
Thread 0: Running uname -a to 192.168.137.2
Thread 1: Running uname -a to 192.168.137.3
Linux centos 2.6.18-308.el5 #1 SMP Tue Feb 21 20:05:41 EST 2012 i686 i686 i386 GNU/Linux
Linux centos-zh 2.6.18-308.el5 #1 SMP Tue Feb 21 20:05:41 EST 2012 i686 i686 i386 GNU/Linux
Dispatch Completed in 10.2910089493 seconds
注意:最后一个多线程,只能执行一条命令,执行多个命令时会有问题