
程序执行时需要读取两个文件command.txt和ipandpass.txt。格式如下:
ipandpass.txt:
ip username password
程序中的队列操作是修改的别的程序,写的确实不错。
该程序亦正亦邪,如果拿去做坏事,我先声明与我无关,我只是分享我的代码罢了。
希望有兴趣的同志们来讨论技术应用。
这其中用到了paramiko,队列,多线程,后续也会写一些这三个方面的东西。欢迎批评指正。
其实这个程序有些地方还有待优化。
#function:upload files through ssh protocal and excute the files
#lib:paramiko
#MyThread:init a thread to run the function
#ThreadPol:init a thread pool
#uploadAndExecu:upload file and excute
#readConf:read config file
#-*- coding = utf-8 -*-
import Queue
import sys
import threading
import paramiko
import socket
from threading import Thread
import time
class MyThread(Thread):
def __init__(self, workQueue, timeout=1):
Thread.__init__(self)
self.timeout = timeout
self.setDaemon(False)
self.workQueue = workQueue
self.start()
#print 'i am runnning ...'
def run(self):
emptyQueue = 0
while True:
try:
callable, username, password, ipAddress, port,comms = self.workQueue.get(timeout = self.timeout)
#print 'attacking :',ipAddress,username,password,threading.currentThread().getName(),' time : '
callable(username,password, ipAddress, port,comms)
except Queue.Empty:
print threading.currentThread().getName(),":queue is empty ; sleep 5 seconds\n"
emptyQueue += 1
#judge the queue,if it is empty or not.
time.sleep(5)
if emptyQueue == 5:
print threading.currentThread().getName(),'i quit,the queue is empty'
break
except Exception, error:
print error
class ThreadPool:
def __init__(self, num_of_threads=10):
self.workQueue = Queue.Queue()
self.threads = []
self.__createThreadPool(num_of_threads)
#create the threads pool
def __createThreadPool(self, num_of_threads):
for i in range(num_of_threads):
thread = MyThread(self.workQueue)
self.threads.append(thread)
def wait_for_complete(self):
#print len(self.threads)
while len(self.threads):
thread = self.threads.pop()
if thread.isAlive():
thread.join()
def add_job(self, callable, username, password, ipAddress, Port,comms):
self.workQueue.put((callable, username, password, ipAddress, Port,comms))
def uploadAndExecu(usernam,password,hostname,port,comm):
print usernam,password,hostname,port,comm
try:
t = paramiko.Transport((hostname,int(port)))
t.connect(username=username,password=password)
sftp=paramiko.SFTPClient.from_transport(t)
sftp.put(comm['local_dir'],comm['remote_dir'])
except Exception,e:
print 'upload files failed:',e
t.close()
finally:
t.close()
try:
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
ssh.connect(hostname, port=int(port), username=username, password=password)
ssh.exec_command(comm['alter_auth'])
ssh.exec_command(comm['exec_program'])
except Exception,e:
print 'chang file auth or execute the file failed:',e
ssh.close()
def readConf():
comm={}
try:
f = file('command.txt','r')
for l in f:
sp = l.split(':')
comm[sp[0]]=sp[1].strip('\n')
except Exception,e:
print 'open file command.txt failed:',e
f.close()
return comm
if __name__ == "__main__":
commandLine = readConf()
print commandLine
#prepare the ips
wm = ThreadPool(int(commandLine['ThreadNum']))
try:
ipFile = file('ipandpass.txt','r')
except:
print "[-] ip.txt Open file Failed!"
sys.exit(1)
for line in ipFile:
IpAdd,username,pwd = line.strip('\r\n').split(' ')
wm.add_job(uploadAndExecu,username,pwd,IpAdd,commandLine['port'],commandLine)
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
Power BI 热力地图制作指南:从数据准备到实战分析 在数据可视化领域,热力地图凭借 “直观呈现数据密度与分布趋势” 的核心优势 ...
2025-08-20PyTorch 矩阵运算加速库:从原理到实践的全面解析 在深度学习领域,矩阵运算堪称 “计算基石”。无论是卷积神经网络(CNN)中的 ...
2025-08-20数据建模:CDA 数据分析师的核心驱动力 在数字经济浪潮中,数据已成为企业决策的核心资产。CDA(Certified Data Analyst)数据分 ...
2025-08-20KS 曲线不光滑:模型评估的隐形陷阱,从原因到破局的全指南 在分类模型(如风控违约预测、电商用户流失预警、医疗疾病诊断)的评 ...
2025-08-20偏态分布:揭开数据背后的非对称真相,赋能精准决策 在数据分析的世界里,“正态分布” 常被视为 “理想模型”—— 数据围绕均值 ...
2025-08-19CDA 数据分析师:数字化时代的价值创造者与决策智囊 在数据洪流席卷全球的今天,“数据驱动” 已从企业战略口号落地为核心 ...
2025-08-19CDA 数据分析师:善用 Power BI 索引列,提升数据处理与分析效率 在 Power BI 数据分析流程中,“数据准备” 是决定后续分析质量 ...
2025-08-18CDA 数据分析师:巧用 SQL 多个聚合函数,解锁数据多维洞察 在企业数据分析场景中,单一维度的统计(如 “总销售额”“用户总数 ...
2025-08-18CDA 数据分析师:驾驭表格结构数据的核心角色与实践应用 在企业日常数据存储与分析场景中,表格结构数据(如 Excel 表格、数据库 ...
2025-08-18PowerBI 累计曲线制作指南:从 DAX 度量到可视化落地 在业务数据分析中,“累计趋势” 是衡量业务进展的核心视角 —— 无论是 “ ...
2025-08-15Python 函数 return 多个数据:用法、实例与实战技巧 在 Python 编程中,函数是代码复用与逻辑封装的核心载体。多数场景下,我们 ...
2025-08-15CDA 数据分析师:引领商业数据分析体系构建,筑牢企业数据驱动根基 在数字化转型深化的今天,企业对数据的依赖已从 “零散分析” ...
2025-08-15随机森林中特征重要性(Feature Importance)排名解析 在机器学习领域,随机森林因其出色的预测性能和对高维数据的适应性,被广 ...
2025-08-14t 统计量为负数时的分布计算方法与解析 在统计学假设检验中,t 统计量是常用的重要指标,其分布特征直接影响着检验结果的判断。 ...
2025-08-14CDA 数据分析师与业务数据分析步骤 在当今数据驱动的商业世界中,数据分析已成为企业决策和发展的核心驱动力。CDA 数据分析师作 ...
2025-08-14前台流量与后台流量:数据链路中的双重镜像 在商业数据分析体系中,流量数据是洞察用户行为与系统效能的核心依据。前台流量与 ...
2025-08-13商业数据分析体系构建与 CDA 数据分析师的协同赋能 在企业数字化转型的浪潮中,商业数据分析已从 “可选工具” 升级为 “核 ...
2025-08-13解析 CDA 数据分析师:数据时代的价值挖掘者 在数字经济高速发展的今天,数据已成为企业核心资产,而将数据转化为商业价值的 ...
2025-08-13解析 response.text 与 response.content 的核心区别 在网络数据请求与处理的场景中,开发者经常需要从服务器返回的响应中提取数 ...
2025-08-12MySQL 统计连续每天数据:从业务需求到技术实现 在数据分析场景中,连续日期的数据统计是衡量业务连续性的重要手段 —— 无论是 ...
2025-08-12