def ssh_login(user_obj, bind_host_obj, mysql_engine, log_recording): try: client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.WarningPolicy()) print('***Connecting***') client.connect(bind_host_obj.host.ip_addr, bind_host_obj.host.port, bind_host_obj.remoteuser.username, bind_host_obj.remoteuser.password, timeout=30) cmd_caches = [] chan = client.invoke_shell() print(repr(client.get_transport())) cmd_caches.append( models.AuditLog(user_id=user_obj.id, bind_host_id=bind_host_obj.id, action_type='login', date=datetime.datetime.now())) log_recording(user_obj, bind_host_obj, cmd_caches) interactive.interactive_shell(chan, user_obj, bind_host_obj, cmd_caches, log_recording) chan.close() client.close() except Exception as e: print('Caught Exception:%s:%s' % (e.__class__, e)) traceback.print_exc() try: client.close() except: pass sys.exit(1)
def posix_shell(chan,user_obj,bind_host_obj,cmd_caches,log_recording): import select oldtty = termios.tcgetattr(sys.stdin) try: tty.setraw(sys.stdin.fileno()) tty.setcbreak(sys.stdin.fileno()) chan.settimeout(0.0) cmd = '' tab_key = False while True: r, w, e = select.select([chan, sys.stdin], [], []) if chan in r: try: x = u(chan.recv(1024)) if tab_key: if x not in ('\x07' , '\r\n'): #print('tab:',x) cmd += x tab_key = False if len(x) == 0: sys.stdout.write('\r\n*** EOF\r\n') break sys.stdout.write(x) sys.stdout.flush() except socket.timeout: pass if sys.stdin in r: x = sys.stdin.read(1) if '\r' != x: cmd +=x else: # print('cmd->:',cmd) log_item = models.AuditLog(user_profile_id=user_obj.id, bind_host_id=bind_host_obj.id, action_type='cmd', cmd=cmd , op_date=datetime.datetime.now() ) cmd_caches.append(log_item) cmd = '' log_recording(user_obj, bind_host_obj, cmd_caches) # if len(cmd_caches)>=10: # log_recording(user_obj, bind_host_obj, cmd_caches) # cmd_caches = [] if '\t' == x: tab_key = True if len(x) == 0: break chan.send(x) finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
def ssh_login(user_obj, bind_host_obj, mysql_engine, log_recording): # now, connect and use paramiko Client to negotiate SSH2 across the connection try: #尝试连接 client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.WarningPolicy()) print('*** Connecting...') #client.connect(hostname, port, username, password) client.connect( bind_host_obj.host.ip_addr, # 通过bind_host_obj获取到ip bind_host_obj.host.port, # 通过bind_host_obj获取到 port bind_host_obj.remoteuser.username, # 通过bind_host_obj获取到 用户名 bind_host_obj.remoteuser.password, # 通过bind_host_obj获取到 密码 timeout=30) cmd_caches = [] chan = client.invoke_shell() #调用shell print(repr(client.get_transport())) print('*** Here we go!\n') cmd_caches.append( models.AuditLog( user_id=user_obj.id, bind_host_id=bind_host_obj.id, action_type='login', date=datetime.datetime.now())) #一行一条命令,以用户名,绑定主机id 时间 log_recording(user_obj, bind_host_obj, cmd_caches) #将命令写入到数据库下. interactive.interactive_shell(chan, user_obj, bind_host_obj, cmd_caches, log_recording) chan.close() client.close() except Exception as e: print('*** Caught exception: %s: %s' % (e.__class__, e)) traceback.print_exc() try: client.close() except: pass sys.exit(1)