def security_root(self, random_password=False, root_key=False, security_login=None): result = {'random_password': False, 'root_key': False, 'security_login': False} password = get_random_passwd(config.PASSWORD_LENGTH) if random_password and self._set_password('root', password): result['random_password'] = True if security_login: cmd = '''sed -i 's/#PermitEmptyPasswords/PermitEmptyPasswords/g;s/#PermitRootLogin yes/PermitRootLogin no/g' {config}'''.format(config=config.SSHD_CONFIG) cmd += ''' && sed -i '/Match/d;$d' {config}'''.format(config=config.SSHD_CONFIG) cmd += ''' && sed -i '$a Match Address {addr}\\n PermitRootLogin yes' {config}'''.format(addr=','.join(security_login), config=config.SSHD_CONFIG) cmd += ''' && service sshd restart''' result['security_login'] = self._execute(cmd) if root_key: cmd = '''mkdir -p {root_ssh_path}'''.format(root_ssh_path=Root.get_ssh_path()) cmd += ''' && echo {content} > {root_authorized_keys}'''.format( content=Root.get_key(), root_authorized_keys=Root.get_authorized_keys_path() ) cmd += ''' && chmod 600 {root_authorized_keys}'''.format(root_authorized_keys=Root.get_authorized_keys_path()) result['root_key'] = self._execute(cmd) return result
def get_password(self, length=config.PASSWORD_LENGTH): return utils.get_random_passwd(length)