文章导航绿软键入站解压软件键入xf881兴发手机版资源苹果资源专题

您当前所在位置:首页操作系统LINUX → crond运转Python投资公司宣传片脚本实现多台linux漆器的监控

crond运转Python投资公司宣传片脚本实现多台linux漆器的监控

时间:2015/6/28来源:xf881兴发手机版入口作者:聚生网管完美破解版联盟我要搬家评论(1)

Pexpect简介

  Pexpect 是一度用于启动汇编延时子程序并对其进行自动控制的 Python 模块。它可以用于和像 ssh。passwd,telnet 等命令行程序进行自动互相。本文介绍 Pexpect 的主要用法和在实际应用中的注意点。 Python 语言的兴趣爱好者,系统管理人员,配置及测试人员都能使用 Pexpect 在自己的工作中实现与命令行互相的明朗化。

  具体可以参考http://www.noah.org/wiki/Pexpect#Download_and_Installation

  键入地址 http://pexpect.sourceforge.net/pexpect-2.3.tar.gz

  今天介绍的内容就是利用pexpect来实现的远程多漆器的管理。

Python投资公司宣传片脚本

  以下是python的底码。程序的大概过程是使用pexpect的ssh命令循环登陆到远程漆器上,将服务和进程的运转情况纪录到临时文件中,过后再从文件中获取服务和进程的运转情况,如果服务或进程异常停止。程序则将其重新启动,所有漆器的运转状况都将纪录在日志文件中。

  代码

  # coding=utf-8

  #!/usr/bin/env python

  import pexpect

  import getpass, os

  import string

  import time

  from datetime import datetime, date

  ssh_newkey = 'Are you sure you want to continue connecting (yes/no)?'

  hosts = []

  hosts.append('xx.xx.xx.xx')

  user = 'user'

  password = 'pwd'

  logFile = '/tmp/pexpect-2.3/monitor.log'

  def restartService(host, user, password, service):

  child = pexpect.spawn('ssh %s@%s' %(user, host))

  i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])

  if i == 0: # Timeout

  return None

  if i == 1:

  child.sendline('yes')

  child.sendline(password)

  child.sendline('sudo service %s restart' %service)

  j = child.expect(['Password: ', '$', '#'])

  if j == 0:

  child.sendline(password)

  if j == 1:

  child.sendline(password)

  time.sleep(30)

  child.sendline('exit')

  fout = file(logFile, 'a')

  child.logfile_read = fout

  return child

  # restart one process

  def restartProcess(host, user, password):

  child = pexpect.spawn('ssh %s@%s' %(user, host))

  i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])

  if i == 0: # Timeout

  return None

  if i == 1:

  child.sendline('yes')

  child.sendline(password)

  child.sendline('sudo su')

  j = child.expect(['Password: ', '$', '#'])

  if j == 0:

  child.sendline(password)

  if j == 1:

  child.sendline(password)

  child.sendline('cd /processlocation/bin')

  child.sendline('./processName.sh &')

  time.sleep(20)

  child.sendline('exit')

  child.sendline('exit')

  fout = file(logFile, 'a')

  child.logfile_read = fout

  return child

  # log process' status to file tmpProc.txt

  def logProcessInfo(host, user, password, process):

  child = pexpect.spawn('ssh %s@%s' %(user, host))

  i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])

  if i == 0: # Timeout

  return None

  if i == 1:

  child.sendline('yes')

  child.sendline(password)

  child.sendline('ps -ef|grep %s' %process)

  child.sendline('exit')

  fout = file('tmpProc.txt', 'w')

  child.logfile_read = fout

  return child

#p#副题#e#

  # get process' id from file tmpProc.txt

  def getProcessId():

  val = ''

  f = file('tmpProc.txt', 'r')

  f.seek(0)

  while True:

  line = f.readline()

  if line.find('processName') > 0:

  index = line.find(' ')

  subline = line[index+1:]

  while True:

  if subline[0] == ' ':

  index = subline.find(' ')

  subline = subline[index+1:]

  else:

  break

  index2 = subline.find(' ')

  number = subline[:index2]

  val = number

  if len(line) == 0:

  break

  f.close()

  return val

  # log service's status to file tmpServ.txt

  def logServiceInfo(host, user, password, serviceName):

  child = pexpect.spawn('ssh %s@%s' %(user, host))

  i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])

  if i == 0: # Timeout

  return None

  if i == 1:

  child.sendline('yes')

  child.sendline(password)

  child.sendline('service %s status' %serviceName)

  child.sendline('exit')

  fout = file('tmpServ.txt', 'w')

  child.logfile_read = fout

  return child

  # get servicee's status of start or stop from file tmpServ.txt

  def hasServiceStart():

  val = True

  f = file('tmpServ.txt', 'r')

  f.seek(0)

  while True:

  line = f.readline()

  if line.find('stop') > 0:

  val = False

  if len(line) == 0:

  break

  f.close()

  return val

  def logOperation(content):

  f = file(logFile, 'a')

  f.write(content)

  f.close()

  current = datetime.now()

  logOperation('\n\n***')

  logOperation(current.strftime("%a %b %d %H:%M:%S %Y") + '\n')

  for host in hosts:

  processChild = logProcessInfo(host, user, password, 'processName')

  processChild.expect(pexpect.EOF)

  processId = getProcessId()

  if processId == '':

  logOperation('processName on ' + host + ' has been stopped\n')

  processChild = restartProcess(host, user, password)

  processChild.expect(pexpect.EOF)

  else:

  logOperation('processName on ' + host + ' is ok\n')

  serviceChild = logServiceInfo(host, user, password, 'serviceName')

  serviceChild.expect(pexpect.EOF)

  if hasServiceStart():

  logOperation('serviceName on ' + host + ' is ok\n')

  else:

  logOperation('serviceName on ' + host + ' has been stopped\n')

  serviceChild = restartService(host, user, password, 'serviceName')

  serviceChild.expect(pexpect.EOF)

  print 'mission accomplished'

  三。通过配置crond使投资公司宣传片脚本能够定时执行

  1)登陆到投资公司宣传片脚本所在的linux漆器上

  2)运转命令crontab -e

  说明:系统默认的编辑器是VIM,如果不是请加上以下shell:

  $EDITOR=vi

  $export EDITOR

  3)日益增长0 * * * * python /tmp/pexpect-2.3/autoMonitor.py

  4)重起crond

  cd /etc/init.d

  ./crond restart

  这样每个小时0分的时候105上会自动运转autoMonitor.py投资公司宣传片脚本

基本词标签:crond,Python投资公司宣传片脚本,lin

相关阅读

文章评论
发表评论

热门文章 使用Clonezilla迁移到编造Linux环境Linux漆器:设计高性能网站架构-LLMP安装配置MRTG监控Linux网络Linux下的Root权限控制

相关软件

人气排名榜 Linux下获取CPUID,硬盘cad2007序列号与MAC地址linux tc实现ip流量限制dmidecode命令查看内存型号linux下解压rar文件安装红帽子曲奇RedHat Linux9.0操作系统教程Ubuntu linux 关机,撤回 命令lcx.exe,nc.exe,sc.exe入侵中的使用方法查看linux漆器硬盘IO读写负载

Baidu