python实现hp刀片ilo地址配置
在 shell实现hp刀片ilo地址配置 篇中有提到通过python来实现HP管理口的配置,没事写了段python实现的代码。大意也是通过pexpect模块来实现相应的配置,不过这段代码初写的时候是基于paramiko模块获取信息,并找到可用IP的,后面又写了一段通过pexpect实现自动交互输入的。真正的实现的时候并不需要paramiko部分的,不过懒得的代码整合和美化了,凑合着全部堆上来吧。代码如下:
1#!/usr/bin/python
2#-*- coding: utf-8 -*-
3# code from www.361way.com <[email protected]>
4import re,paramiko
5import commands
6def ssh2(ip,username,passwd,cmd):
7 try:
8 ssh = paramiko.SSHClient()
9 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
10 ssh.connect(ip,22,username,passwd,timeout=5)
11 stdin,stdout,stderr = ssh.exec_command(cmd)
12 return stdout.readlines()
13 #print x.strip("\n")
14 #print '%s\tOK\n'%(ip)
15 ssh.close()
16 except :
17 print '%s\tError\n'%(ip)
18def stdoutsplit(stdout,keywords):
19 for line in stdout:
20 if line.find(keywords) != -1:
21 return re.split(r': ',line)[1].strip('\n')
22def iprange(ip,mask):
23 ipaddr = ip.split('.')
24 netmask = mask.split('.')
25 cidr = sum([bin(int(x)).count('1') for x in netmask])
26 net_start = [str(int(ipaddr[x]) & int(netmask[x]))
27 for x in range(0,4)]
28 end = int(netmask[3]) + 2**(32-cidr) - 1
29 firstip = '.'.join(net_start)
30 endip = '.'.join(ipaddr[0:3]) + '.' + str(end)
31 return firstip + '\t' + endip
32stdout = ssh2("10.105.10.45","admin","password","SHOW OA NETWORK")
33oaip = stdoutsplit(stdout,'IPv4 Address:')
34mask = stdoutsplit(stdout,'Netmask:')
35gate = stdoutsplit(stdout,'Gateway Address:')
36#print oaip,mask,gate
37cmd ="fping -g " + iprange(oaip,mask)
38status, output = commands.getstatusoutput(cmd)
39print 'Available IP follows:'
40print '*'*40
41for line in output.split('\n'):
42 if line.find('unreachable') != -1:
43 print line
44print '*'*40
45iloip = raw_input("please input the ilo ip: ")
46bid = raw_input("please input the blade bay id: ")
47setip="set ebipa server " + iloip + '\t' + mask + '\t' + bid
48setgate="set ebipa server gateway " + gate + '\t' + bid
49print setip
50print setgate
51#---------------------------------------------------------------------
52from pexpect.pxssh import pxssh
53s = pxssh()
54s.login('10.105.10.45','admin','password',original_prompt='>',auto_prompt_reset=False)
55s.sendline("set ebipa server 10.105.10.58 255.255.255.0 13")
56s.expect('bays\?')
57s.sendline('YES')
58#print s.before
59s.expect('>')
60print s.before
61s.logout()
算个半成品吧,需要的功能代码里都有了,想实现shell里的功能,只是简单的修改下就成了。实际代码要不了这么多的,后面重新IP规划,给所有代片全配置上连续的IP,HP管理口本身也有autofill 功能,配置起来也比较快。使用脚本无非是练习下,同时也对管理口增强更多的内部了解。
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/python-hp-ilo/5570.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.