def run(self): ''' 类的入口方法 :return: ''' print('Connect remote host [%s] as user [%s]...' %(self.ip, self.username)) client = self.get_ssh(self.ip, self.port, self.username, self.password) # 获取ssh对象 if client: chan = client.invoke_shell() print("Connect success let's go [%s]" %self.user.username) auditlog.insert_log(self.user, self.hostuser, u'login', 'login') # 记录登录日志 interactive.interactive_shell(self.user, self.hostuser, chan, client) auditlog.insert_log(self.user, self.hostuser, u'logout', 'logout') # 记录退出日志 chan.close() # 关闭shell client.close() # 关闭ssh通道 return True else: mylib.print_err(conf.ERRORNO['5001']) return False
def posix_shell(user, hostuser, chan, ssh): # """ # Use paramiko channel connect server interactive. # 使用paramiko模块的channel,连接后端,进入交互式 # """ global vim_data global vim_flag global cmd import time import os import sys import fcntl data = '' is_tab = False input_mode = False oldtty = termios.tcgetattr(sys.stdin) try: tty.setraw(sys.stdin.fileno()) tty.setcbreak(sys.stdin.fileno()) chan.settimeout(0.0) while True: try: r, w, e = select.select([chan, sys.stdin], [], []) # flag = fcntl.fcntl(sys.stdin, fcntl.F_GETFL, 0) # fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flag|os.O_NONBLOCK) except Exception: pass if chan in r: try: x = chan.recv(10240) if len(x) == 0: sys.stdout.write('\r\n*** EOF\r\n') break if vim_flag: vim_data += str(x, 'utf8') index = 0 len_x = len(x) while index < len_x: try: n = os.write(sys.stdout.fileno(), x[index:]) sys.stdout.flush() index += n except OSError as msg: print(msg) if input_mode and not is_output(x): data += str(x, 'utf8') except socket.timeout: pass if sys.stdin in r: try: x = os.read(sys.stdin.fileno(), 4096) except OSError: pass input_mode = True #print(type(x)) #print(str(x, 'utf8')) if str(x, 'utf8') in ['\r', '\n', '\r\n']: if vim_flag: match = ps1_pattern.search(vim_data) if match: vim_flag = False data = deal_command(data)[0:200] if len(data) > 0: pass else: data = deal_command(data)[0:200] # print(data) # print(type(data)) if len(data) > 0: auditlog.insert_log(user, hostuser, u'cmd', data) pass data = '' vim_data = '' input_mode = False if str(x, 'utf8') == '\t': pass if len(x) == 0: break chan.send(x) except Exception as e: print(e) finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
def posix_shell(user, hostuser, chan,ssh): # """ # Use paramiko channel connect server interactive. # 使用paramiko模块的channel,连接后端,进入交互式 # """ global vim_data global vim_flag global cmd import time import os import sys import fcntl data = '' is_tab = False input_mode = False oldtty = termios.tcgetattr(sys.stdin) try: tty.setraw(sys.stdin.fileno()) tty.setcbreak(sys.stdin.fileno()) chan.settimeout(0.0) while True: try: r, w, e = select.select([chan, sys.stdin], [], []) # flag = fcntl.fcntl(sys.stdin, fcntl.F_GETFL, 0) # fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flag|os.O_NONBLOCK) except Exception: pass if chan in r: try: x = chan.recv(10240) if len(x) == 0: sys.stdout.write('\r\n*** EOF\r\n') break if vim_flag: vim_data += str(x, 'utf8') index = 0 len_x = len(x) while index < len_x: try: n = os.write(sys.stdout.fileno(), x[index:]) sys.stdout.flush() index += n except OSError as msg: print(msg) if input_mode and not is_output(x): data += str(x, 'utf8') except socket.timeout: pass if sys.stdin in r: try: x = os.read(sys.stdin.fileno(), 4096) except OSError: pass input_mode = True #print(type(x)) #print(str(x, 'utf8')) if str(x, 'utf8') in ['\r', '\n', '\r\n']: if vim_flag: match = ps1_pattern.search(vim_data) if match: vim_flag = False data = deal_command(data)[0:200] if len(data) > 0: pass else: data = deal_command(data)[0:200] # print(data) # print(type(data)) if len(data) > 0: auditlog.insert_log(user, hostuser, u'cmd', data) pass data = '' vim_data = '' input_mode = False if str(x, 'utf8') == '\t': pass if len(x) == 0: break chan.send(x) except Exception as e: print(e) finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)