(+84) 236.3827111 ex. 402

Script python Tự động tìm và reset các IP PHONE


Trong công ty có nhiều IP PHONE (PoE) chạy lâu ngày bị đơ lag, nhiều khi sếp gọi nhân viên nhưng không có chuông kêu. Mỗi lần thế IT lại cần reboot lại con PHONE thì lại chạy ok. Nhưng mất công và bị động.

ý tưởng: Ta viết script chạy vào mỗi cuối tuần, tự động reboot các ip phone bằng cách:

- Login vào các switch cắm Phone

- Tìm địa chỉ MAC của phone trên switch, (sẽ có phần MAC chung của các phone, theo từng vendor),

- Tìm ra port ứng với các MAC đó rồi shutdown, no shut trở lại.

- Do phone chạy PoE nên sẽ được reboot.

\

Script

from netmiko import ConnectHandler

import re

#khai báo các switch

sw_user2={

'device_type': 'cisco_ios',

'ip': '192.168.64.100',

'username': 'admin',

'password': 'abc@1234',

'secret': 'abc@1234',

'verbose': False,

}

sw_user3={

'device_type': 'cisco_ios',

'ip': '192.168.64.101',

'username': 'admin',

'password': 'abc@1234',

'secret': 'abc@1234',

'verbose': False,

}

sw_user4={

'device_type': 'cisco_ios',

'ip': '192.168.64.102',

'username': 'admin',

'password': 'abc@1234',

'secret': 'abc@1234',

'verbose': False,

}

#------------------------Login vào switch-----------------------#

all_switches=[sw_user2,sw_user3,sw_user4]

for switches in all_switches:

net_connect=ConnectHandler(**switches)

net_connect.enable()

print('\n\n********************shut and no shut cac port tren '+ switches['ip'] +'******************')

wr=net_connect.send_command_timing("show mac address-table | include (c074|000b)") ##MáC CỦA IP PHONE HÃNG GRANDSTREAM

f=open('temp.txt','w') ##Ghi mac vào file tạm

f.write(wr)

f.close()

f=open('temp.txt','r') ##Mở file tạm ra

linehai=f.readlines()

for i in range (0,len(linehai)):

if ('Gi' in linehai[i]):

port=re.findall(r'Gi.*',linehai[i])[0] ##Lấy port tương ứng với MAC

#print(port)

command= "int "+port+" \n shut \n no shut \n" ##Lệnh shutdown, no shutdown

print(command)

net_connect.send_command_timing('conf t')

net_connect.send_command_timing(command) ##Thực hiện lệnh ở trên