def connect(self) -> pref.Error: ''' Connects its self to the computer based on the computer object passed in init. The computer with its IP and username. Before a computer can be connected to for the first time addComputer must first be run on it. @returns the error code pref.Success if it worked ''' err = pref.Success for _ in range(1): #make sure ip isnt Blacklisted blackListFileName = pref.getNoCheck(pref.CONFIG_BLACKLIST_IP_FILE) if (blackListFileName != ""): blackList = None try: blackList = [ line.rstrip("\n") for line in open(blackListFileName) ] except: tempErr = pref.getError(pref.ERROR_FILE_NOT_FOUND, args=(blackListFileName)) logger.error(tempErr) break err = whitelistBlackList.confirmValidIP( self.computer.IP, blackList) else: logger.warning("no ip blacklist no filtering") #Create ssh client with basic autoadd settings self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #Get Keyfile from preferences try: keyFileName = pref.getNoCheck(pref.CONFIG_PRIVATE_SSH_KEY) keyFile = paramiko.RSAKey.from_private_key_file(keyFileName) except: err = pref.getError(pref.ERROR_CANT_FIND_SSH_KEY, args=(keyFileName)) logger.error(err) break #connect to the computer #TODO test self.computer.IP, needs list of blacklisted commands try: self.ssh.connect(self.computer.IP, username=self.computer.username, pkey=keyFile, timeout=5, allow_agent=False) #Authentication error rerun addComputer on this computer to fix except paramiko.ssh_exception.AuthenticationException as e: err = pref.getError(pref.ERROR_SSH_AUTHENTICATION_FAILED, args=(self.computer.username, self.computer.IP)) logger.error(err) break #Failed to connect except: err = pref.getError(pref.ERROR_CONNECTION_FAILED, args=(self.computer.username, self.computer.IP)) logger.error(err) return err
def testBlackListedIP(self): holder = blacklist.confirmValidIP("192.168.1.0", ["192.168.1.0", "192.989.1.1"]) self.assertEqual(holder, pref.getError(pref.ERROR_BLACKLISTED_IP))
def addNewComputer(computerIP: str, username: str, password: str) -> pref.Error: ''' adds a new computer with using its username and password @param computerIP:str ip of the computer to add as a string. @param username:str username of the computer not user adding the computer. @param password:str password of the user described above. @return err ''' #init values err = pref.Success ssh = None ftp_client = None remoteFolder = None stdin = None stdout = None stderr = None for _ in range(1): #make sure ip isnt Blacklisted blackListFileName = pref.getNoCheck(pref.CONFIG_BLACKLIST_IP_FILE) if (blackListFileName != ""): blackList = None try: blackList = [ line.rstrip("\n") for line in open(blackListFileName) ] except: tempErr = pref.getError(pref.ERROR_FILE_NOT_FOUND, args=(blackListFileName)) logger.error(tempErr) break err = whitelistBlackList.confirmValidIP(computerIP, blackList) if (err != pref.Success): logger.error(err) break else: logger.warning("no ip blacklist no filtering") #get shh public key sshKey = pref.getNoCheck(pref.CONFIG_PUBLIC_SSH_KEY) #if its empty row public sshkey empty if (sshKey == None or sshKey.strip().strip("\n") == ""): err = pref.getError(pref.ERROR_EMPTY_SSH_PUBLIC_KEY) logger.error(err) break #Create ssh client with basic autoadd settings ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #Connects to computer with password and username try: ssh.connect(computerIP, username=username, password=password, timeout=5, look_for_keys=False, allow_agent=False) ftp_client = ssh.open_sftp() #Incorrect username or password except paramiko.ssh_exception.AuthenticationException as e: err = pref.getError(pref.ERROR_SSH_AUTHENTICATION_FAILED, args=(username, computerIP)) logger.error(err) break #Connection timed out or port rejected. except: err = pref.getError(pref.ERROR_CONNECTION_FAILED, args=(username, computerIP)) logger.error(err) break #Create tempfolder on the remote computer. remoteFolder = pref.getNoCheck(pref.CONFIG_REMOTE_FOLDER) addSSHKeyScript = pref.getNoCheck(pref.CONFIG_ADD_COMPUTER_SCRIPT) resFolder = pref.getNoCheck(pref.CONFIG_RES_FOLDER) #Add ssh file to server and change permissions. err = copyFileToComputer(ftp_client, remoteFolder, resFolder, addSSHKeyScript) if (err != pref.Success): break #Run setup script with sshKey config file. try: stdin, stdout, stderr = ssh.exec_command( "sshkey=\"{}\" {}{} > /dev/null; echo $?".format( sshKey, remoteFolder, addSSHKeyScript)) except: #failed to execute script. err = pref.getError(pref.ERROR_SSH_FAILED_TO_EXECUTE_SCRIPT, args=(addSSHKeyScript)) logger.error(err) break #Check error code of script. errCode = "".join(stdout.readlines()).rstrip("\n") if (errCode != "0"): err = pref.getError( pref.ERROR_SSH_SCRIPT_FAILED_WITH_ERROR_CODE, args=(errCode)) logger.error(err) return err
def testValidIP(self): holder = blacklist.confirmValidIP("192.168.1.0", ["192.545.0.1", "192.989.1.1"]) self.assertEqual(holder, pref.Success)