def check_user_password(request): """ 判断远程登陆用户名和密码是否准确 """ if request.method == "POST": try: data = dict() post_data = json.loads(str(request.body, encoding='utf-8')) info = { "hostname": post_data["hostname"], "username": post_data["username"], "password": post_data["password"], "port": post_data["port"] } ssh = SSH(**info) ssh.get_client() data["status"] = "success" data['result'] = info except paramiko.ssh_exception.NoValidConnectionsError: data["status"] = "fail" data["result"] = u"端口未开放或路由不可达,请重试或关闭!" except paramiko.ssh_exception.AuthenticationException: data["status"] = "fail" data["result"] = u"用户名或密码不准确,请重试或关闭!" except socket.error as err: data["status"] = "fail" data["result"] = u"连接超时,请重试或关闭!" return json_response(data)
def _get_ssh(kwargs, pkey=None, private_key=None, public_key=None, password=None): try: ssh = SSH(pkey=pkey or private_key, **kwargs) ssh.get_client() return ssh except AuthenticationException as e: if password: with SSH(password=str(password), **kwargs) as ssh: ssh.add_public_key(public_key) return _get_ssh(kwargs, private_key) raise e