2016-07-26 11 views
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が印刷された行の文字列に一致するということです。誰かがこれを踏みにじっているかもしれない考えを持っていますか?

チェックの前に両方の変数を文字列に変換しようとしましたが、それが機能しませんでした。

+0

'stdout.readline()'を

switchPort.strip() 

switchPort = switchPort.strip() 

に(。それは新しい、剥奪、文字列を返しますstr.strip何も変更されません):修正は変更することがありますか? –

+0

'SwitchPort'に'
'がありますが、' line'にはありません。 – TigerhawkT3

+0

時々コンソールの中で見えにくい文字列間に小さな違いがあります。 'print( 'SwitchPort:{!r}'。format(switchPort))'と 'print( '行:{!r}'。書式(行))'を試してください。違いをより簡単に見つけることができます。 – smarx

答えて

0

時にはコンソールには見えない文字列間に小さな違いがあることがあります。これを試してみてください:

print('SwitchPort: {!r}'.format(switchPort)) 
print('line: {!r}'.format(line)) 

おそらく違いが分かりやすくなります。

上記の説明では、実際の問題は後に続くスペースでした。 ;

関連する問題