Python 自动化运维脚本示例(并行发送sh命令)

Python 自动化运维脚本示例(并行发送sh命令),第1张

概述Python 自动化运维脚本示例并行发送sh命令) 感兴趣Python 自动化运维脚本示例的小伙伴,下面一起跟随内存溢出 jb51.cc的小编来看看吧。
# 内存溢出  (jb51.cc)#!/usr/bin/python# -*- Coding: UTF-8 -*-import paramikoimport sysimport threading #copy local file to Remote Server.def sshclIEnt_scp(hostname,port,username,password,local_path,remote_path):    t = paramiko.Transport((hostname,port))    t.connect(username=username,password=password)  # 登录远程服务器    sftp = paramiko.SFTPClIEnt.from_transport(t)  # sftp传输协议    sftp.put(local_path,remote_path)    t.close() def sshclIEnt_scp_get(hostname,remote_path,local_path):    t = paramiko.Transport((hostname,password=password)  # 登录远程服务器    sftp = paramiko.SFTPClIEnt.from_transport(t)  # sftp传输协议    sftp.get(remote_path,local_path)    t.close() def sshclIEnt_execmd(hostname,execmd):    paramiko.util.log_to_file("paramiko.log")    s = paramiko.SSHClIEnt()    s.set_missing_host_key_policy(paramiko.autoAddPolicy())    s.connect(hostname=hostname,port=port,username=username,password=password)    stdin,stdout,stderr = s.exec_command(execmd)    stdin.write("Y")  # Generally speaking,the first connection,need a simple interaction.    line=stdout.read()    s.close()    print (hostname+":")    print line  try:    file_name = sys.argv[1]    cmd= sys.argv[2]except IndexError:    print 'Wrong params!'    print 'Usage :'    print '       batch.py "$OS_List_file" "$BATCH_EXECUTE_CMD"'    print 'cat osList.txt:'    print '192.168.0.1,22,oracle,passwd1'    print '192.168.0.2,passwd1'    print '192.168.0.3,24,passwd1'    print 'Format is :'    print 'IPADDR,SSHPORT,USERname,PASSWORD'    print 'Examples of usage:'    print './batch.py "/root/workspace/osList.txt" "df -h"'     sys.exit() #file_name = sys.argv[1]#cmd= sys.argv[2] #maintenance_osinfowith open(file_name) as file_object:    for line in file_object:        splits_str = line.rstrip().split(',')        a=threading.Thread(target=sshclIEnt_execmd,args=(splits_str[0],int(splits_str[1]),splits_str[2],splits_str[3],cmd))        a.start()        #print sshclIEnt_execmd(splits_str[0],cmd)#        print sshclIEnt_scp(splits_str[0],file_name,splits_str[4]+file_name) #  End 5.1.2笔记-jb51.cc
  总结

以上是内存溢出为你收集整理的Python 自动化运维脚本示例(并行发送sh命令)全部内容,希望文章能够帮你解决Python 自动化运维脚本示例(并行发送sh命令)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1199884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存