我几个小时来一直在反对这个问题,研究和重构,但我无法让它发挥作用。
import paramiko
import sys
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ssh.connect(switch, username='user',
password='pass')
stdin,stdout,stderr = ssh.exec_command("show interfaces descriptions")
line = stdout.readline()
while line != "":
if ("UNIT " + unit) in line:
switchPort = line [:9]
switchPort.strip()
line = stdout.readline()
print (switchPort)
command = "show vlans"
stdin,stdout,stderr = ssh.exec_command(command)
line = stdout.readline()
while line != "":
if acronym + "-s" in line or acronym + "-r" in line or ("subscribed" in line and "un" not in line and "pvlan" not in line):
line.strip(' ')
subscribedVlan = ''.join([i for i in line if i.isdigit()])
line=stdout.readline()
if switchPort in line:
portVlan = "Subscribed"
elif "un" in line and "pvlan" not in line:
unsubscribedVlan = ''.join([i for i in line if i.isdigit()])
if switchPort in line:
portVlan = "Unsubscribed"
else:
line=stdout.readline()
print ("SwitchPort: " + switchPort)
print ("line: " + line)
if switchPort in line:
portVlan = "Unsubscribed"
print ("In Unsubscribed")
else:
print("Check Failed")
输出:
SwitchPort:ge-0/0/3
line:ge-0/0 / 3.0 *,ge-0/0 / 47.0,ge-0/1 / 3.0 *
检查失败
我遇到失败问题的主要部分是elif部分。除了其他实例之外,我几乎完全匹配if in语法和逻辑。抛弃循环的原因是switchPort打印匹配一串线。有没有人知道可能会绊倒什么?
我尝试在检查之前将两个变量转换为字符串,但这不起作用。
答案 0 :(得分:0)
有时在控制台中很容易看到的字符串之间存在细微差别。试试这个:
print('SwitchPort: {!r}'.format(switchPort))
print('line: {!r}'.format(line))
这可能会让人更容易发现。
根据上面的讨论,这里的实际问题是一个尾随空格。修复方法是改变:
switchPort.strip()
到
switchPort = switchPort.strip()
(str.strip
不会修改任何内容;它会返回一个新的,剥离的字符串。)