0
私は数時間にわたりこれに対して頭を叩き、リサーチやリファクタリングを行ってきましたが、動作させることができません。Python:文字列チェックの文字列が失敗しました
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")
出力:
スイッチポート:GE-0/0/3
ライン:GE-0/0/3.0 *、GE-0/0/47.0、GE-0/1/3.0 *
チェック
私はelifの部分にある失敗の問題を抱えています主要部分を失敗しました。他のインスタンスを除いて、シンタックスとロジックでほぼ一致しました。ループのために私を投げているのは、switchPortが印刷された行の文字列に一致するということです。誰かがこれを踏みにじっているかもしれない考えを持っていますか?
チェックの前に両方の変数を文字列に変換しようとしましたが、それが機能しませんでした。
'stdout.readline()'を
に(。それは新しい、剥奪、文字列を返します
str.strip
何も変更されません):修正は変更することがありますか? –'SwitchPort'に'
'がありますが、' line'にはありません。 – TigerhawkT3
時々コンソールの中で見えにくい文字列間に小さな違いがあります。 'print( 'SwitchPort:{!r}'。format(switchPort))'と 'print( '行:{!r}'。書式(行))'を試してください。違いをより簡単に見つけることができます。 – smarx