def connect(self, host, user, password=None, ssh_key=None, port=22, timeout=30, term='ansi', pty_width=80, pty_height=24): try: ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if ssh_key: key = get_key_obj(paramiko.RSAKey, pkey_obj=ssh_key, password=password) or \ get_key_obj(paramiko.DSSKey, pkey_obj=ssh_key, password=password) or \ get_key_obj(paramiko.ECDSAKey, pkey_obj=ssh_key, password=password) or \ get_key_obj(paramiko.Ed25519Key, pkey_obj=ssh_key, password=password) ssh_client.connect(username=user, hostname=host, port=port, pkey=key, timeout=timeout) else: ssh_client.connect(username=user, password=password, hostname=host, port=port, timeout=timeout) transport = ssh_client.get_transport() self.channel = transport.open_session() self.channel.get_pty(term=term, width=pty_width, height=pty_height) self.channel.invoke_shell() for i in range(2): recv = self.channel.recv(1024).decode('utf-8') self.message['status'] = 0 self.message['message'] = recv message = json.dumps(self.message) self.websocker.send(message) self.res += recv # 创建3个线程将服务器返回的数据发送到django websocket(1个线程都可以) Thread(target=self.websocket_to_django).start() Thread(target=self.websocket_to_django).start() Thread(target=self.websocket_to_django).start() except: self.message['status'] = 2 self.message['message'] = 'connection faild...' message = json.dumps(self.message) self.websocker.send(message) self.websocker.close(3001)
def connect(self, host, user, password=None, ssh_key=None, port=22, timeout=30, term='xterm', pty_width=80, pty_height=24): try: ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if ssh_key: key = get_key_obj(paramiko.RSAKey, pkey_obj=ssh_key, password=password) or \ get_key_obj(paramiko.DSSKey, pkey_obj=ssh_key, password=password) or \ get_key_obj(paramiko.ECDSAKey, pkey_obj=ssh_key, password=password) or \ get_key_obj(paramiko.Ed25519Key, pkey_obj=ssh_key, password=password) ssh_client.connect(username=user, hostname=host, port=port, pkey=key, timeout=timeout) else: ssh_client.connect(username=user, password=password, hostname=host, port=port, timeout=timeout) transport = ssh_client.get_transport() self.channel = transport.open_session() self.channel.get_pty(term=term, width=pty_width, height=pty_height) self.channel.invoke_shell() for i in range(2): recv = self.channel.recv(1024).decode('utf-8') self.message['status'] = 0 self.message['message'] = recv message = json.dumps(self.message) self.websocker.send(message) except socket.timeout: self.message['status'] = 1 self.message['message'] = 'ssh 连接超时' message = json.dumps(self.message) self.websocker.send(message) self.close() except: self.close()
def connect(self, host, user, password=None, ssh_key=None, port=22, timeout=None, term='ansi', pty_width=80, pty_height=24): try: # 实例化SSHClient ssh_client = paramiko.SSHClient() # 当远程服务器没有本地主机的密钥时自动添加到本地,这样不用在建立连接的时候输入yes或no进行确认 ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 用key进行认证 if ssh_key: key = get_key_obj(paramiko.RSAKey, pkey_obj=ssh_key, password=password) or \ get_key_obj(paramiko.DSSKey, pkey_obj=ssh_key, password=password) or \ get_key_obj(paramiko.ECDSAKey, pkey_obj=ssh_key, password=password) or \ get_key_obj(paramiko.Ed25519Key, pkey_obj=ssh_key, password=password) ssh_client.connect(username=user, hostname=host, port=port, pkey=key, timeout=timeout) else: # 用账号密码的方式进行认证 ssh_client.connect(username=user, password=password, hostname=host, port=port, timeout=timeout) # 打开ssh通道,建立长连接 transport = ssh_client.get_transport() self.channel = transport.open_session() # 获取ssh通道,并设置term和终端大小 self.channel.get_pty(term=term, width=pty_width, height=pty_height) # 激活终端,正常登陆 self.channel.invoke_shell() # 一开始展示Linux欢迎相关内容,后面不进入此方法 for i in range(2): recv = self.channel.recv(1024).decode('utf-8') self.message['status'] = 0 self.message['message'] = recv message = json.dumps(self.message) self.websocker.send(message) self.res += recv # 创建3个线程将服务器返回的数据发送到django websocket(1个线程都可以) Thread(target=self.websocket_to_django).start() # Thread(target=self.websocket_to_django).start() # Thread(target=self.websocket_to_django).start() except: self.message['status'] = 2 self.message['message'] = 'connection faild...' message = json.dumps(self.message) self.websocker.send(message) self.websocker.close(3001)