質問編集履歴
2
文章の体裁
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,16 +16,16 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
+
|
20
|
+
|
21
|
+
YouheiSakurai様のご回答によりTimeoutError発生後も停止せずに以下任意のエラー出力と共に処理継続する事が確認できました!
|
22
|
+
|
23
|
+
## SSH connection failed for xx.xx.xx.xxx ##
|
24
|
+
|
25
|
+
|
26
|
+
|
19
27
|
修正後のコード:
|
20
28
|
|
21
|
-
YouheiSakurai様のご回答によりTimeoutError発生後も停止せずに以下任意のエラー出力と共に処理継続する事が確認できました!
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
## SSH connection failed for xx.xx.xx.xxx ##
|
26
|
-
|
27
|
-
|
28
|
-
|
29
29
|
------------------------
|
30
30
|
|
31
31
|
```ここに言語を入力
|
1
修正版コードの追加、処理結果記入
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,10 +16,28 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
+
修正後のコード:
|
20
|
+
|
21
|
+
YouheiSakurai様のご回答によりTimeoutError発生後も停止せずに以下任意のエラー出力と共に処理継続する事が確認できました!
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
## SSH connection failed for xx.xx.xx.xxx ##
|
26
|
+
|
27
|
+
|
28
|
+
|
19
29
|
------------------------
|
20
30
|
|
31
|
+
```ここに言語を入力
|
32
|
+
|
33
|
+
#Modules
|
34
|
+
|
21
35
|
import paramiko
|
22
36
|
|
37
|
+
from contextlib import suppress
|
38
|
+
|
39
|
+
from paramiko import SSHException
|
40
|
+
|
23
41
|
|
24
42
|
|
25
43
|
#Variables
|
@@ -30,6 +48,8 @@
|
|
30
48
|
|
31
49
|
|
32
50
|
|
51
|
+
|
52
|
+
|
33
53
|
#Classes and Functions
|
34
54
|
|
35
55
|
class InputReader:
|
@@ -54,6 +74,8 @@
|
|
54
74
|
|
55
75
|
with open(path) as f:
|
56
76
|
|
77
|
+
# return map(lambda v: v.strip(), f.readlines())
|
78
|
+
|
57
79
|
return [v.strip() for v in f.readlines()] #List comprehension
|
58
80
|
|
59
81
|
|
@@ -70,6 +92,122 @@
|
|
70
92
|
|
71
93
|
def execute(self):
|
72
94
|
|
95
|
+
with suppress(TimeoutError):
|
96
|
+
|
97
|
+
ssh = paramiko.SSHClient()
|
98
|
+
|
99
|
+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
100
|
+
|
101
|
+
ssh.connect(self.host, username=USER, password=PSWD)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
stdin, stdout, stderr = ssh.exec_command(self.command)
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
errors = stderr.readlines()
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
lines = [v.strip() for v in stdout.readlines()]
|
114
|
+
|
115
|
+
return lines
|
116
|
+
|
117
|
+
print('## SSH connection failed for %s ##' % h + '\n')
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
#Main Procedure
|
122
|
+
|
123
|
+
if __name__ == '__main__':
|
124
|
+
|
125
|
+
reader = InputReader("commands2.txt", "systems2.txt")
|
126
|
+
|
127
|
+
reader.read()
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
for h in reader.hosts:
|
132
|
+
|
133
|
+
for c in reader.commands:
|
134
|
+
|
135
|
+
executer = CommandExecuter(h, c)
|
136
|
+
|
137
|
+
results = executer.execute()
|
138
|
+
|
139
|
+
print("IP:{0}({1}):".format(h, c) + '\n')
|
140
|
+
|
141
|
+
if results != None:
|
142
|
+
|
143
|
+
for i in results:
|
144
|
+
|
145
|
+
print(i + '\n')
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
```
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
修正前のコード:
|
154
|
+
|
155
|
+
------------------------
|
156
|
+
|
157
|
+
```ここに言語を入力
|
158
|
+
|
159
|
+
import paramiko
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
#Variables
|
164
|
+
|
165
|
+
USER = 'UserID'
|
166
|
+
|
167
|
+
PSWD = 'password'
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
#Classes and Functions
|
172
|
+
|
173
|
+
class InputReader:
|
174
|
+
|
175
|
+
def __init__(self, commands_path, hosts_path):
|
176
|
+
|
177
|
+
self.commands_path = commands_path
|
178
|
+
|
179
|
+
self.hosts_path = hosts_path
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
def read(self):
|
184
|
+
|
185
|
+
self.commands = self.__readlines(self.commands_path)
|
186
|
+
|
187
|
+
self.hosts = self.__readlines(self.hosts_path)
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
def __readlines(self, path):
|
192
|
+
|
193
|
+
with open(path) as f:
|
194
|
+
|
195
|
+
return [v.strip() for v in f.readlines()] #List comprehension
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
class CommandExecuter:
|
200
|
+
|
201
|
+
def __init__(self, host, command):
|
202
|
+
|
203
|
+
self.host = host
|
204
|
+
|
205
|
+
self.command = command
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
def execute(self):
|
210
|
+
|
73
211
|
ssh = paramiko.SSHClient()
|
74
212
|
|
75
213
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
@@ -125,3 +263,5 @@
|
|
125
263
|
print(i)
|
126
264
|
|
127
265
|
print('\n')
|
266
|
+
|
267
|
+
```
|