Python实现自动装机功能案例分析
作者:weixin_42952974 时间:2022-05-16 12:35:48
前言
提示:在管理服务器的过程中,发现有很多服务器在启动的过程中默认以PXE方式启动,这就导致我们无法将PXE装机程序放开到所有的交换机端口中,本文是以Python对dell服务器进行了一些控制,更多厂商机器的管理和控制,仍在调研中。
提示:以下是本篇文章正文内容,下面案例可供参考
一、利用snmp协议获取到目标机器的网卡mac地址
代码如下
def get_mac(ipmi, netcard):
#ipmi即服务器idrac_ip,netcard即网卡序列号(一般是4个,从1开始)
# 将控制卡IP传给snmp命令,获取mac地址
popen = subprocess.Popen(f'snmpwalk -v 2c -c public {ipmi} 1.3.6.1.4.1.674.10892.5.4.1100.90.1.6.1.{netcard}',
stdout=subprocess.PIPE, shell=True)
popen.wait()
res = popen.stdout.read().decode().split()[-1].split('"')[0]
res1 = res.replace(":", "")
res2 = res.lower()
# 获取到的mac地址,去空格,写入到文件中
with open("/root/allow_mac", mode="w", encoding="utf-8") as f:
f.write(res + "\n")
print(res)
#获取到mac地址的目的有两个,可以将mac地址传给后端交换机,交换机找到对应的接口,自动进行网络配置的下发,
#另外一个是针对mac地址做防火墙控制
防火墙的初始化:
def init_iptables():
# 调用iptables初始化防火墙策略
print("防火墙开始初始化")
subprocess.call('/sbin/iptables -F ', shell=True)
subprocess.call('/sbin/iptables -P OUTPUT ACCEPT ', shell=True)
subprocess.call('/sbin/iptables -A INPUT -m state --state INVALID -j DROP ', shell=True)
subprocess.call('/sbin/iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT ', shell=True)
subprocess.call('/sbin/iptables -P INPUT DROP ', shell=True)
# stdout = subprocess.call('/sbin/iptables -L ', shell=True)
# print(stdout)
# subprocess.call('systemctl stop dhcpd ', shell=True)
print("防火墙初始化完毕")
#主要就是封装了一系列防火墙的配置,在装机完成之后,可以进行再控制,防止其他机器通过pxe-server进行装机操作
来源:https://blog.csdn.net/weixin_42952974/article/details/109195680
标签:Python,自动装机
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp 动态生成rss(不成生xml文件)代码
2011-04-04 11:17:00
请问能否在ASP中调用DLL
2009-06-07 18:24:00
关于Math.PI、前自增和后自增
2009-05-25 12:38:00
ASP使用组件在线发E-mail的2个函数
2008-06-12 07:13:00
Pandas 连接合并函数merge()详解
2021-10-28 05:13:44
30个运用纸类元素网页设计的创意实例欣赏
2009-04-20 12:49:00
![](https://img.aspxhome.com/file/UploadPic/20094/20/paper-web-design-01-500x272-56s.jpg)
python3 selenium自动化测试 强大的CSS定位方法
2021-09-13 01:41:13
为什么str(float)在Python 3中比Python 2返回更多的数字
2022-11-09 22:55:56
python读取文本中数据并转化为DataFrame的实例
2021-10-24 13:08:28
![](https://img.aspxhome.com/file/2023/5/77785_0s.jpg)
python3发送邮件需要经过代理服务器的示例代码
2023-07-27 01:00:44
Linux下python3.7.0安装教程
2021-07-30 05:25:58
![](https://img.aspxhome.com/file/2023/7/90437_0s.jpg)
PHP使用观察者模式处理异常信息的方法详解
2023-10-19 15:53:30
Python中使用多进程来实现并行处理的方法小结
2023-10-23 14:21:38
Sql语句优化注意
2009-10-31 13:15:00
也谈 CSS Sprites
2009-10-06 15:14:00
![](https://img.aspxhome.com/file/UploadPic/200910/6/1254278481253-97s.png)
Python 添加命令行参数步骤
2022-10-24 05:15:40
![](https://img.aspxhome.com/file/2023/3/80723_0s.png)
Python如何避免文件同名产生覆盖
2021-02-05 21:16:34
![](https://img.aspxhome.com/file/2023/4/85364_0s.png)
php线性表顺序存储实现代码(增删查改)
2023-11-19 06:51:53
php初始化对象和析构函数的简单实例
2023-11-09 18:11:14
python实现库存商品管理系统
2023-06-01 06:37:29
![](https://img.aspxhome.com/file/2023/7/80597_0s.jpg)