Python實現(xiàn)自動裝機功能案例分析
前言
提示:在管理服務(wù)器的過程中,發(fā)現(xiàn)有很多服務(wù)器在啟動的過程中默認以PXE方式啟動,這就導(dǎo)致我們無法將PXE裝機程序放開到所有的交換機端口中,本文是以Python對dell服務(wù)器進行了一些控制,更多廠商機器的管理和控制,仍在調(diào)研中。
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、利用snmp協(xié)議獲取到目標機器的網(wǎng)卡mac地址
代碼如下
def get_mac(ipmi, netcard):
#ipmi即服務(wù)器idrac_ip,netcard即網(wǎng)卡序列號(一般是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地址傳給后端交換機,交換機找到對應(yīng)的接口,自動進行網(wǎng)絡(luò)配置的下發(fā),
#另外一個是針對mac地址做防火墻控制
防火墻的初始化:
def init_iptables():
# 調(diào)用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進行裝機操作
到此這篇關(guān)于Python實現(xiàn)自動裝機功能案例分析的文章就介紹到這了,更多相關(guān)Python自動裝機內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中號稱神仙的六個內(nèi)置函數(shù)詳解
這篇文章主要介紹了Python中號稱神仙的六個內(nèi)置函數(shù),今天分享的這6個內(nèi)置函數(shù),在使用?Python?進行數(shù)據(jù)分析或者其他復(fù)雜的自動化任務(wù)時非常方便,需要的朋友可以參考下2022-05-05
python json load json 數(shù)據(jù)后出現(xiàn)亂序的解決方案
今天小編就為大家分享一篇python json load json 數(shù)據(jù)后出現(xiàn)亂序的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02

