#!/bin/env python
# -*- coding:utf-8 -*-
# program: ssh remote execute command
# genaral data story local
# author: chenhao
# date: 2015-09-14
import sys
import paramiko
import xlwt
import time
from datetime import datetime
import logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
filename='check.log',
filemode='w',
datefmt='%Y-%m-%d %X')
reload(sys)
ISOTIMEFORMAT='%Y-%m-%d'
sys.setdefaultencoding('utf-8')
def header(work_sheet):
work_sheet.write(0, 0, '检查项目', style0)
work_sheet.write(0, 1, '命令', style0)
work_sheet.write(0, 2, '基准', style0)
work_sheet.write(0, 3, '检查结果', style0)
work_sheet.write(0, 4, '检查主机', style0)
logging.info('header add ok!')
return work_sheet
def run_host_cmd(host, port, username, password, work_sheet):
ssh = paramiko.SSHClient()
command_file = open('./hosts/' + host + '.cmd', 'r')
for line in command_file.readlines():
rows = len(work_sheet.rows)
line = line.strip('\n')
items = line.split('#')
check_type = items[0].decode('gbk').encode('utf-8')
check_cmd = items[1].decode('gbk').encode('utf-8')
check_base_line = items[2].decode('gbk').encode('utf-8')
opration = check_base_line[0:1]
base_value = check_base_line[1:]
work_sheet.write(rows, 0, check_type)
work_sheet.write(rows, 1, check_cmd)
work_sheet.write(rows, 2, check_base_line)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, port, username, password, key_filename=None, timeout=2)
except Exception, e:
logging.error('can not connect host: ' + host)
logging.error('command can not exec: ' + check_cmd)
logging.error(e)
continue
stdin, stdout, stderr = ssh.exec_command(check_cmd)
return_info = stdout.read().strip()
style_red = xlwt.easyxf('pattern: pattern solid, fore_colour red;' +
'font: name Times New Roman, color-index black, bold on;' +
'borders: left thick, right thick, top thick, bottom thick;' +
'align: horiz center')
error_flag = False
if opration == '>':
if str(base_value) > str(return_info):
error_flag = True
elif opration == '=':
if str(return_info) != str(base_value):
error_flag = True
elif opration == '<':
if str(base_value) < str(return_info):
error_flag = True
if error_flag:
logging.warning(host + ' checked diff')
logging.warning('command is: ' + check_cmd)
work_sheet.write(rows, 3, return_info.decode('gbk').encode('utf-8'), style_red)
else:
work_sheet.write(rows, 3, return_info.decode('gbk').encode('utf-8'))
work_sheet.write(rows, 4, 'server IP: ' + host)
check_info = check_type + '\t' + check_cmd + '\t' + check_base_line.replace('\n','')+ '\t' + 'server IP: ' + host
check_info = check_info.strip('\n')
#check_info += '\t' + 'server IP: ' + host
logging.info(check_info)
return_info = return_info.strip('\n')
logging.info(return_info)
command_file.close()
ssh.close()
return work_sheet
if __name__=='__main__':
# try:
# main()
# except Exception,e:
# print e
style0 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow;' +
'font: name Times New Roman, color-index black, bold on;' +
'borders: left thick, right thick, top thick, bottom thick;' +
'align: horiz center',
num_format_str='0,000.00')
work_book = xlwt.Workbook(encoding='utf-8')
work_sheet = work_book.add_sheet('A Test Sheet')
work_sheet = header(work_sheet)
hosts_file = open('./hosts/host.info', 'r')
for line in hosts_file.readlines():
if line[0:1] == '#': continue
line = line.strip('\n')
items = line.split()
port = 22
host = items[0]
username = items[1]
password = items[2]
work_sheet = run_host_cmd(host, port, username, password, work_sheet)
logging.info(host + ' check finish !\n')
file_pre = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
work_book.save(file_pre + '_xujian.xls')
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
daily_check_py.rar (85个子文件)
daily_check_py
daily_check.py 4KB
2018-02-24_xujian.xls 6KB
daily_check
daily_check.py 4KB
.git
logs
HEAD 163B
refs
heads
master 163B
remotes
origin
master 148B
info
exclude 240B
index 2KB
COMMIT_EDITMSG 12B
objects
pack
c5
095c228b73831c209511a3155fb53b2f5204f1 730B
62
0c06b40c489cf75b8ef764f9d035c628d722d2 1KB
info
a5
250d32bdd1163e38abbb48b1bbd72e9743e817 327B
263923ab3123070ffbbfbc311c3372ecbcff69 416B
8a
a4b731aa220ea1aed10332c11a9cf8f421bbe1 2KB
ce
01120069aff1342d9890120e0202d30dc466bc 335B
dd
4064ad8dd99736d18b9b7cd3cfa5ab4c5efa3d 305B
d4
5ac526ab205847e82952396547dfbdf29c9378 165B
c9
197df1f2d320ce322c436ade3422935d9e46cb 397B
6b
11eafc80e31b777f876700354e2968b6308cbd 390B
ba
6da3da88dab3f7c096ee6cc373021ccfa1e165 349B
4f
faff03d693c6e801ea35034473668fc4c966b8 361B
b9
1f1d7bd2064b3e5c9c03ab8d7290749f1a15b0 129B
e2
c1a7904bb74b20bbcf601ed836a9f70b1dbdb2 320B
30
937162275e322ee71c076ab677828ce430eaca 332B
e4
25385284a510e5776944d585ff384d5fc8e430 336B
17
5328ace6c67c29d36254c982e068488a15a62e 103B
HEAD 23B
config 316B
refs
heads
master 41B
tags
remotes
origin
master 41B
hooks
pre-applypatch.sample 424B
pre-push.sample 1KB
commit-msg.sample 896B
pre-commit.sample 2KB
applypatch-msg.sample 478B
prepare-commit-msg.sample 1KB
update.sample 4KB
fsmonitor-watchman.sample 3KB
post-update.sample 189B
pre-rebase.sample 5KB
pre-receive.sample 544B
description 73B
2018-02-24_xujian.xls 6KB
check.log 5KB
hosts
10.29.37.128.cmd 467B
10.48.114.165.cmd 875B
10.48.114.164.cmd 1KB
10.48.114.166.cmd 875B
10.29.37.125.cmd 467B
10.29.37.123.cmd 467B
10.46.205.24.cmd 1KB
10.29.37.122.cmd 467B
10.48.94.149.cmd 1KB
10.29.37.127.cmd 467B
10.29.37.126.cmd 467B
10.46.205.26.cmd 850B
10.46.205.25.cmd 859B
10.46.205.23.cmd 762B
10.29.37.121.cmd 467B
host.info 406B
10.55.12.11.cmd 467B
10.48.94.148.cmd 1KB
10.29.37.124.cmd 467B
10.46.205.59.cmd 1KB
check.log 5KB
hosts
10.29.37.128.cmd 467B
10.48.114.165.cmd 875B
10.48.114.164.cmd 1KB
10.48.114.166.cmd 875B
10.29.37.125.cmd 467B
10.29.37.123.cmd 467B
10.46.205.24.cmd 1KB
10.29.37.122.cmd 467B
10.48.94.149.cmd 1KB
10.29.37.127.cmd 467B
10.29.37.126.cmd 467B
10.46.205.26.cmd 850B
10.46.205.25.cmd 859B
10.46.205.23.cmd 762B
10.29.37.121.cmd 467B
host.info 406B
10.55.12.11.cmd 467B
10.48.94.148.cmd 1KB
10.29.37.124.cmd 467B
10.46.205.59.cmd 1KB
共 85 条
- 1
资源评论
sinat_26192211
- 粉丝: 2
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功