跨平台信息获取小工具

# coding=utf-8
import paramiko
import os
import time
import xlrd
import xlwt
import openpyxl

all_row = []


class read_excel(object):
def __init__(self, num):
self.num = num
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
ev_row.append(value)
all_row.append(ev_row)
print("ev_row:", ev_row)
print('*******************************************************')
print(all_row)
print(len(all_row))


class acquisition_of_information(read_excel):

def __init__(self, num, name):
# self.num = num
self.num = num
print("name:", name)
def ssh2(self):
# 创建SSHClient实例对象
ssh = paramiko.SSHClient()
# 调用方法,标识没有远程机器的公钥,允许访问
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
len1 = len(all_row)
# 连接远程机器 地址端口用户名密码
for num in range(0, len1):
fo = open(all_row[num][0] + ".log", "wb+")
fo.truncate() # 清空文件内容
try:
ssh.connect(all_row[num][0], 22, all_row[num][1], all_row[num][2])
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 + 1

def __del___(self, num):
self.num = num


class print_all_row(read_excel):
def print_all_row(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])


if __name__ == '__main__':
info = acquisition_of_information(0, "zhouhaiwu")
info.read_excel()
info.ssh2()
par = print_all_row(0)
par.print_all_row()
posted @ 2019-06-29 18:42  zhouhaiwu  阅读(232)  评论(0编辑  收藏  举报