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)

 

转载于:https://www.cnblogs.com/AlwaysT-Mac/p/6010100.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值