最近跟做一个项目,项目中用到的centos7系统,不知道设么原因,系统的开机脚本设置一无线网卡为monitor模式,却总是发现启不起来,有的时候启起来又不会自己down掉,有的时候又会自己变成Manster模式。按理说,别的程序也没有用到这个网卡,就我自己的程序用到了,但是我也没有去改变网卡模式,所以就很奇怪,怀疑是网络管理服务等的影响的原因。
为了保持网卡的工作模式和up状态,就写了个简单的脚本(其实只是为了日更字数才废话)。
#!/usr/bin/python
#-- coding: utf-8 --
#author: Zbertj
import os
import sys
argv_netcard = ""
def check_status():
print("[*]run check_status")
if len(sys.argv) != 2:
print("argv is err")
sys.exit(-1)
global argv_netcard
argv_netcard = sys.argv[1]
ifconfig_process = os.popen('ifconfig')
ifconfig_result = ifconfig_process.read()
ifconfig_process.close()
if ifconfig_result:
ret = ifconfig_result.find(argv_netcard)
if ret > 0:
print(argv_netcard + " status: up")
return ret
else:
print(argv_netcard + "status: down")
return ret
print("ifconfig: " + argv_netcard + "not found")
return -1
def check_mode_monitor():
print("[*]run check_mode_monitor")
str = "iwconfig " + argv_netcard
iwconfig_process = os.popen(str)
iwconfig_result = iwconfig_process.read()
iwconfig_process.close()
if iwconfig_result:
ret = iwconfig_result.find("Mode:Monitor")
if ret > 0:
print(argv_netcard + " mode: monitor")
return ret
else:
print(argv_netcard + " mode not monitor")
return ret
print("iwconfig: " + argv_netcard + " not found")
return -1
def set_mode_monitor():
print("[*]run set_mode_monitor")
cmd_str1 = "ifconfig " + argv_netcard + " down"
os.system(cmd_str1)
cmd_str2 = "iwconfig " + argv_netcard + " mode monitor"
os.system(cmd_str2)
cmd_str3 = "ifconfig " + argv_netcard + " up"
os.system(cmd_str3)
check_mode_monitor()
if __name__ == '__main__':
ret = check_status()
if ret > 0:
ret = check_mode_monitor()
if ret > 0:
pass
else:
set_mode_monitor()
else:
set_mode_monitor()