I write it yesterday to watch the NE process (RCPFD,CFGD) automatically and then I'll 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进程的函数, Process stuck will report an exception, check once per SS (s>1) seconds
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)
My first Python script for work