网络管理员必须掌握Python
Python已经成为网络管理员的日常管理工具, 本文通过一段代码来展示如何使用.
在python code中调用系统ping命令
下面这段程序先测试目标地址是否能ping通, 如果可以ping通, 就执行traceroute 命令.
import datetime
import subprocess
# monitoring time is when the script start
monitor_start_time = datetime.datetime.now()
monitoring_date_time = "Testing started at: " + str(monitor_start_time).split(".")[0]
ip_list = ["8.8.8.8"]
for ip in ip_list:
print("********* Connecting to " + ip + " *********" + '\n')
# print started time
print(monitoring_date_time + '\n')
print
ping_result = subprocess.getoutput(" ping -c 4 " + ip)
print(ping_result + '\n'