質問編集履歴
2
文章の体裁
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,11 +7,11 @@
|
|
7
7
|
|
8
8
|
ご教授の程よろしくお願いいたします。
|
9
9
|
|
10
|
-
|
10
|
+
|
11
11
|
YouheiSakurai様のご回答によりTimeoutError発生後も停止せずに以下任意のエラー出力と共に処理継続する事が確認できました!
|
12
|
-
|
13
12
|
## SSH connection failed for xx.xx.xx.xxx ##
|
14
13
|
|
14
|
+
修正後のコード:
|
15
15
|
------------------------
|
16
16
|
```ここに言語を入力
|
17
17
|
#Modules
|
1
修正版コードの追加、処理結果記入
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,13 +7,23 @@
|
|
7
7
|
|
8
8
|
ご教授の程よろしくお願いいたします。
|
9
9
|
|
10
|
+
修正後のコード:
|
11
|
+
YouheiSakurai様のご回答によりTimeoutError発生後も停止せずに以下任意のエラー出力と共に処理継続する事が確認できました!
|
12
|
+
|
13
|
+
## SSH connection failed for xx.xx.xx.xxx ##
|
14
|
+
|
10
15
|
------------------------
|
16
|
+
```ここに言語を入力
|
17
|
+
#Modules
|
11
18
|
import paramiko
|
19
|
+
from contextlib import suppress
|
20
|
+
from paramiko import SSHException
|
12
21
|
|
13
22
|
#Variables
|
14
23
|
USER = 'UserID'
|
15
24
|
PSWD = 'password'
|
16
25
|
|
26
|
+
|
17
27
|
#Classes and Functions
|
18
28
|
class InputReader:
|
19
29
|
def __init__(self, commands_path, hosts_path):
|
@@ -26,6 +36,7 @@
|
|
26
36
|
|
27
37
|
def __readlines(self, path):
|
28
38
|
with open(path) as f:
|
39
|
+
# return map(lambda v: v.strip(), f.readlines())
|
29
40
|
return [v.strip() for v in f.readlines()] #List comprehension
|
30
41
|
|
31
42
|
class CommandExecuter:
|
@@ -34,6 +45,64 @@
|
|
34
45
|
self.command = command
|
35
46
|
|
36
47
|
def execute(self):
|
48
|
+
with suppress(TimeoutError):
|
49
|
+
ssh = paramiko.SSHClient()
|
50
|
+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
51
|
+
ssh.connect(self.host, username=USER, password=PSWD)
|
52
|
+
|
53
|
+
stdin, stdout, stderr = ssh.exec_command(self.command)
|
54
|
+
|
55
|
+
errors = stderr.readlines()
|
56
|
+
|
57
|
+
lines = [v.strip() for v in stdout.readlines()]
|
58
|
+
return lines
|
59
|
+
print('## SSH connection failed for %s ##' % h + '\n')
|
60
|
+
|
61
|
+
#Main Procedure
|
62
|
+
if __name__ == '__main__':
|
63
|
+
reader = InputReader("commands2.txt", "systems2.txt")
|
64
|
+
reader.read()
|
65
|
+
|
66
|
+
for h in reader.hosts:
|
67
|
+
for c in reader.commands:
|
68
|
+
executer = CommandExecuter(h, c)
|
69
|
+
results = executer.execute()
|
70
|
+
print("IP:{0}({1}):".format(h, c) + '\n')
|
71
|
+
if results != None:
|
72
|
+
for i in results:
|
73
|
+
print(i + '\n')
|
74
|
+
|
75
|
+
```
|
76
|
+
|
77
|
+
修正前のコード:
|
78
|
+
------------------------
|
79
|
+
```ここに言語を入力
|
80
|
+
import paramiko
|
81
|
+
|
82
|
+
#Variables
|
83
|
+
USER = 'UserID'
|
84
|
+
PSWD = 'password'
|
85
|
+
|
86
|
+
#Classes and Functions
|
87
|
+
class InputReader:
|
88
|
+
def __init__(self, commands_path, hosts_path):
|
89
|
+
self.commands_path = commands_path
|
90
|
+
self.hosts_path = hosts_path
|
91
|
+
|
92
|
+
def read(self):
|
93
|
+
self.commands = self.__readlines(self.commands_path)
|
94
|
+
self.hosts = self.__readlines(self.hosts_path)
|
95
|
+
|
96
|
+
def __readlines(self, path):
|
97
|
+
with open(path) as f:
|
98
|
+
return [v.strip() for v in f.readlines()] #List comprehension
|
99
|
+
|
100
|
+
class CommandExecuter:
|
101
|
+
def __init__(self, host, command):
|
102
|
+
self.host = host
|
103
|
+
self.command = command
|
104
|
+
|
105
|
+
def execute(self):
|
37
106
|
ssh = paramiko.SSHClient()
|
38
107
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
39
108
|
ssh.connect(self.host, username=USER, password=PSWD)
|
@@ -61,4 +130,5 @@
|
|
61
130
|
if results != None:
|
62
131
|
for i in results:
|
63
132
|
print(i)
|
64
|
-
print('\n')
|
133
|
+
print('\n')
|
134
|
+
```
|