My first Python script for work

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.