python – SSHException ('No existing session') when starting 500 multiprocessing processes creating ssh connections using paramiko


I am trying to create a script for configuring hosts over ssh via paramiko using the multiprocessing module. Since the nodes are about 500, processing them in turn takes a lot of time. There is the following code:

import time
import sys
import paramiko
import socket
import re
import multiprocessing
host = open('/home/user/hosts', 'r')
hosts = host.readlines()
def connect_ssh(host, queue):
            remote_conn_pre = paramiko.SSHClient()


            remote_conn_pre.connect(host, username=username, password=password, look_for_keys=False, allow_agent=False, timeout=5)

            print("SSH connection established to %s" % host)
            remote_conn = remote_conn_pre.invoke_shell()
            print("Interactive SSH session established")

            ###здесь операции с хостами####

               queue.put({host: output})

        except (socket.gaierror,socket.timeout,TimeoutError):
                print("Could not connect to %s \n" % host)

def send_commands(function):
    processes = []
    queue = multiprocessing.Queue()
    for l in range(len(hosts)):
        p = multiprocessing.Process(target = function, args = (hosts[l], queue))
    for p in processes:

    results = []
    for p in processes:
    return results


If you apply it to a small group of nodes (about 10 for example), then everything works correctly. But one has only to start execution on all 500 devices, then on half (and randomly) the following error falls out:

Traceback (most recent call last):
  File "/usr/lib/python3.5/multiprocessing/", line 249, in _bootstrap
  File "/usr/lib/python3.5/multiprocessing/", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "scripts/", line 24, in connect_ssh
    remote_conn_pre.connect(host, username=username, password=password, look_for_keys=False, allow_agent=False, timeout=5)
  File "/usr/local/lib/python3.5/dist-packages/paramiko/", line 341, in connect
    server_key = t.get_remote_server_key()
  File "/usr/local/lib/python3.5/dist-packages/paramiko/", line 670, in get_remote_server_key
    raise SSHException('No existing session')

At the same time, as far as I understand, the error is typical for cases when the keys look_for_keys = False and allow_agent = False are not specified in connect paramiko. But I have them. What could be the catch?


If you want to configure 500 hosts over ssh, check out a very cool tool – ansible

A lot of recipes have already been made for him, and in life the knowledge of such a tool is more useful than the invention of the bicycle.

Scroll to Top