首页 > 解决方案 > 用于抓取 web gui 字符串的脚本,Python 2.7 上的错误在 Python 3 上运行

问题描述

作为我工作的一部分,我可能需要访问 Cisco 电话(Cisco 8851、7960 等)的 GUI 以确定分配给设备的 MAC 地址。MAC 地址以 SEP 为前缀(例如 SEPAABBCCDDEEFF),因为呼叫处理服务器提供的系统日志在特定事件中仅包括 IP 地址,而不包括 MAC 地址。需要 MAC 地址来确认服务器上该设备是否存在给定的一组配置。手动拉起一大块 50-100 部电话通过 http 检查 MAC 地址是很糟糕的。我尝试将其自动化,并且确实做到了,但我错过了 Python3 中的标记并完全在 Python2 上失败,不会运行超过用户输入集合。

我的问题:

1) 关于 awk 和/或类似工具的一般问题——检查从网站返回的数据是否有必要的 SEP* 字符串我使用 awk 打印出存在于同一行的多个 SEP* 实例,但它提供了两个输出而不仅仅是第一个。我试过使用“grep -o”SEP*”,但这只提供 SEP 作为响应。关于如何让它返回 SEP* 的第一个实例(例如 SEPAABBCCDDEEFF)的想法,而不是一整行 html 代码?

问题 #1 - 如您所见,awk 尝试确实干净地提供了第一个实例,但第二个实例在两端都提供了很长的垃圾。我的意图是只为它解析的每个 Web 链接提供一个 SEP* 值。

@ubuntu:~/Scripts/CiscoScripts$ python transientPhones.py 
How many phones?: 1
What is the phone IP address?: <ip-addr>
SEPAABBCCDDEEFF
width=20></TD><TD><B>SEPAABBCCDDEEFF</B></TD></TR><TR><TD><B>
kenneth@ubuntu:~/Scripts/CiscoScripts$ 

2) 在 Python 2.7 环境中运行脚本,脚本因 SyntaxError 失败:收集用户输入后的语法无效。我不明白为什么(除了我做错了或以不兼容的方式)。我的家庭环境是 python 3.x(最新),在编写要在 python 2.7 环境中使用的脚本时我没有考虑到这一点,而且由于我是 python 和编码的新手,我实际上只是在学习 python3 语法和约定. 这里有什么想法吗?

问题#2——这个让我很困惑。我敢肯定这里有一个简单的答案/解决方案......我没有足够的经验来看到它。

$:python transientPhones.py
How many phones?: 1
What is the phone IP address?: 192.168.1.1
Traceback (most recent call last):
  File "transientPhones.py", line 13, in <module>
    ipAddress.append(input('What is the phone IP address?: '))
  File "<string>", line 1
    192.168.1.1
            ^
SyntaxError: invalid syntax

代码:

#!/usr/bin/python
#import required modules
import subprocess

#Define Variables
x = input('How many phones?: ')
x = int(x)
ipAddress = []

#Loop to grab IP addresses
for i in range(x) : #Loop X amount of times based on input from user
        ipAddress.append(input('What is the phone IP address?: '))

#Grab XML Data and awk it for SEP*.
for n in ipAddress :
        subprocess.call ("curl --max-time 5 -s http://" + n + "/CGI/Java/Serviceability?adapter=device.statistics.device | awk '/SEP*/{for(i=1;i<=NF;++i)if($i~/SEP*/)print $i}'", shell=True)

标签: python

解决方案


您会收到此错误,因为 Python 2 中的输入和 Python 3 中的输入是完全不同的函数。

在 Python 2 中,获取用户输入并执行它,而在 Python 3 中,您可以使用. 因此,您将 192.168.1.1 称为 Python 代码。当然,这不是有效的 Python 代码,因此您会收到 SyntaxError。input eval(input(...))

在 Python 3 中,只是获取用户输入,而在 Python 2中则做同样的事情。input raw_input

这意味着在您的情况下,您需要使用raw_inputPython 2 和inputPython 3。当然,您可以只替换inputraw_input,但如果您尝试在 Python 3 上运行代码,它将无法正常工作。

这个问题有一些很好的解决方案。您可以在 Python 2 上使用 raw_input 重新定义输入函数,并将所有内容保留在 Python 3 上。

#!/usr/bin/python
#import required modules
import subprocess

try:
    input = raw_input
except NameError:
    pass

#Define Variables
x = input('How many phones?: ')
x = int(x)
ipAddress = []

#Loop to grab IP addresses
for i in range(x) : #Loop X amount of times based on input from user
        ipAddress.append(input('What is the phone IP address?: '))

#Grab XML Data and awk it for SEP*.
for n in ipAddress:
        subprocess.call("curl --max-time 5 -s http://" + n + "/CGI/Java/Serviceability?adapter=device.statistics.device | awk '/SEP*/{for(i=1;i<=NF;++i)if($i~/SEP*/)print $i}'", shell=True)

查看此答案以获取更多信息:Use of input/raw_input in python 2 and 3


推荐阅读