跨平台信息获取小工具第三版本(增加了继承、多线程、异常处理模块、excel表格内容剔除空格)
# coding=utf-8 import threading
import paramiko
import os
import time
import xlrd
import xlwt
import openpyxl all_row = []
threads = [] class read_excel(object):
def __init__(self, num):
#threading.Thread.__init__(self)
#self.threadID = threadID
self.num = num
def read_excel(self):
filePath = os.path.join(os.getcwd(), 'D:\浙商银行\IP.xlsx') wb = openpyxl.load_workbook(filePath) # 打开excel文件
ws = wb['Sheet1']
res = ws.cell(row=1, column=1).value # 获取单元格的内容
# print(res)
# print(wb.sheetnames) # 显示excel文件的所有sheet名字
print(ws.min_row, ws.max_row, ws.max_column, ws.min_column) # 单元格的行数、列数; max_row:最大行、max_column:最大列
print('*******************************************************')
#all_row = []
for row in ws.rows: # 按行获取单元格(Cell对象) - 生成器
ev_row = []
for cell in row:
value = cell.value
value.strip()
print("value.strip():{0}".format(value.strip()))
ev_row.append(value.strip())
all_row.append(ev_row)
#print("ev_row:", ev_row)
#print('*******************************************************')
#print(all_row)
#print(len(all_row))
#print("all_row[0][0]:", all_row[0][0]) class acquisition_of_information(read_excel, threading.Thread): def __init__(self, num, threadID, name):
#super().__init__(self, num)
threading.Thread.__init__(self)
self.threadID = threadID
self.num = num
print("threadID:{0} name:{1}".format(threadID, name))
def run(self):
# 创建SSHClient实例对象
ssh = paramiko.SSHClient()
# 调用方法,标识没有远程机器的公钥,允许访问
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
len1 = len(all_row)
print("len1:", len1)
# 连接远程机器 地址端口用户名密码
for num in range(0, len1):
fo = open(all_row[num][0] + ".log", "wb+")
fo.truncate() # 清空文件内容
all_row[num][0].strip() # 去除excel表格内容头和尾的空格
print("all_row{0}[0]:{1}".format(num, all_row[num][0].strip()))
all_row[num][1].strip() # 去除excel表格内容头和尾的空格
print("all_row{0}[1]:{1}".format(num, all_row[num][1].strip()))
all_row[num][2].strip() # 去除excel表格内容头和尾的空格
print("all_row{0}[2]:{1}".format(num, all_row[num][2].strip()))
try:
ssh.connect(all_row[num][0].strip(), 22, all_row[num][1].strip(), all_row[num][2].strip())
except IOError:
print ("连接服务器失败,IP:{0}".format(all_row[num][0]))
else:
print ("连接服务器成功,IP:{0}".format(all_row[num][0]))
print("num:", num)
# 创建目录
stdin, stdout, stderr = ssh.exec_command("ip a | grep 192 | awk '{print $2}'")
fo.write(stdout.read())
str = "***************************" \
"***************************\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("uname -a | grep -i 'linux' | wc -l")
res = stdout.read()
print("res:", res)
if res == b'1\n':
str = "df -Ph\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("df -Ph")
fo.write(stdout.read())
str = "***************************" \
"***************************\n"
str = str.encode()
fo.write(str)
else:
str = "df -Pg\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("df -Pg")
fo.write(stdout.read())
str = "***************************" \
"***************************\n"
str = str.encode()
fo.write(str)
str = "cat .profile\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("cd /root && cat .profile")
res, err = stdout.read(), stderr.read()
fo.write(res)
str = "***************************" \
"***************************\n"
str = str.encode()
fo.write(str)
str = "arp -a\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("arp -a")
res, err = stdout.read(), stderr.read()
fo.write(res)
str = "***************************" \
"***************************\n"
str = str.encode()
fo.write(str) str = "netstat -rn\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("netstat -rn")
res, err = stdout.read(), stderr.read()
fo.write(res)
str = "***************************" \
"***************************\n"
str = str.encode()
fo.write(str) str = "cat /etc/rc.local\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("cat /etc/rc.local")
res, err = stdout.read(), stderr.read()
fo.write(res)
str = "***************************" \
"***************************\n"
str = str.encode()
fo.write(str) str = "cat /etc/hosts\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("cat /etc/hosts")
res, err = stdout.read(), stderr.read()
fo.write(res)
str = "***************************" \
"***************************\n"
str = str.encode()
fo.write(str) str = "netstat -an|grep LISTEN*\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("netstat -an|grep LISTEN*")
res, err = stdout.read(), stderr.read()
fo.write(res)
str = "***************************" \
"***************************\n"
str = str.encode()
fo.write(str) str = "ps -ef | grep java\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("ps -ef | grep java")
res, err = stdout.read(), stderr.read()
fo.write(res)
str = "***************************" \
"***************************\n"
str = str.encode()
fo.write(str) str = "ps -ef | grep zabbix\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("ps -ef | grep zabbix")
res, err = stdout.read(), stderr.read()
fo.write(res)
str = "***************************" \
"***************************\n"
str = str.encode()
fo.write(str) str = "ps -ef | grep ctm\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("ps -ef | grep ctm")
res, err = stdout.read(), stderr.read()
fo.write(res)
str = "***************************" \
"***************************\n"
str = str.encode()
fo.write(str)
str = "crontab -l\n"
str = str.encode()
fo.write(str)
stdin, stdout, stderr = ssh.exec_command("crontab -l")
res, err = stdout.read(), stderr.read()
fo.write(res)
str = "--------------------------" \
"***************************" \
"***************************" \
"---------------------------\n"
str = str.encode()
fo.write(str)
ssh.close()
fo.close()
num = num + 2 def __del___(self, num):
self.num = num class print_all_row(read_excel, threading.Thread):
def __init__(self, num, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.num = num
print("print_all_row name:", name) def run(self):
print("all_row[0][0]:", all_row[0][0])
print("all_row[0][1]:", all_row[0][1])
print("all_row[0][2]:", all_row[0][2]) def main():
start = time.time()
excel = read_excel(0)
excel.read_excel()
thread1 = acquisition_of_information(0, "Thread-2", "zhouhaiwu")
thread2 = acquisition_of_information(1, "Thread-3", "zhouhaiwu")
thread3 = print_all_row(0, "Thread-4", "zhouhaiwu")
# 开启新线程
#thread1.start()
thread1.start()
thread2.start()
thread3.start()
# 添加线程到线程列表
#threads.append(thread1)
threads.append(thread1)
threads.append(thread2)
threads.append(thread3)
# 等待所有线程完成
for t in threads:
t.join()
print ("Exiting Main Thread")
end = time.time()
print (str(end)) if __name__ == '__main__':
main()
跨平台信息获取小工具第三版本(增加了继承、多线程、异常处理模块、excel表格内容剔除空格)的更多相关文章
- 今日头条Marketing API小工具(.Net Core版本)
前言 由于工作原因,需要用到今日头条的Marketing API做一些广告投放的定制化开发.然后看现在网上也没多少关于头条Marketing API的文章,于是便就有了该篇文章. 头条Marketin ...
- 懒人小工具:自动生成Model,Insert,Select,Delete以及导出Excel的方法
在开发的过程中,我们为了节约时间,往往会将大量重复机械的代码封装,考虑代码的复用性,这样我们可以节约很多时间来做别的事情.最近跳槽到一节webform开发的公司,主要是开发自己公司用的ERP.开始因为 ...
- Linux下显示IP地理位置信息的小工具-nali
一.简介 nali,名字取自中文“哪里”的拼音.nali包含一组命令行程序,其主要功能就是把一些网络工具的输出的IP字符串,附加上地理位置信息(使用纯真数据库QQWry.Dat).例如74.125.1 ...
- Unity3D ——强大的跨平台3D游戏开发工具(三)
第四章 为地形添加水源.水流以及水下的模糊效果 制作好了地形的各种效果,接下来我们给场景添加一些水效果,使场景更加丰富. 第一步:添加水面 由于我在上一次的地形创作中就已经在山峰之间制作了一块洼地,它 ...
- 迅雷VIP帐号获取小工具
自己写的迅雷vip帐号获取工具,主要是熟悉一下正则表达式 下载地址: 迅雷VIP获取工具 另附vip防踢补丁,不能使用最新迅雷,我使用的是迅雷尊享版2.0.12.258,使用了一段时间,至少没被踢出来 ...
- 如何为你的美术妹子做Unity的小工具(三)
绘制脚本组件监视面板的内容 我们写了一个脚本Test using UnityEngine; using System.Collections; using System.Collections.Gen ...
- FuzzScanner 信息收集小工具
前言: 该工具集成了各种大牛的工具,比如子域名发现,目录扫描,nmap端口扫描,c段地址查询,端口指纹,以及waf查询 00X1: 安装不推荐git安装,首先直接githup脱下来:git clone ...
- phpexcel获取excel表格内容
excel表格式: 代码: $objPHPExcel=PHPExcel_IOFactory::load($excelFilePath);//$file_url即Excel文件的路径 $sheet=$o ...
- 小技巧之“将Text文件中的数据导入到Excel中,这里空格为分割符为例”
1.使用场景 将数据以文本导出后,想录入到Excel中,的简便方案, 起因:对于Excel的导出,Text导出明显会更方便些 2.将Text文件中的数据导入到Excel中,这里空格为分割符为例的步骤 ...
随机推荐
- 由于SID连接不匹配,监听器拒绝连接。
java.sql.SQLException: Listener refused the connection with the following error:ORA-12505, TNS:liste ...
- logstash 写入数据到elasticsearch 索引相差8小时解决办法
问题说明 Logstash用的UTC时间, logstash在按每天输出到elasticsearch时,因为时区使用utc,造成每天8:00才创建当天索引,而8:00以前数据则输出到昨天的索引 # 使 ...
- python学习之内置函数(一)
4.7 内置函数 4.7.1 内置函数(1) eval 执行字符串类型的代码,并返回最终结果. eval('2 + 2') # 4 n=81 eval("n + 4") # 85 ...
- heartbeat双主高可用
一.基础配置 1.hostnamectl set-hostname node1 (node2) 2.[root@node1 ~]# cat /etc/hosts 192.168.40.128 ...
- [BeiJingWc2008]Gate Of Babylon
<基尔伽美修>是人类历史上第一部英雄史诗,两河流域最杰出的文学作品之一.作品讲述了基尔伽美修一生的传奇故事.在动画Fate/staynight中,基尔伽美修与亚瑟王等传说中的英雄人物一起出 ...
- python基础语法之集合set
1 概念 集合是一个无需的,不重复的数组组合,它的主要作用如下: 去重,将一个列表装换成集合,会将其去重 关系测试,测试两组数据的交集,差集,并集等关系. 集合对象是一组无需排列的可哈希的值,集合成员 ...
- kali安装redis
下载 wget http://download.redis.io/releases/redis-4.0.11.tar.gz 解压 tar -zxvf redis-4.0.11.tar.gz 切换目录 ...
- 【神经网络与深度学习】Caffe部署中的几个train-test-solver-prototxt-deploy等说明
1:神经网络中,我们通过最小化神经网络来训练网络,所以在训练时最后一层是损失函数层(LOSS), 在测试时我们通过准确率来评价该网络的优劣,因此最后一层是准确率层(ACCURACY). 但是当我们真正 ...
- ICPC Asia Nanning 2017 F. The Chosen One (大数、规律、2的k次幂)
Welcome to the 2017 ACM-ICPC Asia Nanning Regional Contest.Here is a breaking news. Now you have a c ...
- Spring JdbcTemplate 和 NamedParameterJdbcTemplate 使用
1.简单介绍 DAO层 的一般使用常见的是MyBatis 和 Hibernate,但是Hibernate是重量级的,而且学习成本较高,Mybatis 需要编写大量配置文件及接口文件,对于简单的项目应用 ...