码迷,mamicode.com
首页 > 编程语言 > 详细

基于python一个分发小脚本

时间:2016-05-06 16:36:26      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:python

基于paramiko分发公钥到其他机器,有不足的地方还望见谅

#-*- coding: utf-8 -*-
#!/usr/bin/python 
import os
import paramiko
import threading
data=‘ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAvQfqvW1JjYhRVgqYjBCLsTWNx0q/X+IcHdATG0MvJ5Nrew08eoEiy4/rScsUiQaMLaIApTa0Smn5Yg9vP0Z4ESfqMEye3KuBx4R+Y+XvImIL2y3FTkMO9A5ecOegWmJCfoGNBoA9NwYlPF1yAhyhjuVVmqKJc8vgzRo5gTUmAEWaM509l5TuV4OlR6dYzDhHsSXkPeWdpSfUbseg2pUmtOva/n9fgCIj1k7z1nv2SLWxRosnedO20dYIbNR0f1pzH6eMHILDuwohS5n9jbORyqaQaSQ3q9o421pVCOv6PftfPIm9wpN+zRp7pQ6oUTxdxIXkNjFmjL15IOb03SxKEw== root@x-shcs-creditcard-v02‘
def ssh2(ip,username,passwd,cmd):
    try:
        paramiko.util.log_to_file(‘paramiko________.log‘)
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        #ssh.connect(ip,22,username,passwd,allow_agent=False,look_for_keys=False,timeout=5)
        ssh.connect(ip,22,username,passwd,timeout=5)
        for m in cmd:
            stdin, stdout, stderr = ssh.exec_command(m)
            print stdout.readlines()
            sftp = ssh.open_sftp()
            remote_file = sftp.file(‘/root/.ssh/authorized_keys‘, ‘wb‘)
            remote_file.set_pipelined(True)
            remote_file.write(data)
        print ‘%s\tOK\n‘%(ip)
        sftp.close()
        ssh.close()
    except :
        print ‘%s\tError\n‘%(ip)
if __name__==‘__main__‘:
    username="root"
    passwd="Test@2016"
    cmd=[‘if(! -d /root/.ssh/);then mkdir /root/.ssh/;fi‘]
    #cmd=[‘w‘]
    threads=[]
    print "Begine....."
    for i in range(200,217):
        ip=‘192.168.50.‘+str(i)
        #print ip
        begine=threading.Thread(target=ssh2,args=(ip,username,passwd,cmd))
        #ssh2(ip,username,passwd)
        begine.start()


基于python一个分发小脚本

标签:python

原文地址:http://liuyusheng.blog.51cto.com/10570745/1770623

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