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

python 之 paramiko

时间:2018-10-04 17:50:26      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:code   nbsp   usr   cli   roo   read   HCL   exce   __name__   

 

#!/usr/bin/python


import paramiko

import traceback


user=‘root‘

ipfile = ‘/root/ipfile‘

cmd = r‘‘‘

#!/bin/sh

/usr/local/nginx/sbin/nginx -t

if [ $? -eq 0 ]; then

        /usr/local/nginx/sbin/nginx -s reload

fi

‘‘‘


def get_ip():

    iplist = []

    f = file(ipfile,‘r‘)

    while True:

        ip = f.readline().strip()

        if len(ip) == 0:

            break

        else:

            iplist.append(ip)

    f.close()

    return iplist




if __name__ == "__main__":

    iplist = get_ip()

    for ip in iplist:

        try:

            ssh = paramiko.SSHClient()

            ssh.load_system_host_keys()

            ssh.connect(ip,username=user,timeout=5)

            print "%s" % (ip)

            stdin,stdout,stderr = ssh.exec_command(cmd)

            print stdout.read()

            print stderr.read()

            ssh.close()

        except:

            exstr=traceback.format_exc()

            print "[%s] : %s" % (ip.ljust(18),exstr)

            ssh.close()

            continue

python 之 paramiko

标签:code   nbsp   usr   cli   roo   read   HCL   exce   __name__   

原文地址:https://www.cnblogs.com/feiyun8616/p/9742651.html

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