paramiko intertive.py文件简单修改记录用户输入命令 v1
2016-09-15 09:40
232 查看
# Copyright (C) 2003-2007 Robey 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, MA 02111-1307 USA. 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([chan, sys.stdin], [], []) 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
相关文章推荐
- linux修改目录和文件权限的简单命令解释
- PHP运行SVN命令显示某用户的文件更新记录的代码
- GIT 版本控制软件的常用命令记录每次更新到仓库 现在我们手上已经有了一个真实项目的 Git 仓库,并从这个仓库中取出了所有文件的工作拷贝。接下来,对这些文件作些修改,在完成了一个阶段的目标和使用方法
- 第4章 处理用户输入与显示数据------------(禁止命令输出、lsof命令、关闭文件描述符、创建读取/写入文件描述符)
- 第4章 处理用户输入与显示数据------------(创建本地临时文件、在/temp中创建临时文件、创建临时目录、tee命令)
- linux下根据用户输入批量修改配置文件ip
- 解决useradd 用户后没有添加用户Home目录的情况,Linux改变文件或目录的访问权限命令,linux修改用户密码
- 关于普通用户输入命令提示command not found 及字符集修改
- 一个简单的利用ini配置文件以及用户输入的交互进行文件夹内容拷贝的批处理文件脚本的实现
- Linux文件权限修改命令,添加,删除用户等
- linux修改目录和文件权限的简单命令解释
- Linux 用户(user)和用户组(group)概念以及修改linux文件权限命令chmod
- Linux下用户、组的命令及修改文件实现用户和组的添加
- Windows 控制台命令 编写简单的bat文件(从控制台获取输入的变量)
- [添加用户]解决useradd 用户后没有添加用户Home目录的情况,Linux改变文件或目录的访问权限命令,linux修改用户密码,usermod的ysuum安装包。飞
- 第1章 BashShell命令------------(创建新用户组、修改用户组、 /etc/group文件)
- Linux下自动修改用户密码的方法(直接通过命令而不是在终端输入密码)
- Linux七天系列(第一天:文件操作、用户权限 、软链接与硬链接 、文件压缩与解压、通信命令、命令链接符 、输入/输出重定向)
- 第4章 处理用户输入与显示数据------------(禁止命令输出、lsof命令、关闭文件描述符、创建读取/写入文件描述符)
- 第4章 处理用户输入与显示数据------------(禁止命令输出、lsof命令、关闭文件描述符、创建读取/写入文件描述符)