私はネットワークスイッチにtelnetしてコマンドを実行する小さなスクリプトを持っています。それはうまく動作しますが、コマンドのいずれかが失敗したり、ネットワークが失速したり何かを与えたりすると、停止してエラーメッセージを表示する必要があります。私は、スクリプトが失敗するコマンドの後に複数のコマンドの実行を停止したい失敗したコマンドや警告のメッセージをキャッチするPythonスクリプト
import sys
import telnetlib
HOST = "10.10.10.1"
user = "Test"
password = "TestPW"
tn = telnetlib.Telnet(HOST)
tn.read_until("username: ")
tn.write(user + "\n")
tn.read_until("password: ")
tn.write(password + "\n")
n, match, previous_text = tn.expect([r'Login incorrect', r'\$'], 1)
if n == 0:
print "Invalid Credentials"
tn.close()
else:
tn.write("Term len 0\n")
#Reads data from commands.txt file and executes it line by line
with open("commands.txt") as commands:
singlecommand = commands.read()
tn.write(singlecommand)
print singlecommand
#Need exception/error checking to catch fail commands or warnings.
tn.write("exit\n")
tn.write("y\n")
print tn.read_all()
tn.close()
または多分何かが間違っているCLIからの警告:ここで
はコードです。すでに印刷機能があるので、失敗したエラーメッセージとコマンドが表示されるはずです。 「^」マーカーで検出%無効な入力:ここ
は、失敗したコマンドの例です。ここで
警告メッセージの例です:
%警告: