zoukankan      html  css  js  c++  java
  • 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:UserscchenDesktop')
    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 ')
    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)

  • 相关阅读:
    LeetCode题目(python)
    解决:centos配置ssh免密码登录后仍要输入密码
    解决 find: 路径必须在表达式之前:
    --解决Lock wait timeout exceeded; try restarting transaction
    Linux文件删除,但是df之后磁盘空间没有释放
    定位class时空格注意
    解决jenkins的Console Output中文乱码
    CPU飙升问题排查
    JVM笔记
    List集合的使用
  • 原文地址:https://www.cnblogs.com/AlwaysT-Mac/p/6010100.html
Copyright © 2011-2022 走看看