Last active
December 15, 2015 06:39
-
-
Save tawateer/d2ae83cd468ef4a83aae to your computer and use it in GitHub Desktop.
检查 idrac 是否存活和检查机器电源状态
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env | |
import os | |
import sys | |
import re | |
import logging | |
import time | |
import argparse | |
import pexpect | |
import subprocess | |
from multiprocessing.dummy import Pool as ThreadPool | |
DOMAIN = "nosa.me" | |
MAX_THREAD_NUM = 10 | |
ILO_PASSWDS = ['calvin'] | |
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout, | |
format='%(message)s') | |
def shell(cmd, exit=True): | |
process = subprocess.Popen(args=cmd, stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, shell=True) | |
so, se = process.communicate() | |
rc = process.poll() | |
if rc == 0: | |
# message = "cmd:%s" % cmd | |
# logging.info(message) | |
return so.strip() | |
else: | |
message = "cmd:%s, error:%s" % (cmd, se) | |
logging.error(message) | |
if exit == True: | |
sys.exit(1) | |
else: | |
return False | |
def get_ip(idc, sn): | |
cmd = "nslookup idrac-{sn}.ilo.{domain}. ddns0.{idc}.{domain}".format( | |
sn=sn, idc=idc, domain=DOMAIN) | |
return shell(cmd).splitlines()[-1].split(":")[-1].strip() | |
def get_passwd(ip, sn): | |
for passwd in ILO_PASSWDS: | |
cmd = '''ssh -oStrictHostKeyChecking=no root@{ip} '''\ | |
'''"racadm getsvctag" '''.format(ip=ip) | |
# logging.info(cmd) | |
ssh = pexpect.spawn(cmd) | |
try: | |
i = ssh.expect( | |
["password:"], timeout=180) | |
if i == 0: | |
ssh.sendline(passwd) | |
ret = ssh.read() | |
# logging.info(ret) | |
if sn in ret: | |
return passwd | |
except pexpect.EOF: | |
ssh.close() | |
pass | |
except pexpect.TIMEOUT: | |
ssh.close() | |
pass | |
raise Exception("sn:{sn} cann't get ilo passwd".format(sn=sn)) | |
class Gen(object): | |
def __init__(self, idc, sn): | |
self.idc = idc | |
self.sn = sn | |
self.ip = get_ip(self.idc, self.sn) | |
self.passwd = get_passwd(self.ip, self.sn) | |
def ssh_cmd(self, cmd): | |
new_cmd = '''ssh -o StrictHostKeyChecking=no '''\ | |
'''-o ConnectTimeout=600 root@{ip} "{cmd}" '''.format( | |
ip=self.ip, cmd=cmd) | |
# logging.info(new_cmd) | |
ssh = pexpect.spawn(new_cmd, timeout=600) | |
ssh.expect([pexpect.TIMEOUT, 'password: ']) | |
ssh.sendline(self.passwd) | |
time.sleep(1) | |
return ssh.read() | |
def get_sn(self): | |
cmd = r"racadm getsvctag" | |
r = self.ssh_cmd(cmd) | |
second_line = r.splitlines()[1] | |
return second_line.strip() | |
def main(): | |
parser = argparse.ArgumentParser(description='check idrac is alive') | |
parser.add_argument( | |
'-i', '--idc', required=True, type=str, help='idc') | |
parser.add_argument( | |
'-f', '--file', required=True, type=str, help='sn file path') | |
args = parser.parse_args() | |
sns = [] | |
with open(args.file) as f: | |
for line in f.readlines(): | |
if line.strip() != "": | |
sns.append(line.strip()) | |
logging.info("sns:%s" % sns) | |
def single(sn): | |
try: | |
ilo_oj = Gen(args.idc, sn) | |
_sn = ilo_oj.get_sn() | |
if sn == _sn: | |
logging.info(sn) | |
else: | |
logging.warning("%s, %s", sn, _sn) | |
except Exception, e: | |
logging.warning("%s,%s", sn, e) | |
pool = ThreadPool(MAX_THREAD_NUM) | |
result_data = pool.map(single, sns) | |
pool.close() | |
pool.join() | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env | |
import os | |
import sys | |
import re | |
import logging | |
import time | |
import argparse | |
import pexpect | |
import subprocess | |
from multiprocessing.dummy import Pool as ThreadPool | |
DOMAIN = "nosa.me" | |
MAX_THREAD_NUM = 10 | |
ILO_PASSWDS = ['calvin'] | |
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout, | |
format='%(message)s') | |
def shell(cmd, exit=True): | |
process = subprocess.Popen(args=cmd, stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, shell=True) | |
so, se = process.communicate() | |
rc = process.poll() | |
if rc == 0: | |
# message = "cmd:%s" % cmd | |
# logging.info(message) | |
return so.strip() | |
else: | |
message = "cmd:%s, error:%s" % (cmd, se) | |
logging.error(message) | |
if exit == True: | |
sys.exit(1) | |
else: | |
return False | |
def get_ip(idc, sn): | |
cmd = "nslookup idrac-{sn}.ilo.{domain}. ddns0.{idc}.{domain}".format( | |
sn=sn, idc=idc, domain=DOMAIN) | |
return shell(cmd).splitlines()[-1].split(":")[-1].strip() | |
def get_passwd(ip, sn): | |
for passwd in ILO_PASSWDS: | |
cmd = '''ssh -oStrictHostKeyChecking=no root@{ip} '''\ | |
'''"racadm getsvctag" '''.format(ip=ip) | |
# logging.info(cmd) | |
ssh = pexpect.spawn(cmd) | |
try: | |
i = ssh.expect( | |
["password:"], timeout=180) | |
if i == 0: | |
ssh.sendline(passwd) | |
ret = ssh.read() | |
# logging.info(ret) | |
if sn in ret: | |
return passwd | |
except pexpect.EOF: | |
ssh.close() | |
pass | |
except pexpect.TIMEOUT: | |
ssh.close() | |
pass | |
raise Exception("sn:{sn} cann't get ilo passwd".format(sn=sn)) | |
class Gen(object): | |
def __init__(self, idc, sn): | |
self.idc = idc | |
self.sn = sn | |
self.ip = get_ip(self.idc, self.sn) | |
self.passwd = get_passwd(self.ip, self.sn) | |
def ssh_cmd(self, cmd): | |
new_cmd = '''ssh -o StrictHostKeyChecking=no '''\ | |
'''-o ConnectTimeout=600 root@{ip} "{cmd}" '''.format( | |
ip=self.ip, cmd=cmd) | |
# logging.info(new_cmd) | |
ssh = pexpect.spawn(new_cmd, timeout=600) | |
ssh.expect([pexpect.TIMEOUT, 'password: ']) | |
ssh.sendline(self.passwd) | |
time.sleep(1) | |
return ssh.read() | |
def get_powerstatus(self): | |
cmd = "racadm serveraction powerstatus" | |
r = self.ssh_cmd(cmd) | |
return r.strip() | |
def main(): | |
parser = argparse.ArgumentParser(description='check idrac is alive') | |
parser.add_argument( | |
'-i', '--idc', required=True, type=str, help='idc') | |
parser.add_argument( | |
'-f', '--file', required=True, type=str, help='sn file path') | |
args = parser.parse_args() | |
sns = [] | |
with open(args.file) as f: | |
for line in f.readlines(): | |
if line.strip() != "": | |
sns.append(line.strip()) | |
logging.info("sns:%s" % sns) | |
def single(sn): | |
try: | |
ilo_oj = Gen(args.idc, sn) | |
powerstatus = ilo_oj.get_powerstatus() | |
logging.info("%s:%s", sn, powerstatus) | |
except Exception, e: | |
logging.warning("%s,%s", sn, e) | |
pool = ThreadPool(MAX_THREAD_NUM) | |
result_data = pool.map(single, sns) | |
pool.close() | |
pool.join() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment