#!/usr/bin/env python
#-*-coding:utf-8 -*-
import time
import paramiko
import json
import os
import re
def file_up_sh(hostname,username,password,port):
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
# 这里的os.path.join 只是个人需要 可以直接sftp.put(local_file_path, remote_file_path)
sftp.put(os.path.join('./LOG_TXT/','show_dpi_linux.py'), os.path.join('/byzoro/tmp/', 'text.py'))
t.close()
class Log(object):
def __init__(self,dpi_ip,dpi_port):
self._DPI_IP = dpi_ip
self._DPI_PORT = dpi_port
self._User_Name = "root"
self._PassWord = "123456"
self._LogPath = "./LOG_TXT/test_log.txt"
def file_write(self,message):
with open(self._LogPath,'a') as f:
f.write(message+'\n')
f.close()
def log_generated(self,COMID):
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(hostname=self._DPI_IP,port=self._DPI_PORT,username=self._User_Name,password=self._PassWord)
while 1:
stdin,stdout,stderr = self.ssh.exec_command('cd /byzoro/tmp/;python text.py %s'%COMID)
message = stdout.readlines()
err_message = stderr.readlines()
#print message
num_start = len(message)
if num_start <10:
count = 15
while count >0:
stdin,stdout,stderr = self.ssh.exec_command('cd /byzoro/tmp/;python text.py %s'%COMID)
message = stdout.readlines()
err_message = stderr.readlines()
#print message
num = len(message)
#print num
if num < 4:
#print '没有show到结果'
time.sleep(10)
count -=1
elif 4 < num <10:
time.sleep(10)
continue
elif num >10:
p_ass = 'DPI加载策略成功'
print p_ass.decode('UTF-8').encode('GBK')
self.file_write(p_ass)
break
else:
print "DPI上已经有该策略"
Count = 15
while Count>0:
time.sleep(10)
stdin,stdout,stderr = self.ssh.exec_command('cd /byzoro/tmp/;python text.py %s'%COMID)
message = stdout.readlines()
err_message = stderr.readlines()
#print message
num_end = len(message)
if num_end >num_start:
p_ass = 'DPI加载策略成功'
print p_ass.decode('UTF-8').encode('GBK')
self.file_write(p_ass)
break
elif num_end == num_start:
Count -= 1
continue
else:
pass
break
def Dpi_Log_Creat(hostname,COMID):
#hostname ='10.84.9.52'
username = 'root'
password = '123456'
port = 22
#上传本地文件到shell下
file_up_sh(hostname,username,password,port)
cu = Log(hostname,port)
cu.log_generated(COMID)
if __name__ == '__main__':
Dpi_Log_Creat(hostname,COMID)