Pythonオブジェクト指向プログラミングは勉強の段階の身なのですが、以下ParamikoモジュールによるSSH接続と接続先の複数のLinuxサーバー上でコマンド実行し結果を返す処理を行うプログラムを知人の助けを借りて作成しました。(別txtファイルから接続先サーバーIPと実行コマンドを読み込む仕様です)
追加したい機能はSSH接続に失敗した際にエラー例外処理を行い、エラー内容とメッセージを出力後、処理を最後まで継続するように変更する事です。現在はSSH処理が失敗した段階で以降の処理が停止してしまう状態です。
https://www.programcreek.com/python/example/7495/paramiko.SSHException
上記ページを参照し試行錯誤してみてはみますが、オブジェクト指向で書かれた処理にどう綺麗に当該処理を加える事ができるのか今のところ上手くいっておりません。
ご教授の程よろしくお願いいたします。
YouheiSakurai様のご回答によりTimeoutError発生後も停止せずに以下任意のエラー出力と共に処理継続する事が確認できました!
SSH connection failed for xx.xx.xx.xxx
修正後のコード:
#Modules import paramiko from contextlib import suppress from paramiko import SSHException #Variables USER = 'UserID' PSWD = 'password' #Classes and Functions class InputReader: def __init__(self, commands_path, hosts_path): self.commands_path = commands_path self.hosts_path = hosts_path def read(self): self.commands = self.__readlines(self.commands_path) self.hosts = self.__readlines(self.hosts_path) def __readlines(self, path): with open(path) as f: # return map(lambda v: v.strip(), f.readlines()) return [v.strip() for v in f.readlines()] #List comprehension class CommandExecuter: def __init__(self, host, command): self.host = host self.command = command def execute(self): with suppress(TimeoutError): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(self.host, username=USER, password=PSWD) stdin, stdout, stderr = ssh.exec_command(self.command) errors = stderr.readlines() lines = [v.strip() for v in stdout.readlines()] return lines print('## SSH connection failed for %s ##' % h + '\n') #Main Procedure if __name__ == '__main__': reader = InputReader("commands2.txt", "systems2.txt") reader.read() for h in reader.hosts: for c in reader.commands: executer = CommandExecuter(h, c) results = executer.execute() print("IP:{0}({1}):".format(h, c) + '\n') if results != None: for i in results: print(i + '\n')
修正前のコード:
import paramiko #Variables USER = 'UserID' PSWD = 'password' #Classes and Functions class InputReader: def __init__(self, commands_path, hosts_path): self.commands_path = commands_path self.hosts_path = hosts_path def read(self): self.commands = self.__readlines(self.commands_path) self.hosts = self.__readlines(self.hosts_path) def __readlines(self, path): with open(path) as f: return [v.strip() for v in f.readlines()] #List comprehension class CommandExecuter: def __init__(self, host, command): self.host = host self.command = command def execute(self): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(self.host, username=USER, password=PSWD) stdin, stdout, stderr = ssh.exec_command(self.command) errors = stderr.readlines() '''if len(errors) != 0: #raise Exception(errors)''' lines = [v.strip() for v in stdout.readlines()] #ssh.close() return lines #Main Procedure if __name__ == '__main__': reader = InputReader("commands.txt", "systems.txt") reader.read() for h in reader.hosts: for c in reader.commands: executer = CommandExecuter(h, c) results = executer.execute() print("{0}({1}):".format(h, c)) if results != None: for i in results: print(i) print('\n')

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/16 13:40
2017/12/16 15:12 編集