Sorry to be vague, I just didnt know what part of my code to submit, Below is the block its stuck on. To answer some of the questions asked: I wrote it in Windows 10, I installed paramiko and pycrypto using pip3, and lastly I am not sure it is running perfectly in pycharm, just that it runs and exits correctly.
import socket
import userPassword
import paramiko
import Batching
import GetUser
from GetHosts import hosts
key_dict = {}
user = GetUser.getUser()
l_password = ufitadminPassword.getuserPassword()
l_host = hosts()
l_commands = ["sudo -k chown -R user/home/%s/" % user, "cat /home/%s/.ssh/authorized_keys" % user]
l_user = "user"
generator = Batching.batched(l_host, 10)
generator = list(generator)
def exisingKeys():
for host in l_host:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, username=l_user, password=l_password, timeout=5)
except paramiko.SSHException:
print("Connection Failed on host %s" % host)
continue
except paramiko.AuthenticationException:
print("authentication Exception on host %s" % host)
continue
except socket.error:
print("Socket Error on host %s" % host)
continue
transport = ssh.get_transport()
print("connected to %s" % host)
session = transport.open_session()
session.set_combine_stderr(True)
session.get_pty()
for commands in l_commands:
try:
session = transport.open_session()
except socket.error:
print("Socket Error on host %s" % host)
continue
session.set_combine_stderr(True)
session.get_pty()
session.exec_command(commands)
stdin = session.makefile('wb', -1)
stdout = session.makefile('rb', -1)
try:
stdin.write(l_password + '\n')
except:
socket.error("Error writing password")
continue
stdin.flush()
print("Verifying Keys for host: %s " % host)
key_list = []
for line in stdout.read().splitlines():
input_line = line
input_line = input_line.replace(b'[sudo] password for user: ', b'')
key_individual = str(input_line, 'utf-8')
key_individual = key_individual.replace(
'cat: /home/%s/.ssh/authorized_keys: No such file or directory' % user,
'No key exists for this user.')
key_list.append(key_individual)
print(key_individual)
print("Key verified for host: %s" % host)
print("--------------------------------------------------------------")
key_dict[host] = key_list
filterString = "No key exists for this user."
filteredKeylist = [key for key, values in key_dict.items() if filterString in values]
return filteredKeylist