teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

質問内容修正

2021/01/06 04:58

投稿

ji-desuji
ji-desuji

スコア11

title CHANGED
@@ -1,1 +1,1 @@
1
- paramikoによるSSH接続でのwarning、exceptionについて
1
+ paramikoによるSSH接続でのOS Errorについて
body CHANGED
@@ -1,16 +1,35 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- paramikoを使って、SSH接続をしようとしていますが、下記のwarning、exceptionが出てしまいます。
3
+ paramikoを使って、SSH接続をしようとしていますが、下記のOSError: Socket is closedが出てしまいます。
4
- この後に、stdin.write('xxxx')でコマンドを送ろうとすると、socket is closedのexceptionが出て、実行が止まってしまいます。
5
- warningとexceptionを解消すれば良いと考えています。
4
+ ただ、そ前に下記のexceptionが出ているので、
5
+
6
+ ■Socket exception: 既存の接続はリモート ホストに強制的に切断されました。 (10054)
7
+
8
+ これが原因かと思っています。
6
9
  解消方法を教えて頂けると助かります。
7
10
 
8
11
  ### 発生している問題・エラーメッセージ
9
12
 
10
13
  ```
11
- 835: UserWarning: Unknown ecdsa-sha2-nistp521 host key for 192.168.**.***: b'******************************'
12
- warnings.warn(
13
14
  Socket exception: 既存の接続はリモート ホストに強制的に切断されました。 (10054)
15
+ Traceback (most recent call last):
16
+ File "c:/Users/Desktop/macro/main.py", line 56, in <module>
17
+ stdin.write('int ethernet 1/1')
18
+ File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\paramiko\file.py", line 405, in write
19
+ self._write_all(data)
20
+ File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\paramiko\file.py", line 522, in _write_all
21
+ count = self._write(data)
22
+ File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\paramiko\channel.py", line 1364, in _write
23
+ self.channel.sendall(data)
24
+ File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\paramiko\channel.py", line 846, in sendall
25
+ sent = self.send(s)
26
+ File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\paramiko\channel.py", line 801, in send
27
+ return self._send(s, m)
28
+ File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\paramiko\channel.py", line 1198, in _send
29
+ raise socket.error("Socket is closed")
30
+ OSError: Socket is closed
31
+
32
+
14
33
  ```
15
34
 
16
35
  ### 該当のソースコード
@@ -18,20 +37,26 @@
18
37
  ```python
19
38
  client = paramiko.SSHClient()
20
39
  client.set_missing_host_key_policy(paramiko.WarningPolicy())
40
+ client.load_host_keys(os.path.join(os.path.dirname(__file__), 'known_hosts'))
21
41
  client.connect(IP_address, username=ID, password=Password, timeout=3.0)
22
42
 
23
43
  stdin, stdout, stderr = client.exec_command('dir')
44
+ stdin.write('cd test\n')
45
+ stdin.flush()
46
+
47
+ for out in stdout:
48
+ print('[std]', out, end='')
49
+ for err in stderr:
50
+ print('[err]', err, end='')
51
+
24
52
  ```
25
53
 
26
54
  ### 試したこと
27
55
 
28
- known_hostsに追加し実行してみましたが、同じwarning、exceptionが出
56
+ その他、色々なサイトを見てみましたが、まく分からず・・・
29
- client.load_host_keys(os.path.join(os.path.dirname(__file__), 'known_hosts'))
30
57
 
31
- ■その他、色々なサイトを見てみましたが、良く分からず・・・
32
58
 
33
59
 
34
-
35
60
  ### 補足情報(FW/ツールのバージョンなど)
36
61
 
37
62
  win10

3

IP address修正

2021/01/06 04:58

投稿

ji-desuji
ji-desuji

スコア11

title CHANGED
File without changes
body CHANGED
@@ -8,7 +8,7 @@
8
8
  ### 発生している問題・エラーメッセージ
9
9
 
10
10
  ```
11
- 835: UserWarning: Unknown ecdsa-sha2-nistp521 host key for 192.168.90.254: b'******************************'
11
+ 835: UserWarning: Unknown ecdsa-sha2-nistp521 host key for 192.168.**.***: b'******************************'
12
12
  warnings.warn(
13
13
  Socket exception: 既存の接続はリモート ホストに強制的に切断されました。 (10054)
14
14
  ```

2

誤記修正

2021/01/06 04:47

投稿

ji-desuji
ji-desuji

スコア11

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,7 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
3
  paramikoを使って、SSH接続をしようとしていますが、下記のwarning、exceptionが出てしまいます。
4
- この後に、stdin.write('xxxx')でコマンドを送ろうとすると、socket is closedのexceptionが出て、実行が止まっています。
4
+ この後に、stdin.write('xxxx')でコマンドを送ろうとすると、socket is closedのexceptionが出て、実行が止まってしまいます。
5
5
  なので、このwarningとexceptionを解消すれば良いと考えています。
6
6
  解消方法を教えて頂けると助かります。
7
7
 

1

コードの誤記修正

2021/01/06 04:36

投稿

ji-desuji
ji-desuji

スコア11

title CHANGED
File without changes
body CHANGED
@@ -18,7 +18,7 @@
18
18
  ```python
19
19
  client = paramiko.SSHClient()
20
20
  client.set_missing_host_key_policy(paramiko.WarningPolicy())
21
- client.connect(Raritan_IP_address, username=Raritan_ID, password=Raritan_Password, timeout=3.0)
21
+ client.connect(IP_address, username=ID, password=Password, timeout=3.0)
22
22
 
23
23
  stdin, stdout, stderr = client.exec_command('dir')
24
24
  ```