12
# -*-coding:utf-8-*-
import xlwt # 写入文件
#import xlrd # 打开excel文件
fopen = open('E:\\python\\monit\\12g.txt', 'r')
lines = fopen.readlines()
# 创建workbook和sheet对象
workbook = xlwt.Workbook(encoding = 'utf-8') # Workbook的开头W 大写
sheet1 = workbook.add_sheet('sheet1', cell_overwrite_ok=True)
# 向sheet页中写入数据
sheet1.write(0, 0, 'url')
sheet1.write(0, 1, 'time')
dict ={}
dict1 ={}
list=[]
list1=[]
row = 1
for line in lines:
aa = eval(line)
x = aa["request"].split()[1]
y = aa["request_time"]
z = aa["http_version"]
list1.append(x)
if y > 1:
list.append(x)
sheet1.write(row, 0, x)
sheet1.write(row, 1, y)
sheet1.write(row, 2, z)
row += 1
workbook.save('12g.xls') # 写入excel
for key1 in list1:
dict1[key1] = dict1.get(key1,0) +1
fw1 = open("12g1.txt",'w+')
for z,k in dict1.items():
fw1.write(z+' '+str(k)) #把字典转化为str
fw1.write('\n')
fw1.close()
for key in list:
dict[key] =dict.get(key,0) + 1
fw = open("12ga.txt",'w+')
for a,b in dict.items():
fw.write(a+' '+str(b)) #把字典转化为str
fw.write('\n')
fw.close()
利用paramiko模块实现服务器之间文件传输。
# -*- coding:UTF-8 -*-
import paramiko
hostname = '*********'
port = 22
username = 'appdeploy'
password = "********"
cmd =['cd /app/zhaokai','grep -a "globalgreen" 12g.txt > 123.txt']
# 发送paramkio日志到syslogin.log文件
paramiko.util.log_to_file('syslogin.log')
# 创建一个ssh客户端client对象
ssh = paramiko.SSHClient()
# 获取客户端host_keys,默认~/.ssh/known_hosts, 非默认路径需指定
ssh.load_system_host_keys()
# 创建ssh连接
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname, port =22, username=username, password = password)
# 调用远程执行命令方法exec_command()
tran = ssh.get_transport()
sftp = paramiko.SFTPClient.from_transport(tran) #使用t的设置方式连接远程主机
sftp.get('/app/zhaokai/12g.txt','hello.txt') #下载文件
# 关闭ssh连接
ssh.close()
shell脚本实现搜索关键字
#!/bin/sh
#!/usr/bin/expect
set passwd "PassW0@@rd#"
WORK_DIR=$(cd `dirname $0`; pwd)
cd $WORK_DIR
zgrep -a 'globalgreen' 12g.txt > 123.txt