Skip to content

集成工程线缆检查脚本

集成工程线缆检查脚本

脚本位置

https:###19-10-25 《加入光衰检查 带外ipmi模块加载》###19-10-27 《加入关闭系统自带的lldp,第一次使用较久》importdatetimeimportxlrdimportxlsxwriterimportparamikoimporttimeimportstringdefSysname_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()iftx =="":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:passtemp=str(tx)temp1 =temp.split("")sysname =str(temp1[1])sysname=sysname.split("\n")[0]returnsysnameexceptIndexError:print("list index out of range")defPort_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])returnportexceptIndexError:print("list index out of range")defHostname():#服务器主机名try:stdin,stdout,stderr =ssh.exec_command("hostname")hostname =stdout.read().decode().strip()returnhostnameexceptIndexError:print("list index out of range")defServerIPMI():#服务器IPMI地址try:stdin1,stdout1,stderr1 =ssh.exec_command("lsmod |grep ^ipmi")m =stdout1.read().decode().strip()ifm !="":stdin,stdout,stderr =ssh.exec_command("ipmitool lan print |grep 'IP Address '")s =stdout.read().decode().strip()ipmi =s.split(":")[1]returnipmielse: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)iff ==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]returnipmiexceptExceptionase:print("ErrorInfo:%s"%e)defServerIPMImask():#服务器IPMI掩码try:stdin,stdout,stderr =ssh.exec_command("ipmitool lan print |grep \'Subnet Mask\'")s =stdout.read().decode().strip()ipmi =s.split(":")[1]returnipmiexceptIndexError:print("list index out of range")defServerIPMIgway():#服务器IPMI网关try:stdin,stdout,stderr =ssh.exec_command("ipmitool lan print |grep \'Default Gateway IP\'")s =stdout.read().decode().strip()ipmi =s.split(":")[1]returnipmiexceptIndexError:print("list index out of range")defServerSN():#服务器序列号SNtry:stdin,stdout,stderr =ssh.exec_command("dmidecode -s system-serial-number")SN=stdout.read().decode()returnSNexceptIndexError:print("list index out of range")defServerNetMAC(port):#服务器网卡MAC地址try:stdin,stdout,stderr =ssh.exec_command("cat /sys/class/net/"+port +"/address")netMac =stdout.read().decode()returnnetMacexceptIndexError:print("list index out of range")defServerNetSpeed(port):#服务器网卡速率try:stdin,stdout,stderr =ssh.exec_command("ethtool "+port +"|grep Speed |awk -F \':\'\'{print $2}\'")netSpeed =stdout.read().decode().strip()returnnetSpeedexceptIndexError:print("list index out of range")defSysHostname(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"."insysHostname1:sysHostname =sysHostname1.split(":")[1].split(".")[0]returnsysHostnameelse:returnsysHostname1exceptIndexError:print("list index out of range")defSysVlan(port):#交换机端口所属VLANtry: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] returnsysvlanexceptIndexError:print("list index out of range")defSend_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()returntxdefReceive_Light(port):stdin,stdout,stderr =ssh.exec_command("ethtool -m "+port +"|grep \"Receiver signal\"|awk -F \'/\'\'{print $2}\'")rx =stdout.read().decode().strip()returnrxdefLightRS(Port):send_light =Send_Light(Port)if"dBm"notinsend_light:worksheet.write(i,9,"no lightinfo",color)else:sl =str(send_light.split("")[0])if"+"insl:worksheet.write(i,9,send_light,color)else:sl1 =int(str(sl.replace("-","").split(".")[0]))#print(sl1)ifsl1>=5:worksheet.write(i,9,send_light,color)else:worksheet.write(i,9,send_light) #光衰发光receive_light =Receive_Light(Port)if"dBm"notinreceive_light:worksheet.write(i,10,"no lightinfo",color)else:rl =str(receive_light.split("")[0])if"+"inrl:worksheet.write(i,10,receive_light,color)else:rl1 =int(str(rl.replace("-","").split(".")[0]))#print(rl1)ifrl1>=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.nrowsworkbook =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 =1whilei <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) #服务器IPhostname =Hostname()worksheet.write(i,1,hostname) #服务器主机名worksheet.write(i,2,ServerIPMI()) #服务器IPMIworksheet.write(i,3,ServerIPMImask()) #服务器IPMI掩码worksheet.write(i,4,ServerIPMIgway()) #服务器IPMI网关worksheet.write(i,5,ServerSN()) #服务器SNworksheet.write(i,6,Port) #服务器网卡名称worksheet.write(i,7,ServerNetMAC(Port)) #服务器网卡MACspeed =ServerNetSpeed(Port)worksheet.write(i,8,speed) #服务器网卡速率value =str(speed.strip().split("M")[0])ifint(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)) #交换机端口所属VLANsys_hostname =SysHostname(Port)worksheet.write(i,14,sys_hostname) #交换机端口描述中服务器编号ifhostname !=sys_hostname:status ="false"else:status ="true"worksheet.write(i,15,status) #是否与服务器一致worksheet.write(i,16,Now_Time) #lldp收集时间time.sleep(1)i=i+1ssh.close()exceptExceptionase:print("Errorinfo:%s"%e)i=i+1workbook.close()

lldp.sh

脚本使用前提条件

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