My first python script for work

I write it yesterday to watch the NE process(rcpfd,cfgd) automatically, then i will write a window to implement it:

#! /usr/bin/env python
# -*- coding: UTF-8 -*-

import paramiko
import ssh
import os
import re
import time
#RCPD异常类
class RCPD_Exception(Exception):
def __init__(self,value):
self.value=value
def __str__(self):
return self.value

#CFGD异常类
class CFGD_Exception(Exception):
def __init__(self,value):
self.value=value
def __str__(self):
return self.value

#check进程的函数,进程stuck会报异常,每ss(s>1)秒check一次
def check(hostname,ss):
hostname=hostname
port = 22
username = ‘root‘
password = ‘root‘
#os.chdir(r‘C:\Users\cchen\Desktop‘)
paramiko.util.log_to_file(‘paramiko.log‘)
s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname,port,username,password)
ssh=s.invoke_shell()
ss-=1
while(1):
time.sleep(ss)
ssh=s.invoke_shell()
ssh.send(‘ps -ef\n‘)
time.sleep(1)
x = ssh.recv(10000)
#print x
pattern1=re.compile(r‘(./rcpd|\[rcpd\])‘)
rcpd=re.findall(pattern1,x)
print rcpd,time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
pattern2=re.compile(r‘(./cfgd|\[cfgd\])‘)
cfgd=re.findall(pattern2,x)
print cfgd,time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
if not rcpd[0]==‘./rcpd‘:
try:
raise RCPD_Exception(‘RCPD stuck‘)
except RCPD_Exception,e:
print e

if not cfgd[0]==‘./cfgd‘:
try:
raise CFGD_Exception(‘CFGD stuck‘)
except CFGD_Exception,e:
print e

if __name__==‘__main__‘: check(‘200.200.180.18‘,8)

时间: 2024-10-10 15:45:06

My first python script for work的相关文章

my python script (1) ---------create model script file

# my  python script #  to create script file #!/usr/bin/env python # -*- coding:utf-8 -*- import os,subprocess child = subprocess.Popen('which python',stdout = subprocess.PIPE,shell=True) (pypath,error) = child.communicate() pypath = pypath.strip('\n

Notepad++插件Python Script和Emmet的安装使用教程(转载)

安装插件Python Script和Emmet: 最近在做一个项目,涉及到大量的HTML.CSS代码的编写,手动写代码效率实在是低下.于是想搜索一下,有没有Notepad++插件可以支持自动生成的,果不其然还真有.Emmet,这款神器其实就是 Zen Coding的升级版,它可以极大的提高代码编写的效率,并提供了一种非常简练的语法规则,立刻生成对应的 HTML 结构或者 CSS代码,同时还有多种实用的功能帮助进行前端开发. Emmet支持多种编辑器,如Sublime Text 2,TextMat

Notepad++插件Emmet和Python Script的安装

最近在做一个项目,涉及到大量的HTML.CSS代码的编写,手动写代码效率实在 是低下.于是想搜索一下,有没有Notepad++插件可以支持自动生成的,果不其然还真有.Emmet,这款神器其实就是 Zen Coding 的升级版,它可以极大的提高代码编写的效率,并提供了一种非常简练的语法规则,立刻生成对应的 HTML 结构或者 CSS 代码,同时还有多种实用的功能帮助进行前端开发. Emmet支持多种编辑器,如Sublime Text 2,TextMate 1.x,Eclipse/Aptana,E

[Python] Execute a Python Script

Python scripts can be executed by passing the script name to the python command or created as executable commands that can run stand-alone. You’ll learn how to create both in this lesson. script.py: print("Hello World") When we want to run the s

Python script to create Screen from all Items/Graphs of a host

#!/usr/bin/env python import urllib2 import json import argparse def authenticate(url, username, password): values = {'jsonrpc': '2.0', 'method': 'user.login', 'params': { 'user': username, 'password': password }, 'id': '0' } data = json.dumps(values

Remote rsyslog server records the logs of python script

Ubuntu 14.04.1 LTS rsyslog server:  10.0.7.77 python client: 10.0.2.122 [rsyslog server] 1.Install rsyslog with apt-get sudo apt-get install -y rsyslog 2.edit /etc/rsyslog.conf ,uncomment this line $ModLoad immark # provides --MARK-- message capabili

Autorun a python script after reboot using rc.local

1. /home/lvtest/test/test.py from datetime import datetime now=datetime.now() f=open('test.log','a') f.write('%s '%now) f.close() 2. edit /etc/rc.local with adding codes below: cd /home/lvtest/test su lvtest -c "python /home/lvtest/test/test.py"

pycharm默认的模板修改python script

#!/usr/bin/env python # encoding: utf-8 #set( $SITE = "https://www.cnblogs.com/c-x-a" ) """ @version: v1.0 @author: cxa @contact: [email protected] @site: ${SITE} @software: ${PRODUCT_NAME} @file: ${NAME}.py @time: ${DATE} ${TIME}

NE Upgrade python script. Need to write a Tkinter GUI for it

# -*- coding: utf-8 -*-#from ftplib import FTP __authour__='CC' import osimport telnetlibimport timeimport shutilimport socket#import pdbimport paramikofrom Tkinter import *import globimport re def get_version_path(n1,n2,n3): if(int(n2)==0 and int(n1