4321oiuy 发表于 2016-9-18 11:12:32

paramiko intertive.py文件简单修改记录用户输入命令 v1



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright (C) 2003-2007Robey Pointer <robeypointer@gmail.com>
#
# This file is part of paramiko.
#
# Paramiko is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA02111-1307USA.


import socket
import sys
from paramiko.py3compat import u
import time

# windows does not have termios...
try:
    import termios
    import tty
    has_termios = True
except ImportError:
    has_termios = False


def interactive_shell(chan):
    if has_termios:
      posix_shell(chan)
    else:
      windows_shell(chan)


def posix_shell(chan):
    import select

    oldtty = termios.tcgetattr(sys.stdin)
    try:
      tty.setraw(sys.stdin.fileno())
      tty.setcbreak(sys.stdin.fileno())
      chan.settimeout(0.0)
      recode = []
      cmd = ''
      log_file = "audit_log_%s.log" % time.strftime('%Y_%m_%d')
      f = file(log_file,'ab+')

      while True:
            r, w, e = select.select(, [], [])
            if chan in r:
                try:
                  x = u(chan.recv(1024))
                  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)
                cmd += x
                if x == '\r':
                  #print "cmd===>",cmd
                  cmd_time = time.strftime('%Y-%m-%d-%H-%M-%S')
                  log_line = "username:192.169.10.12:root:%s       %s \n   " %(cmd_time,cmd)
                  f.write(log_line)
          print log_line
                  cmd = ''
                if len(x) == 0:
                  break
                chan.send(x)
      else:
            f.close()

    finally:
      termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)


# thanks to Mike Looijmans for this code
def windows_shell(chan):
    import threading

    sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n")

    def writeall(sock):
      while True:
            data = sock.recv(256)
            if not data:
                sys.stdout.write('\r\n*** EOF ***\r\n\r\n')
                sys.stdout.flush()
                break
            sys.stdout.write(data)
            sys.stdout.flush()

    writer = threading.Thread(target=writeall, args=(chan,))
    writer.start()

    try:
      while True:
            d = sys.stdin.read(1)
            if not d:
                break
            chan.send(d)
    except EOFError:
      # user hit ^Z or F6
      pass







页: [1]
查看完整版本: paramiko intertive.py文件简单修改记录用户输入命令 v1