跳到主要内容

5、集成工程线缆检查脚本

集成工程线缆检查脚本

脚本位置

https://onedayxyy.cn/scripts/check_line/

image-20240405142747410

CheckLine_2019-10-27.py

[root@docusaurus-wiki check_line]#cat CheckLine_2019-10-27.py 
###19-10-25 《加入光衰检查 带外ipmi模块加载》
###19-10-27 《加入关闭系统自带的lldp,第一次使用较久》
import datetime
import xlrd
import xlsxwriter
import paramiko
import time
import string
def Sysname_cut(host_port): #交换机主机名
try:
stdin, stdout, stderr = ssh.exec_command("lldptool -t -n -i " + host_port + " | grep -A 1 \'System Name TLV\'")
tx = stdout.read().decode()
if tx == "":
stdin, stdout, stderr = ssh.exec_command("for i in `ls /sys/kernel/debug/i40e/`; do echo 'lldp stop' > /sys/kernel/debug/i40e/$i/command ; done;")
time.sleep(10)
stdin, stdout, stderr = ssh.exec_command("lldptool -t -n -i " + host_port + " | grep -A 1 \'System Name TLV\'")
tx = stdout.read().decode()
else:
pass
temp=str(tx)
temp1 = temp.split(" ")
sysname = str(temp1[1])
sysname=sysname.split("\n")[0]
return sysname
except IndexError:
print("list index out of range")

def Port_cut(host_port): #交换机端口
try:
stdin, stdout, stderr = ssh.exec_command("lldptool -t -n -i "+host_port+" | grep Ifname")
tx = stdout.read().decode()
temp = tx.split(": ")
port = str(str(temp[1]).split("\n")[0])
return port
except IndexError:
print("list index out of range")

def Hostname(): #服务器主机名
try:
stdin, stdout, stderr = ssh.exec_command("hostname")
hostname = stdout.read().decode().strip()
return hostname
except IndexError:
print("list index out of range")

def ServerIPMI(): #服务器IPMI地址
try:
stdin1, stdout1, stderr1 = ssh.exec_command("lsmod |grep ^ipmi")
m = stdout1.read().decode().strip()
if m != "":
stdin, stdout, stderr = ssh.exec_command("ipmitool lan print | grep 'IP Address '")
s = stdout.read().decode().strip()
ipmi = s.split(":")[1]
return ipmi
else:
stdin2, stdout2, stderr2 = ssh.exec_command("modprobe ipmi_watchdog&&modprobe ipmi_poweroff&&modprobe ipmi_devintf&&modprobe ipmi_si&&modprobe ipmi_msghandler")
f = stdout2.read().decode().strip()
time.sleep(2)
if f == None:
print("error")
else:
stdin, stdout, stderr = ssh.exec_command("ipmitool lan print | grep 'IP Address '")
s = stdout.read().decode().strip()
ipmi = s.split(":")[1]
return ipmi
except Exception as e:
print("ErrorInfo: %s" % e)


def ServerIPMImask(): #服务器IPMI掩码
try:
stdin, stdout, stderr = ssh.exec_command("ipmitool lan print | grep \'Subnet Mask\'")
s = stdout.read().decode().strip()
ipmi = s.split(":")[1]
return ipmi
except IndexError:
print("list index out of range")


def ServerIPMIgway(): #服务器IPMI网关
try:
stdin, stdout, stderr = ssh.exec_command("ipmitool lan print | grep \'Default Gateway IP\'")
s = stdout.read().decode().strip()
ipmi = s.split(":")[1]
return ipmi
except IndexError:
print("list index out of range")

def ServerSN(): #服务器序列号SN
try:
stdin, stdout, stderr = ssh.exec_command("dmidecode -s system-serial-number")
SN = stdout.read().decode()
return SN
except IndexError:
print("list index out of range")

def ServerNetMAC(port): #服务器网卡MAC地址
try:
stdin, stdout, stderr = ssh.exec_command(" cat /sys/class/net/" + port + "/address")
netMac = stdout.read().decode()
return netMac
except IndexError:
print("list index out of range")

def ServerNetSpeed(port): #服务器网卡速率
try:
stdin, stdout, stderr = ssh.exec_command("ethtool " + port + " | grep Speed | awk -F \':\' \'{print $2}\'")
netSpeed = stdout.read().decode().strip()
return netSpeed
except IndexError:
print("list index out of range")

def SysHostname(port): #交换机端口描述中服务器编号
try:
stdin, stdout, stderr = ssh.exec_command("lldptool -t -n -i " + port + " | grep -A 1 \'Port Description TLV\'")
s = stdout.read().decode().strip()
temp=str(s)
temp1 = temp.split(" ")
sysname = str(temp1[1])
sysHostname1 = sysname.split("\n")[0]
if "." in sysHostname1:
sysHostname = sysHostname1.split(":")[1].split(".")[0]
return sysHostname
else:
return sysHostname1
except IndexError:
print("list index out of range")

def SysVlan(port): #交换机端口所属VLAN
try:
stdin, stdout, stderr = ssh.exec_command("lldptool -t -n -i " + port + " | grep -A 1 \'Port VLAN ID TLV\' ")
s = stdout.read().decode()
temp=str(s)
temp1 = temp.split(" ")
vlan = str(temp1[1])
temp2 = vlan.split("\n")[0]
sysvlan = temp2.split(":")[1]
return sysvlan
except IndexError:
print("list index out of range")

def Send_Light(port):
stdin, stdout, stderr = ssh.exec_command("ethtool -m "+ port+" | grep \"Laser output power\" | head -1 | awk -F \'\/\' \'{print $2}\'")
tx = stdout.read().decode().strip()
return tx

def Receive_Light(port):
stdin, stdout, stderr = ssh.exec_command("ethtool -m "+ port +" | grep \"Receiver signal\"| awk -F \'/\' \'{print $2}\'")
rx = stdout.read().decode().strip()
return rx

def LightRS(Port):
send_light = Send_Light(Port)
if "dBm" not in send_light:
worksheet.write(i, 9, "no lightinfo",color)
else:
sl = str(send_light.split(" ")[0])
if "+" in sl:
worksheet.write(i, 9, send_light,color)
else:
sl1 = int(str(sl.replace("-","").split(".")[0]))
#print(sl1)
if sl1>=5:
worksheet.write(i, 9, send_light,color)
else:
worksheet.write(i, 9, send_light) #光衰发光
receive_light = Receive_Light(Port)
if "dBm" not in receive_light:
worksheet.write(i, 10, "no lightinfo",color)
else:
rl = str(receive_light.split(" ")[0])
if "+" in rl:
worksheet.write(i, 10, receive_light,color)
else:
rl1 = int(str(rl.replace("-","").split(".")[0]))
#print(rl1)
if rl1>=5:
worksheet.write(i, 10, receive_light,color)
else:
worksheet.write(i, 10, receive_light) #光衰收光

data = xlrd.open_workbook('D:/Port2.xlsx')
#data = xlrd.open_workbook('C:/Users/liwei/Desktop/Port-huawei-CD.xlsx')
table = data.sheets()[0]
nrows = table.nrows

workbook = xlsxwriter.Workbook('D:/SFG.xlsx')
worksheet = workbook.add_worksheet()
title = [u'服务器IP',u'服务器主机名',u'服务器IPMI',u'IPMI地址掩码',u'IPMI地址网关',u'序列号SN',u'服务器网卡名称',u'服务器网卡MAC',u'服务器网卡速率',u'光衰发光',u'光衰收光',u'交换机主机名',u'交换机端口',u'交换机端口所属VLAN',u'交换机端口描述中服务器编号',u'是否与服务器一致',u'lldp收集时间']
format=workbook.add_format()
format.set_border(1)
format_title=workbook.add_format()
format_title.set_border(1)
format_title.set_bg_color('#cccccc')
format_title.set_align('center')
format_title.set_bold()
format_ave=workbook.add_format()
format_ave.set_border(1)
format_ave.set_num_format('0.00')
worksheet.write_row('A1',title,format_title)
color = workbook.add_format({'bg_color':'red'})
Now_Time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
i = 1
while i < nrows:
ip = table.cell(i,0)
Port = table.cell(i,1)
Port = str(Port).split("'")[1]
ip = str(ip).split("'")[1]
print(ip)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(hostname=ip, port=22, username='root', password='123456',timeout=3)
worksheet.write(i, 0, ip) #服务器IP
hostname = Hostname()
worksheet.write(i, 1, hostname) #服务器主机名
worksheet.write(i, 2, ServerIPMI()) #服务器IPMI
worksheet.write(i, 3, ServerIPMImask()) #服务器IPMI掩码
worksheet.write(i, 4, ServerIPMIgway()) #服务器IPMI网关
worksheet.write(i, 5, ServerSN()) #服务器SN
worksheet.write(i, 6, Port) #服务器网卡名称
worksheet.write(i, 7, ServerNetMAC(Port)) #服务器网卡MAC
speed = ServerNetSpeed(Port)
worksheet.write(i, 8, speed) #服务器网卡速率
value = str(speed.strip().split("M")[0])
if int(value) > 1000:
LightRS(Port)
else:
worksheet.write(i, 9, "非光口")
worksheet.write(i, 10, "非光口")
worksheet.write(i, 11, Sysname_cut(Port)) #交换机主机名
worksheet.write(i, 12, Port_cut(Port)) #交换机端口
worksheet.write(i, 13, SysVlan(Port)) #交换机端口所属VLAN
sys_hostname = SysHostname(Port)
worksheet.write(i, 14, sys_hostname) #交换机端口描述中服务器编号
if hostname != sys_hostname:
status = "false"
else:
status = "true"
worksheet.write(i, 15, status) #是否与服务器一致
worksheet.write(i, 16, Now_Time) #lldp收集时间
time.sleep(1)
i=i+1
ssh.close()
except Exception as e:
print("Errorinfo:%s" %e)
i=i+1
workbook.close()

lldp.sh

[root@docusaurus-wiki check_line]#cat lldp.sh 
#!/bin/bash
lldpad -d

for i in `ls /sys/class/net/ | grep e` ;
do echo "enabling lldp for interface: $i" ;
lldptool set-lldp -i $i adminStatus=rxtx ;
lldptool -T -i $i -V sysName enableTx=yes;
lldptool -T -i $i -V portDesc enableTx=yes ;
lldptool -T -i $i -V sysDesc enableTx=yes;
lldptool -T -i $i -V sysCap enableTx=yes;
lldptool -T -i em1 -V mngAddr ipv4=`hostname -I`;
lldptool -T -i $i -V mngAddr enableTx=yes;
done

脚本使用前提条件

1.目标设备必须是linux机器

2.目标机器可以被ssh 次是通过python ssh模块来获取信息的,因此设备必须要可以ssh到其上面的;

3.本次py脚本是在windows机器下的(请注意,可上外网),即winodws上有python环境(python3版本)

脚本使用时需要修改的地方

image-20240405141604229

1.运行次py脚本前,必须先要在所有linux机器上运行lldp.sh脚本:

image-20240405141710922

即在安装系统前,要保证安装lldpad软件包:

image-20240405141820036

2.修改py脚本中ssh端口,用户名及用户密码即可。

image-20240405141931672

3.注意ip列表/输出文件存放路径

image-20240405142110884

image-20240405142157276

4.保证如下python模块已被安装,如未安装,则在执行过程会报错,根据提示,使用pip安装即可:

image-20240405142223078

脚本输出结果说明

1.输出结果如下文件:SFG.xlsx

image-20240405142248245

# 次脚本输出的信息如下:

系统主机名;

带外ip、掩码、网关;

sn;

服务器网卡名、网卡mac、网卡速率、收发光衰;

上联交换机主机名、交换机端口、所属vlan、交换机下服务器描述信息、

2.注意:如果有光衰异常的情况,次脚本会对次目标进行标注:

image-20240405142312754