我使用 Adafruit 16x2 RGB LCD 为 Raspberry Pi 项目编写了一个小脚本:
def KismetScanner():
print 'Kismet Scan'
#clearlogs = subprocess.Popen('rm -r /var/log/kismet; mkdir /var/log/kismet', shell=True, stderr=subprocess.PIPE, stdout=PIPE)
monitormode = subprocess.Popen('service ifplugd stop; ifconfig wlan0 down; iwconfig wlan0 mode monitor; ifconfig wlan0 up', shell=True)
lcd.clear()
lcd.backlight(lcd.YELLOW)
lcd.message("Enabled\nMonitor Mode")
sleep(2)
lcd.clear()
lcd.message("Scanning for\nAccessPoints 60s")
kismetscan = subprocess.Popen('kismet&', shell=True)
sleep(60)
grep1essid = subprocess.Popen('grep -oP "SSID.*>\K.*(?=<)" /var/log/kismet/Kismet-Feb-28-2015-1.xml | sed -n 1p', shell=True, stderr=subprocess.PIPE, stdout=PIPE)
grep1aOut, grep1aErr = grep1essid.communicate()
grep1bssid = subprocess.Popen('grep -oP "SSID.*>\K.*(?=<)" /var/log/kismet/Kismet-Feb-28-2015-1.xml | sed -n 2p', shell=True, stderr=subprocess.PIPE, stdout=PIPE)
grep1bOut, grep1bErr = grep1bssid.communicate()
print grep1aOut
print grep1bOut
lcd.clear()
lcd.backlight(lcd.GREEN)
lcd.message(grep1aOut + '\n' + grep1bOut)
monitormodeoff = subprocess.Popen('ifconfig wlan0 down; iwconfig wlan0 mode managed; ifconfig wlan0 up; service ifplugd start', shell=True)
sleep(2)
该脚本运行 Kismet 扫描并提取第一个找到的 SSID 和 BSSID,并将其打印在 LCD 上。问题是在将 SSID 和 BSSID 存储在 grep1aOut 和 grep1bOut 中之后,它会打印正确的输出,但不会将其显示在 LCD 上。只有 SSID,所以显示第一行,我不明白为什么。
那么错误在哪里呢?
答案1
我敢打赌这些grep*Out
变量包含尾随换行符,因为它们是 Popen 的 stderr/stdout...这可以解释它,因为显示屏只打印两行。尝试这个:
msg = grep1aOut.strip() + '\n' + grep1bOut.strip()
lcd.message(msg)