京公网安备 11010802034615号
经营许可证编号:京B2-20210330
程序执行时需要读取两个文件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
在自动化办公、数据采集、定时统计、日志清理、系统监控等场景中,程序往往需要按照固定时间间隔重复执行指定任务,这种运行机制 ...
2026-08-12在数据分析日常工作中,Excel数据筛选是数据清洗、数据提取、样本筛选的核心基础操作。传统Excel手动筛选、函数筛选方式,面对多 ...
2026-08-12 很多数据分析师精通Excel函数和数据透视表,但当被问到“数据从哪里来”“表和视图有什么区别”“数据库管理系统和SQL是什么 ...
2026-08-12在数据分析、统计建模、数据挖掘与商业调研过程中,原始数据往往无法做到绝对干净规整。受系统故障、人工录入失误、设备误差、突 ...
2026-08-11在数据分析工作中,聚类分析是典型的无监督学习方法,核心作用是依据数据自身的多维特征,将相似样本自动划分为若干类别,实现“ ...
2026-08-11 很多企业团队并非缺乏指标,而是陷入“指标失控”:仪表盘上堆满实时跳动的数据,却无法回答“当前瓶颈在哪、下一步该做什么 ...
2026-08-11AB实验是互联网产品迭代、营销优化、功能升级的核心科学验证手段,通过流量随机分组、对照组与实验组对比,科学验证策略、功能、 ...
2026-08-10在MySQL数据库优化中,索引是提升查询效率、降低数据库IO开销、优化系统性能的核心手段。普通单列索引仅适配简单查询场景,面对 ...
2026-08-10 很多数据分析师每天盯着几十个指标,但当被问到“这套指标要支撑什么业务目标”“指标之间是什么逻辑关系”“业务变化时如何 ...
2026-08-10在数字化市场调研体系中,大数据与小数据是两类核心调研数据形态,分别对应海量行为统计与精准样本深度调研。行业普遍存在认知误 ...
2026-08-07数据透视表是Excel、WPS中最核心的数据分析工具,凭借快速汇总、分组统计、动态筛选的优势,被广泛应用于销量统计、业绩复盘、数 ...
2026-08-07 很多数据分析师每天盯着GMV、DAU、转化率,但当被问到“哪些指标在所有行业都适用”“哪些指标只对电商有意义”“二者如何搭 ...
2026-08-07在商品销量、市场需求、营收规模等业务数据中,季节性波动是最普遍、最核心的数据特征。零售快消、食品餐饮、家电服饰、电商行业 ...
2026-08-06在流量红利消退、市场竞争白热化的商业环境中,传统依托经验、跟风投放、广撒网式的营销模式,逐渐暴露出成本高、精准度低、转化 ...
2026-08-06 很多数据分析师每天盯着GMV、DAU、转化率,但当被问到“什么是指标”“指标和维度有什么区别”“如何定义指标值的计算规则和 ...
2026-08-06【核心关键词】知识、设备、工程师、数字化、建模、算法、工业大数据、数据分析、控制算法、机器学习、深度学习、业务目标、工 ...
2026-08-05增长率是数据分析、业务报表、可视化看板中最核心的指标之一,常用于衡量营收、销量、用户量、流量等业务数据的涨跌幅度与发展趋 ...
2026-08-05小陈是某电商平台的数据分析师。老板交给他一个任务:“我们平台的注册用户已经突破1000万了,想了解一下用户的平均月消费金额。 ...
2026-08-05在机器学习建模、数据预处理、抽样划分、模型初始化训练的全流程中,随机种子是控制实验随机性、保证结果可复现、提升模型稳定性 ...
2026-08-04Python作为面向对象的主流编程语言,核心编程体系由变量、方法、类三大基础要素构成。三者层层递进、相互协作,支撑起所有基础代 ...
2026-08-04