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

# Copyright (C) 2003-2007  Robey Pointer <[email protected]>
#
# 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
时间: 2024-08-01 14:23:20

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

练习:编写循环,要求用户输入命令,如果命令为空,则继续输入

编写循环,要求用户输入命令,如果命令为空,则继续输入 while True: name = input('请输入:') if name.isspace(): # 如果用户输入为空,则继续,否则退出程序 continue else: break

终端直接执行py文件,不需要python命令

然后给脚本文件运行权限,方法(1)chmod +x ./*.py方法(2)chmod 755 ./*.py (777也无所谓啦) 这个命令不去调整,会出现permission denied的错误终端直接执行. 如果在脚本内容的开头已经给出了类似于如下的注释:#!/usr/bin/env python(或者是 #!/usr/bin/python)#!/usr/bin/python3那就可以直接在终端里运行:./*.py如果没有这个注释就在终端中执行:python ./*.py  (注意:有些lin

WebClient实现下载txt文件并与用户输入进行匹配

/// <summary> /// 验证 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { //WebClient client = new WebClient(); //byt

将.py文件转化成.exe

机子上已经安装好python,且配置好环境变量 编写好xx.py文件 安装pywin32.此处一定注意pywin32有32位和64位之分.可以在命令提示符里输入python来查看python的版本以及是否为32位还是64位(我的python是2.7.9 32位) 安装pyinstaller,只要将pyinstaller解压到任意位置,然后在命令提示符里转到pyinstaller的setup.py位置,输入setup.py  install安装pyinstaller 在命令提示符里转到已经编写好的

read命令读取用户输入

read命令用于从终端或文件中读取用户输入,它读取整行输入,如果没有指定名称,读取的行被赋值给内部变量REPLY.read命令常用选项:-a,-p,-s,-t,-n 1.REPLY变量 $readhello$echo $REPLYhello 2.读入用户指定的变量 $read answerhello$echo $answerhello $read first second thirdchen xiaopang panda$echo $first $second $thirdchen xiaopa

Django 部署一个简单的用户后台

第一步编写models.py,下面是通过在myauth.py 文件models.py 的用户内容,再在models.py中引入这个文件 models.py内容如下: from __future__ import unicode_literals from django.db import models import myauth # Create your models here. myauth.py内容 #!/usr/bin/python # -*- coding: utf-8 -*- __a

Python脚本实现基于文件存储的用户登录程序

1 #!/usr/bin/env python 2 # coding:utf-8 3 4 """ 5 aim: 基于文件存储的用户登录程序(3次登录失败,锁定用户) 6 7 need: 8 a. 用户信息文件 9 b. 用户输入 10 11 logical: 12 a. 校验用户名合法情况 13 b. 校验用户锁定情况 14 c. 校验密码 15 d. 更新登录失败次数 16 e. 写入文件 17 18 sum: 19 a. 更新用户锁定次数不要直接操作文件,可先写入内存,程

.py文件转.exe文件 (转载并减掉很多)

首先下载pyinstaller 下载地址:http://www.pyinstaller.org/downloads.html 我的是windows操作系统,所以我选择了支持3.6版本的zip格式文件 下载解压到一个盘里 然后进入CMD,进入刚刚解压的文件夹里,然后输入python setup.py install 安装成功后,把想转换的.py文件放入到刚刚解压的文件夹里,就是pyinstaller-pyinstaller-xefjwioef 之类的文件夹,(不好意思我不够严谨) (x = pyi

python之模块py_compile用法(将py文件转换为pyc文件)

# -*- coding: cp936 -*- #python 27 #xiaodeng #python之模块py_compile用法(将py文件转换为pyc文件) #二进制文件,是由py文件经过编译后,生成的文件. ''' import py_compile #不带转义r py_compile.compile('D:\test.py') Traceback (most recent call last): File "<pyshell#1>", line 1, in &l