Using Python asking router ARP, export to csv format data, or checking specify device is on ARP list or not, and trying to added back.
- Python-ARP-Tools
ARP-Tools store configuration as JSON format file, named config.json
.
You can editing the clean copy, which looks like this:
{
"last_update_time": "",
"server": "192.168.0.1",
"user_prompt": "login:",
"user": "admin",
"password_prompt": "Password:",
"password": "•••••••••••••••",
"terminal_prompt": "user@router:/# ",
"command": "arp",
"interface_prompt": "-s",
"filter_list": [],
"device_ip": "",
"device_mac_address": ""
}
This module needs some configuration which is necessary.
Telnet function is the alternative solution for NETGEAR official firmware, If you know what is DD-WRT and luckily your router is under support list, try commond line function.
Python Telnet function using telnetlib, which need some necessery configure, which store at config.json
.
If the configuration file not found, it will needed to initialize
Configuration not found, please initialize.
Please enter the Server IP address: 192.168.01.1
Please enter the prompt when asking username: login:
Please enter the username: admin
Please enter the prompt when asking password: Passowrd:
Please enter the password: •••••••••••••••
Please setting the telnet prompt: user@router:/#
Please enter the device IP address (can be skipped):
Please enter the device MAC address (can be skipped):
- Server IP address is the address for your router, store key is
server
- Prompt when asking username is the prompt when typing username, store key is
user_prompt
- User is the administrator's username, usually is admin, store key is
user
- Prompt when asking password is the prompt when typing password, store key is
password_prompt
- Password is the administrator's password, store key is
password
- Telnet prompt depend on you router hostname and prompt, store key is
terminal_prompt
.
The parameter depend on you router hostname and prompt, in my condition, my NETGEAR router is root@RBS50:/#
, DD-WRT default is root@DDWRT:/#
. Blank character at the ending are needed.
If you wnat to keep specify device on ARP list, you need to complete the configuration.
Please enter the device IP address (can be skipped):
Please enter the device MAC address (can be skipped):
- Device IP address is the specify device IP address, store key is
device_ip
- Device MAC address is the specify device MAC address, store key is
device_mac_address
To processing ARP data into dataframe correctly, filter_list
need to configure. First directly export ARP data, check the output file arp_format_check.csv
to know which columns needs to drop, then set filter_list
value to List
. For example:
"filter_list": [6,7,8],
Network interface and ARP command format may quite different on different platform or device, check you environments, then modify the parameter.
"interface_prompt": "-s",
Base on different platform or device, even different firmware version, the MAC address format may quite different.
Default trans MAC address into lower-case
#Trans ARP list into lowercase
comparison_list = [x.lower() for x in comparison_list]
#Try to match
if device_keep.lower() in comparison_list:
In my condition, my router store MAC address in lower-case, and the separate character using :
Base on different platform or device, even different firmware version, ARP refresh times are different, and traditional routers are quite different from Mesh Wi-Fi.
For example, NETGEAR Orbi AC3000 system, RBR50 main station and RBS50 satellite station has separated ARP cache
, so careful, Mesh Wi-Fi environments may not be compatible.
- Import the module
import arp_tools
- Alternatively, you can import the function independent
from arp_tools import arp_output
import arp_tools
#Run asking
result = arp_tools.arp_output(directly_mode=True)
#Check result
if result is None:
print("ARP data dumping fail, error occurred during data processing.\r\nPlease check the error.log")
elif type(result) is bool:
if bool(result) is True:
print("Router connection refused.")
elif bool(result) is False:
print("Error occurred when connect to router.\r\nPlease check the error.log")
else:
print("ARP data save as 'arp_format_check.csv'.")
If the telnet function successfully login, and asking data, the result will print.
Login Router ...
Login success
Sending ARP request to router
Receive ARP data from router
Logout Router ...
2021-02-22 10:16:07 | ARP data dump complete
ARP data save as 'arp_format_check.csv'.
If error occurred, it will return Boolean
or None
as the result.
import arp_tools
#Run asking
result = arp_tools.arp_output(directly_mode=False)
#Check result
if result is None:
print("ARP data dumping fail, error occurred during data processing.\r\nPlease check the error.log")
elif type(result) is bool:
if bool(result) is True:
print("Router connection refused.")
elif bool(result) is False:
print("Error occurred when connect to router.\r\nPlease check the error.log")
else:
print("ARP data save as 'arp_data_output.csv'.")
If the telnet function successfully login, and asking data, the result will print.
Login Router ...
Login success
Sending ARP request to router
Receive ARP data from router
Logout Router ...
2021-02-22 10:17:48 | ARP data dump complete
ARP data save as 'arp_data_output.csv'.
If error occurred, it will return Boolean
or None
as the result.
Checking specify device is on ARP list or not, and trying to added back.
import arp_tools
#Run asking
result = arp_tools.arp_overwatch()
#Check result
if result is None:
print("ARP data dumping fail, error occurred during data processing.\r\nPlease check the error.log")
elif type(result) is int:
print("ARP update command has been amended.")
elif type(result) is str:
print("No needed to update.")
elif type(result) is bool:
if bool(result) is True:
print("Router connection refused.")
elif bool(result) is False:
print("Error occurred when connect to router.\r\nPlease check the error.log")
If the telnet function successfully login, and need to update ARP, the result will print.
Initialization, event log create
Login Router ...
Login success
Sending ARP request to router
Receive ARP data from router
2021-02-21 22:43:24 | Update
Logout
It will return integer
as the result.
If the device already on ARP table, the result will print.
Login Router ...
Login success
Sending ARP request to router
Receive ARP data from router
2021-02-21 22:43:27 | Device already on the list
Logout
It will return string
as the result.
If error occurred, it will return Boolean
or None
as the result.
Directly export ARP data will store at arp_format_check.csv
:
IP,address,HW,type,Flags,HW.1,address.1,Mask,Device
10.0.1.7,0x1,0x2,2e:07:75:cc:9d:01,*,br0,,,
10.0.1.6,0x1,0x2,5e:bd:ef:3c:38:35,*,br0,,,
10.0.1.15,0x1,0x2,26:ff:10:f0:6e:2a,*,br0,,,
The ARP data after processing will store at arp_data_output.csv
:
IP address,HW type,Flags,HW address,Mask,Device
10.0.1.19,0x1,0x2,2e:07:75:cc:9d:01,*,br0
10.0.1.15,0x1,0x2,26:ff:10:f0:6e:2a,*,br0
10.0.1.6,0x1,0x2,5e:bd:ef:3c:38:35,*,br0
The ARP Check/Update event will store at arp_placeholder.csv
:
time,event
2021-01-17 20:07:55,check
2021-01-17 20:08:01,update
- Python 3.6 or above
- csv
- json
- time
- pandas
- logging
- datetime
- telnetlib
- io
- getpass
General Public License -3.0