def pxsshConnect(f_key, ip, port, username, cryptPasswd, verbose): if verbose: print("--- Connecting with pxssh ...") from pexpect import pxssh print() try: s = pxssh.pxssh() s.login(ip, username, decryptPassword(f_key, cryptPasswd, verbose), port=int(port)) s.sendline("uptime") # run a command s.prompt() # match the prompt # print(s.before) # print everything before the prompt. pxsshPrint(s.before) print() s.sendline("ls -l") s.prompt() # print(s.before) pxsshPrint(s.before) print() s.sendline("df") s.prompt() pxsshPrint(s.before) # print(s.before) print() s.logout() except pxssh.ExceptionPxssh as e: print("pxssh failed on login.") print(str(e))
def editConnections(f_key, connectionFile, show, verbose): print("\nEdit connection\n----------") if show: print("----------------------------------------------------") ip, host, port, userNo, username, cryptPasswd = selectConnection( f_key, connectionFile, show, verbose) maxNumber = 5 print("\nSelect what to edit:") print(" 1: IP: " + ip) print(" 2: Hostname: " + host) print(" 3: Port : " + port) if userNo >= 0: print(" 4: Username: "******" 5: Password: "******" 5: Password: *****") else: maxNumber = 3 print("\nEnter number:") while True: selection = input(" ? ") if not selection: print("\nYou must select a number 1-" + str(maxNumber) + "\nTry again") else: try: selection = int(selection) except: print("Only integers allowed\nTry again:") else: if selection <= 0 or selection > maxNumber: print("Number must be 1-" + str(maxNumber)) else: break if selection == 1: changeSectionName(ip, host, connectionFile, show, verbose) elif selection == 2: # option, value, connectionFile, show, verbose changeValue(ip, "Hostname", host, connectionFile, show, verbose) elif selection == 3: changeValue(ip, "Port", port, connectionFile, show, verbose) elif selection == 4: # f_key, userNo, oldUsername, connectionFile, show, verbosee changeUsername(f_key, ip, userNo, username, cryptPasswd, connectionFile, show, verbose) elif selection == 5: changePassword(f_key, ip, userNo, cryptPasswd, connectionFile, show, verbose)
def viewConnections(f_key, connectionFile, show, verbose): connectionNo = 0 # ciphered_text = f_key.encrypt(b"SuperSecretPassword") #required to be bytes # print("\nEncrypted text: " + str(ciphered_text)) # unciphered_text = (f_key.decrypt(ciphered_text)) # print("\nDecrypted text: " + str(unciphered_text)) # plain_text_encryptedpassword = bytes(unciphered_text).decode("utf-8") #convert to string # print("\nPlain text password: "******"\nConnection " + str(connectionNo) + "\n----------") print(section) print(" Port: " + config.get(section, "port")) print(" Hostname: " + config.get(section, "hostname")) options = config.options(section) for option in options: if option.startswith("username"): print("\n User: "******"password"): cryptPasswd = config.get(section, option) if show: plainTextPass = decryptPassword(f_key, cryptPasswd, verbose) print(" Pass: "******" Pass: " + cryptPasswd) print()
def changePassword(f_key, ip, userNo, oldCryptPasswd, connectionFile, show, verbose): from getpass import getpass if verbose: print("\n--- Changing password for password" + str(userNo) + " \n in section [" + ip + "]") oldPlainTextPasswd = decryptPassword(f_key, oldCryptPasswd, verbose) if show: print("\nOld password: "******"\nOld passwd: *****") if verbose: print("\n--- Reading config file ...") config = configparser.ConfigParser() config.read(connectionFile) # read config fil while True: # input password print("\nPassword ") if show: passwd1 = input("[" + decryptPassword(f_key, oldCryptPasswd, verbose) + "] ? ") # enter password invisible else: passwd1 = getpass("[*****] ? ") # enter password visibly if not passwd1: # if no password is given accept the default one passwd1 = decryptPassword(f_key, oldCryptPasswd, verbose) print("\nEnter password again") if show: passwd2 = input("[" + decryptPassword(f_key, oldCryptPasswd, verbose) + "] ? ") # enter password invisible else: passwd2 = getpass("[*****] ? ") # enter password visibly if not passwd2: # if no password is given accept the default one passwd2 = decryptPassword(f_key, oldCryptPasswd, verbose) if passwd1 == passwd2: # check if the same password was given both times break else: print("\nPasswords do not match\nTry again") if oldPlainTextPasswd != passwd1: if show: print("\nChanging password from " + oldPlainTextPasswd + " to " + passwd1 + " ...") else: print("\nChanging password ...") newEncPasswd = encryptPassword(f_key, passwd1, show, verbose) if verbose: print("\n--- Writing new passwd ...") config.set(ip, "password" + str(userNo), newEncPasswd) else: if passwd1: print("\nYou have entered the old password again") print("\nNo changes made") if verbose: print("\n--- Writing to config file ...") with open(connectionFile, "w") as configfile: config.write(configfile) # write everything to config file
def changeUsername(f_key, ip, userNo, oldUsername, oldCryptPasswd, connectionFile, show, verbose): if verbose: print("\n--- Changing username for username" + str(userNo) + " \n in section [" + ip + "]") print("\nOld username: "******"\nEnter new username, or 'd' to delete") newUsername = input("[" + oldUsername + "]/d ? ") if newUsername.lower() == "d": deleteUser = True break if (not newUsername or newUsername == oldUsername): # if no username stated, accept old username newUsername = oldUsername print( "\nYou've entered the same username again\nKeep old username?") correct = input("(Y/n) ? ") if correct.lower() != "n": # if anything but 'n' was stated print("\nKeeping old username") break else: break if verbose: print("\n--- Reading config file ...") config = configparser.ConfigParser() config.read(connectionFile) # read config file if not deleteUser: changedUsername = False if oldUsername != newUsername: print("\nChanging username from " + oldUsername + " to " + newUsername + " ...") if verbose: print("\n--- Writing new username ...") config.set(ip, "username" + str(userNo), newUsername) changedUsername = True else: print("\nNo changes made") else: # delete user print("\nAre you sure you want to delete user " + oldUsername) delete = input("(y/N) ? ") if delete.lower() == "y": print("\nDeleting user " + oldUsername + " with password") config.remove_option(ip, "username" + str(userNo)) # delete user and pass config.remove_option(ip, "password" + str(userNo)) # move the last user (if there is one) to the now empty username# remainUsers = 0 options = config.options(ip) for option in options: if option.startswith("username"): remainUsers += 1 if verbose: print("\n--- " + str(remainUsers) + " users now remains") if ( remainUsers >= 1 and remainUsers != userNo ): # if there are more than one users remaining and it wasn't the last user we deleted if verbose: print("\n--- Getting username and password for user " + str(remainUsers)) oldUser = config.get( ip, "username" + str(remainUsers)) # read old user and pass oldPasswd = config.get(ip, "password" + str(remainUsers)) if verbose: print("\n--- Moving user " + oldUser) if show: print(" with password " + decryptPassword(f_key, oldPasswd, verbose)) else: print(" with password " + oldPasswd) config.remove_option( ip, "username" + str(remainUsers)) # delete old user and pass config.remove_option(ip, "password" + str(remainUsers)) config.set(ip, ("username" + str(userNo)), oldUser) # rewrite old user and pass config.set(ip, ("password" + str(userNo)), oldPasswd) if verbose: print("\n--- Writing to config file ...") with open(connectionFile, "w") as configfile: config.write(configfile) # write everything to config file changedUsername = False else: print("\nNo changes made") if verbose: print("\n--- Writing to config file ...") with open(connectionFile, "w") as configfile: config.write(configfile) # write everything to config file if changedUsername: print("\nWould you also like to change the password") changePasswd = input("(y/N) ? ") if changePasswd.lower() == "y": changePassword(f_key, ip, userNo, oldCryptPasswd, connectionFile, show, verbose)
def makeConnection(connectionType, f_key, connectionFile, show, verbose): # ssh if connectionType == "ssh": ip, host, port, userNo, username, cryptPasswd = selectConnection( f_key, connectionFile, show, verbose) if userNo >= 0: print("\nWill connect to " + host + " at " + ip + " on port " + port + " as " + username + " who has user index " + str(userNo)) if show: print("\nUse password '" + decryptPassword(f_key, cryptPasswd, verbose) + "'") sshConnect(ip, port, username, "", verbose) else: print("\nCan't make a connection to " + host) # ssh -X elif connectionType == "ssh -X": ip, host, port, userNo, username, cryptPasswd = selectConnection( f_key, connectionFile, show, verbose) if userNo >= 0: print("\nWill connect to " + host + " at " + ip + " on port " + port + " as " + username + " who has user index " + str(userNo)) if show: print("\nUse password '" + decryptPassword(f_key, cryptPasswd, verbose) + "'") sshConnect(ip, port, username, "-X", verbose) else: print("\nCan't make a connection") # ssh -Y elif connectionType == "ssh -Y": ip, host, port, userNo, username, cryptPasswd = selectConnection( f_key, connectionFile, show, verbose) if userNo >= 0: print("\nWill connect to " + host + " at " + ip + " on port " + port + " as " + username + " who has user index " + str(userNo)) if show: print("\nUse password '" + decryptPassword(f_key, cryptPasswd, verbose) + "'") sshConnect(ip, port, username, "-Y", verbose) else: print("\nCan't make a connection to " + host) # sftp elif connectionType == "sftp": ip, host, port, userNo, username, cryptPasswd = selectConnection( f_key, connectionFile, show, verbose) if userNo >= 0: print("\nWill connect to " + host + " at " + ip + " on port " + port + " as " + username + " who has user index " + str(userNo)) if show: print("\nUse password '" + decryptPassword(f_key, cryptPasswd, verbose) + "'") sftpConnect(f_key, ip, port, username, cryptPasswd, verbose) else: print("\nCan't make a connection to " + host) # ssh-copy-id elif connectionType == "ssh-copy-id": keyFile = sshCreateKey(verbose) if verbose: print("\n--- Will use public key at " + keyFile) ip, host, port, userNo, username, cryptPasswd = selectConnection( f_key, connectionFile, show, verbose) if userNo >= 0: print("\nWill transfer key from " + keyFile + " \nto " + host + " at " + ip + " on port " + port + " as " + username) if show: print("\nUse password '" + decryptPassword(f_key, cryptPasswd, verbose) + "'") sshCopyID(ip, port, username, keyFile, verbose) else: print("\nCan't transfer key") # run command on multiple hosts elif connectionType == "run command on multiple hosts": print("\nEnter command:") while True: cmd = input("[uptime] ? ") if cmd: break else: # print("You must enter a command\nTry again") cmd = "uptime" break connectionList = [] print("Select connections to run command") while True: ip, host, port, userNo, username, cryptPasswd = selectConnection( f_key, connectionFile, show, verbose) connectionList.append({ "ip": ip, "host": host, "port": port, "userNo": userNo, "username": username, "cryptPasswd": cryptPasswd, }) print("\nConnections added:") for connection in connectionList: print(" Host: " + connection["host"] + " User: "******"username"]) print("\nDo you want to add another connection") add = input("(y/N) ? ") if add.lower() != "y": break for connection in connectionList: if userNo >= 0: if connection["username"] == "root": prompt = "#" else: prompt = "$" if verbose: print("\n--- Will connect to " + connection["host"] + " at " + connection["ip"] + " on port " + connection["port"] + " as " + connection["username"] + " who has user index " + str(connection["userNo"])) if show: print(" Use password '" + decryptPassword( f_key, connection["cryptPasswd"], verbose) + "'") outputList, errorList = paramikoRunCmd( f_key, connection["ip"], connection["port"], connection["username"], connection["cryptPasswd"], cmd, verbose, ) print("\nOutput from " + connection["username"] + "@" + connection["host"] + " " + prompt) for output in outputList: print(prompt + " " + output.strip("\n")) else: print("\nCan't make a connection to " + host)
def paramikoRunCmd(f_key, ip, port, username, cryptPasswd, cmd, verbose): import paramiko, base64 outputList = [] errorList = [] if verbose: print("\n--- Connecting with paramiko ...") rsaKeyFile = rsaPublicKey.strip(".pub") if verbose: print(" Using keyfile at " + rsaKeyFile) if verbose: print(" Generating host keys ...") host_key = paramiko.RSAKey.from_private_key_file(rsaKeyFile) public_host_key = paramiko.RSAKey(data=host_key.asbytes()) if verbose: print(" Setting up client ...") client = paramiko.SSHClient() # if verbose: # print(" Adding host keys ...") # client.get_host_keys().add(ip, "ssh-rsa", public_host_key) if verbose: print(" Setting missing host key policy ...") client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if verbose: print(" Loading known hosts from " + knownHostsFile + " ...") client.load_host_keys(knownHostsFile) if verbose: print(" Connecting ...") try: client.connect( ip, port=int(port), username=username, password=decryptPassword(f_key, cryptPasswd, verbose), ) except paramiko.ssh_exception.SSHException as e: onError(7, str(e)) stdout = "" stderr = "" else: if verbose: print(" Running command ...") stdin, stdout, stderr = client.exec_command(cmd) if verbose: print("\n--- Output:") for line in stdout: outputList.append(line) if verbose: print(" " + line.strip("\n")) if verbose: print("\n--- Error:") for line in stderr: errorList.append(line) if verbose: print(" " + line.strip("\n")) if verbose: print("\n--- Closing client ...") client.close() return outputList, errorList
def selectConnection(f_key, connectionFile, show, verbose): connectionNo = 0 userNo = 0 connectionList = [] userList = [] if verbose: print("\n--- Searching for sections in connections file") config = configparser.ConfigParser() config.read(connectionFile) # read config file sections = config.sections() print("\nSelect host\n----------") for section in sections: connectionNo += 1 hostname = config.get(section, "hostname") port = config.get(section, "port") print(" " + str(connectionNo) + ": " + section + ":" + port + ", " + hostname) connectionList.append({ u"number": connectionNo, u"ip": section, u"host": hostname, u"port": port }) print("\nEnter number:") while True: ipSelection = input("(1) ? ") if ipSelection: try: ipSelection = int(ipSelection) except: print("\nOnly integers allowed\nTry again:") else: if ipSelection <= 0 or ipSelection > connectionNo: print("Number must be 1-" + str(connectionNo)) else: break else: ipSelection = 1 break # print("\nSelect number 1-" + str(connectionNo) + "\nTry again") for connection in connectionList: if int(connection["number"]) == ipSelection: ip = connection["ip"] host = connection["host"] port = connection["port"] print("\nSelect user on " + ip + ", " + host + "\n----------") options = config.options(ip) userSet = False passwdSet = False for option in options: if option.startswith("username"): userNo += 1 user = config.get(ip, option) userSet = True elif option.startswith("password"): passwd = config.get(ip, option) passwdSet = True if userSet and passwdSet: print(" " + str(userNo) + ": " + user) userList.append({ u"number": userNo, u"user": user, u"passwd": passwd }) userSet = False passwdSet = False if userNo >= 1: print("\nEnter number:") while True: userSelection = input("(1) ? ") if userSelection: try: userSelection = int(userSelection) except: print("\nOnly integers allowed\nTry again:") else: if userSelection <= 0 or userSelection > userNo: print("Number must be 1-" + str(userNo)) else: break else: userSelection = 1 break # print("\nSelect number 1-" + str(userNo) + "\nTry again") for user in userList: if int(user["number"]) == userSelection: username = user["user"] cryptPasswd = user["passwd"] break else: print("\nSorry. No users added to this connection") userSelection = 0 username = "" cryptPasswd = "" if verbose: print("\n--- Selections:") print(" IP: " + ip) print(" Hostname: " + host) print(" Port: " + port) if userNo >= 1: print(" User number: " + str((userSelection - 1))) print(" Username: "******" Password: " + decryptPassword(f_key, cryptPasswd, verbose)) return ip, host, port, userSelection - 1, username, cryptPasswd
def writeConnections(f_key, connectionFile, ip, hostname, port, userList, cryptPasswdList, show, verbose): exUsers = 0 if verbose: print("\n--- Reading config file ...") config = configparser.ConfigParser() config.read(connectionFile) # read config file if verbose: print("Adding " + str(len(userList)) + " users") try: # add section [IP] config.add_section(ip) # raises exception if ip already is a section except configparser.DuplicateSectionError: print("\nIP " + ip + " already exist") try: # add host name oldHostname = config.get( ip, "hostname") # check if host name is present in section except: config[ip]["hostname"] = hostname # write host name to config else: if oldHostname == hostname: print("Host name is already set to " + oldHostname) else: # if new port differs from old print("Updating hostname from " + oldHostname + " with " + hostname) try: # add port number oldPort = config.get(ip, "port") # check if port is present in section except: config[ip]["port"] = str(port) # write port to config else: if oldPort == str(port): print("Port is already set to " + oldPort) else: # if new port differs from old print("Updating port from " + oldPort + " with " + str(port)) options = config.options(ip) # load all options in section for option in options: if option.startswith( "username" ): # if option is username add one to existing users counter exUsers += 1 if verbose: print("\n--- Users to add: " + str(len(userList))) print(" Existing users: " + str(exUsers)) else: if exUsers >= 1: print("\nThere was already " + str(exUsers) + " users for IP '" + ip + "'") delUserList = ( [] ) # will contain usernames that exist both in section and in the add-list if verbose: print("\n--- Checking for existing entries ...") for i in range( 0, len(userList)): # count up to number if users that is to be added for ii in range( 0, exUsers ): # count up to number of users already in this section oldUsername = config.get(ip, "username" + str(ii)) # username for 'username#' if verbose: print(" Checking new user: "******" with index: " + str(i) + ", against username" + str(ii) + ": " + oldUsername) if userList[i] == oldUsername: print("User " + userList[i] + " already exists") if verbose: print("--- Will not add new user with index: " + str(i)) delUserList.append(userList[i]) if len(delUserList) >= 1: if verbose: print("\n--- Not adding " + str(len(delUserList)) + " out of " + str(len(userList)) + " indexes:") for delUser in delUserList: delUserIndex = userList.index( delUser) # get the index-number for username in add-list if verbose: print(" Index: " + str(delUserIndex) + ", Username: "******" Users left to add: " + str(len(userList))) if verbose: print("\n--- Adding connection ...") print(" IP: " + ip) print(" Host name: " + hostname) config.set(ip, "hostname", hostname) if verbose: print(" Port: " + str(port)) config.set(ip, "port", str(port)) if len(userList) == 0: if verbose: print("\n--- No users was left to add") else: for i in range( 0 + exUsers, len(userList) + exUsers ): # start counting at number of existing users +1, count up to number of existing users + number of users to be added if verbose: print("\n User " + str(i + 1) + ": " + userList[i - exUsers]) config[ip]["username" + str(i)] = userList[i - exUsers] # passBytes = cryptPasswdList[i - exUsers] # passString = passBytes.decode() # config[ip]['password' + str(i)] = passString if verbose: if show: print(" Password " + str(i + 1) + ": " + decryptPassword( f_key, cryptPasswdList[i - exUsers], verbose)) else: print(" Password " + str(i + 1) + ": " + cryptPasswdList[i - exUsers]) config[ip]["password" + str(i)] = cryptPasswdList[i - exUsers] with open(connectionFile, "w") as configfile: config.write(configfile)
def createConnection(f_key, connectionFile, show, verbose): print("\nCreate new connection\n----------") defaultIP = get_ip() # this computers IP hostname = "" defaultPort = "22" defaultUser = getuser() # user running this script defaultPasswd = "xxxxx" newSection = False sectionNo = 1 if verbose: print("\n--- Reading config file ...") config = configparser.ConfigParser() config.read(connectionFile) # read config file oldSections = config.sections() print("\n1: Add new") for oldIP in oldSections: sectionNo += 1 oldPort = config.get(oldIP, "port") oldHostname = config.get(oldIP, "hostname") print(str(sectionNo) + ": " + oldIP + ", " + oldHostname) print("\nEnter number:") while True: selection = input("(1) ? ") if not selection: selection = 1 break else: try: selection = int(selection) except: print("\nOnly integers allowed\nTry again:") else: if selection <= 0 or selection > sectionNo: print("Number must be 1-" + str(sectionNo)) else: break if selection == 1: newSection = True else: ip = oldIP hostname = oldHostname port = oldPort print("\nHostname: " + hostname) print("Port: " + str(port)) while True: # section loop, run until all users are added if newSection: # if select new section, continue here while True: # ip loop, run until all users are added print("\nRemote IP") ip = input("[" + defaultIP + "] ? ") if not ip: # if no IP stated, accept default IP ip = defaultIP ipValid = False isURL = False if verbose: print("\n--- Checking if " + ip + " is a valid IPv4 ...") if not validators.ip_address.ipv4(ip): if verbose: print(" Not a valid IPv4") else: if verbose: print(" OK\n Is a valid IPv4") ipValid = True if not ipValid: if verbose: print("\n--- Checking if " + ip + " is a valid IPv6 ...") if not validators.ip_address.ipv6(ip): if verbose: print(" Not a valid IPv6") else: if verbose: print(" OK\n Is a valid IPv6") ipValid = True if not ipValid: if verbose: print("\n--- Checking if " + ip + " is a valid domain ...") if not validators.domain(ip): if verbose: print(" Not a domain") else: if verbose: print(" OK\n Is a valid domain") isURL = True ipValid = True if not ipValid: if verbose: print("\n--- Checking if " + ip + " is a valid URL ...") if not validators.url("http://" + ip): if verbose: print(" Not a URL") else: if verbose: print(" OK\n Is a valid URL") isURL = True ipValid = True if ipValid: if verbose: print("\n--- Checking if " + ip + " is already in sections ...") for existingIP in oldSections: if existingIP == ip: if verbose: print(" Found matching ip") else: print( "\n" + ip + " is already added\nUsing old hostname and port" ) oldPort = config.get(oldIP, "port") oldHostname = config.get(oldIP, "hostname") if verbose: print(" Using old hostname " + oldHostname + " and port " + oldPort) newSection = False break # break out of ip loop else: print( "\n" + ip + "is not\n a valid IPv4 address,\n a valid IPv6 address,\n a valid domain name,\n a valid URL" ) print("Try again") if verbose: print("\n--- Asking " + ip + " for hostname ...") if isURL: if verbose: print("\n--- Trying to resolve " + ip + " ...") try: domain, data, domainIP = socket.gethostbyname_ex(ip) except: print("\nCould not get hostname") else: probeIP = domainIP[0] if verbose: print(" OK\n Got IP " + probeIP) else: probeIP = ip try: hostname = socket.gethostbyaddr(probeIP)[ 0] # probe for hostname except: hostname = "" onError(4, "Could not get hostname") else: if verbose: print(" OK\n Got " + hostname) if newSection: while True: # hostname loop, input host name if hostname: # if hostname could be probed print("\nHost name") newHostname = input("[" + hostname + "] ? ") else: print("\nHost name") newHostname = input(" ? ") if (not newHostname and not hostname ): # if no hostname stated and no hostname could be probed print("\nYou must state a hostname\nTry again") elif (not newHostname and hostname ): # if no hostname stated but hostname was probed break else: hostname = newHostname break # break out of hostname loop if newSection: while True: # port loop, input port print("\nRemote port") port = input("[" + defaultPort + "] ? ") if not port: # if no port stated use default port port = defaultPort try: port = int( port) # raises an exception if port is not an integer except: print("\n" + str(port) + "is not an integer\nTry again") else: if verbose: print("\n--- Checking if port is valid ...") if port >= 0 and port <= 65535: # port must be between 0 and 65535 if verbose: print(" OK") break # break out of port loop else: print("\n" + str(port) + " is outside the range 0-65535\nTry again") userNo = 0 # stores number of users to be added userList = [] # stores user names to be added passwdList = [] # stores passwords to be added cryptPasswdList = [] # stores the encrypted passwords to be added while True: # user-pass loop, add users and passwords userNo += 1 # count up number of users to be added jumpToAdd = False while True: # username loop, input username print("\nUsername " + str(userNo)) username = input("(" + defaultUser + ")/q ? ") if not username: # if no username given accept the default one username = defaultUser elif username.lower() == "q": jumpToAdd = True break # break out of username loop else: defaultUser = username isNewUser = True if verbose: print("\n--- Checking if " + username + " is already in add-list ...") if (username in userList ): # if the username is already given in this session isNewUser = False print("\nUsername already in list\nTry again") else: if verbose: print(" OK\n User not in list") if verbose: print("\n--- Checking if " + username + " is already in connections ...") print(" Trying to read usernames from connections") try: options = config.options(ip) except: if verbose: print(" No section for ip " + ip) # break # this username not added in this session and ip not in connections else: for option in options: if option.startswith("username"): if config.get(ip, option) == username: isNewUser = False if verbose: print(" Username " + username + " already in connections") else: if verbose: print( " No matches found in connections" ) if isNewUser: if verbose: print(" Adding " + username + " to add-list") userList.append(username) # append username to list break # break out of while loop else: print("\nUsername already exists for this IP\nTry again") if jumpToAdd: break # break out of user-pass loop else: while True: # password loop, input password print("\nPassword " + str(userNo)) if show: passwd1 = input("[" + defaultPasswd + "] ? ") # enter password invisible else: passwd1 = getpass("[" + defaultPasswd + "] ? ") # enter password visibly if not passwd1: # if no password is given accept the default one passwd1 = defaultPasswd print("\nEnter password " + str(userNo) + " again") if show: passwd2 = input("[" + defaultPasswd + "] ? ") # enter password invisible else: passwd2 = getpass("[" + defaultPasswd + "] ? ") # enter password visibly if not passwd2: # if no password is given accept the default one passwd2 = defaultPasswd if (passwd1 == passwd2 ): # check if the same password was given both times passwdList.append(passwd1) # append password to list break # break out of password loop else: print("\nPasswords do not match\nTry again") print("\nDo you like to add another user") # add another user? addUser = input("(y/N) ? ") if addUser.lower() != "y": # if anything but 'y' was given break # break out of user-pass loop else: # reset variables username = "" passwd1 = "" passwd2 = "" if newSection or len(userList) >= 1: # encrypt passwords if verbose: print("\n--- Encrypting passwords ...") for i in range(0, len(userList)): cryptPasswdList.append( encryptPassword(f_key, passwdList[i], show, verbose)) # cryptPasswdList.append(f_key.encrypt(passwdList[i].encode())) # encrypt password and append to encrypted password as bytes # display all values and ask if correct print("\nNew connection:\n----------") print("IP: " + ip) print("Host name: " + hostname) print("Port: " + str(port)) for i in range(0, len(userList)): if show: print("\nUser " + str(i + 1) + ": " + userList[i]) print("Pass " + str(i + 1) + ": " + passwdList[i]) else: print("\nUser " + str(i + 1) + ": " + userList[i]) print("Pass " + str(i + 1) + ": " + cryptPasswdList[i]) print("\nIs this correct") correct = input("(Y/n/q) ? ") if correct.lower() == "q": # if 'q' then exit print("\nExiting ...") sys.exit(0) elif correct.lower() == "n": # if anything but 'n' was stated break # break out of loop else: for i in range(0, len(userList)): # encrypt passwords cryptPasswd = cryptPasswdList[i] # encrypted password if verbose: print("\n--- Encrypted password " + str(i + 1) + ": " + cryptPasswd) if show: print("\n--- Plain text password " + str(i + 1) + ": " + decryptPassword(f_key, cryptPasswd, verbose)) print("\nAdding new connection ...") writeConnections( f_key, connectionFile, ip, hostname, port, userList, cryptPasswdList, show, verbose, ) break else: print("\nNothing to add\n\nNo changes made") break # break out of section loop