Search code examples
pythonmultithreadingsshparamikossh-agent

AttributeError: module 'paramiko.win_pageant' has no attribute 'can_talk_to_agent' when connecting using Paramiko from Windows Server


I am currently working on a program that attempts contacting numerous routers which run the Cisco IOS to get their current configs. I am trying to achieve this using the Paramiko module's SSHClient object:

def get_config(file_path, ip, ip_number):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip, port=22, username="user", password="pw", look_for_keys=False, timeout=None)
    stdin,stdout, stderr = ssh.exec_command("show running-config \n")

After that, the configs are written to a specific file inside of a subfolder which has the variable ip_number as its name. As this is done with over a hundred routers, I tried using the threading module to speed up the process. The problem I kept running into started to occur more often when I used more threads at once, so I made the program only work with 14 threads at a time (the server is running on a 14 core CPU) in this fashion:

amount_ip_blocks = int(len(ip_list))/14
if len(ip_list) - 14*amount_ip_blocks != 0:
    amount_remaining_ips = len(ip_list) - 14*amount_ip_blocks

for j in range (0, amount_ip_blocks):
    threads = []
    for i in range (j*14, j*14 + 14):
        thread = threading.Thread(target=get_config, args=(path, ip_list[i], i,))
        thread.start()
        threads.append(thread)
    for thread in threads:
        thread.join()

Finally, the same is done for the remaining IPs.

Now, when working on the first block of 14 IPs, this code inconsistently gives me the following exception anywhere between zero and four times at once on varying threads (it is not always thread-1):

Exception in thread Thread-1:
Traceback (most recent call last):
File "D:\Program Files\Python\lib\threading.py", line 932, in _bootstrap_inner

self.run()
File "D:\Program Files\Python\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "D:\location_of_my_program\config_getter.py", line 99, in get_config
ssh.connect(ip, port=22, username="user", password="pw", look_for_keys=False, timeout=None)
File "D:\Program Files\Python\lib\site-packages\paramiko-2.7.2-py3.8.egg\paramiko\client.py", line 435, in connect
File "D:\Program Files\Python\lib\site-packages\paramiko-2.7.2-py3.8.egg\paramiko\client.py", line 691, in _auth
File "D:\Program Files\Python\lib\site-packages\paramiko-2.7.2-py3.8.egg\paramiko\agent.py", line 372, in __init__
AttributeError: module 'paramiko.win_pageant' has no attribute 'can_talk_to_agent'

I have tried looking online for this exception, but wasn't able to find anything. I have also tried looking into paramiko's code to see if it would make any sense to me, but to no avail. Finally, I have tried adjusting the size of the IP blocks, smaller blocks seem to cause the issue less oftenly, and if it occurs, it always does on the first IP block. After extensive experimentation, I wasn't able to deduce any regularity. Is there a way to fix the issue? Or is it simply not possible to have multiple SSH connections running with paramiko at the same time / in the same program? Or better yet, does anybody know how this issue even occurs?


Solution

  • I do not have a solution, but maybe a workaround. As you do not seem to use the Agent, did you try to turn it off?

    Set allow_agent=False in SSHClient.connect call.