paramiko模块

模拟ssh远程登录的功能
1
#!/usr/bin/env python 2 #coding:utf-8 3 4 import paramiko 5 6 ssh = paramiko.SSHClient() 7 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 8 ssh.connect('192.168.30.241',22,'root','123456') 9 stdin,stdout,stderr=ssh.exec_command('ls') 10 print stdout.read() 11 ssh.close() 12 13 #用户名和密码连接

在paramiko-master源码包中修改文档,使其可以记录用户登录的输入记录。

  1 # Copyright (C) 2003-2007  Robey Pointer <robeypointer@gmail.com>
  2 #
  3 # This file is part of paramiko.
  4 #
  5 # Paramiko is free software; you can redistribute it and/or modify it under the
  6 # terms of the GNU Lesser General Public License as published by the Free
  7 # Software Foundation; either version 2.1 of the License, or (at your option)
  8 # any later version.
  9 #
 10 # Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
 11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 12 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 13 # details.
 14 #
 15 # You should have received a copy of the GNU Lesser General Public License
 16 # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
 17 # 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 18 
 19 import time#自己加的
 20 import socket
 21 import sys
 22 from paramiko.py3compat import u
 23 
 24 # windows does not have termios...
 25 try:
 26     import termios
 27     import tty
 28     has_termios = True
 29 except ImportError:
 30     has_termios = False
 31 
 32 
 33 def interactive_shell(chan):
 34     if has_termios:
 35         posix_shell(chan)
 36     else:
 37         windows_shell(chan)
 38 
 39 
 40 def posix_shell(chan):
 41     import select
 42     
 43     oldtty = termios.tcgetattr(sys.stdin)
 44     f = open('record_txt', 'ab+')  # 自己加的
 45     try:
 46         tty.setraw(sys.stdin.fileno())
 47         tty.setcbreak(sys.stdin.fileno())
 48         chan.settimeout(0.0)
 49         records=[]#自己加的
 50 
 51         while True:#一次只接收一个数据
 52             r, w, e = select.select([chan, sys.stdin], [], [])
 53             if chan in r:
 54                 try:
 55                     x = u(chan.recv(1024))
 56                     if len(x) == 0:
 57                         sys.stdout.write('\r\n*** EOF\r\n')
 58                         break
 59                     sys.stdout.write(x)#结果的输出
 60                     sys.stdout.flush()#屏幕刷新
 61                 except socket.timeout:
 62                     pass
 63             if sys.stdin in r:#接收输入
 64                 x = sys.stdin.read(1)#每次只接收一个字符
 65                 records.append(x)#自己加的
 66                 if x=='\r':#自己加的
 67                     c_time=time.strftime('%Y-%m-%d %H:%M:%S')#自己加的
 68                     cmd=''.join(records).replace('\r','\n')#自己加的
 69                     log='%s %s'%(c_time,cmd)#自己加的
 70                     f.write(log)#自己加的
 71                     records=[]#自己加的
 72                 if len(x) == 0:
 73                     break
 74                 chan.send(x)
 75 
 76     finally:
 77         termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
 78         f.close()#自己加的
 79 
 80     
 81 # thanks to Mike Looijmans for this code
 82 def windows_shell(chan):
 83     import threading
 84 
 85     sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n")
 86         
 87     def writeall(sock):
 88         while True:
 89             data = sock.recv(256)
 90             if not data:
 91                 sys.stdout.write('\r\n*** EOF ***\r\n\r\n')
 92                 sys.stdout.flush()
 93                 break
 94             sys.stdout.write(data)
 95             sys.stdout.flush()
 96         
 97     writer = threading.Thread(target=writeall, args=(chan,))
 98     writer.start()
 99         
100     try:
101         while True:
102             d = sys.stdin.read(1)
103             if not d:
104                 break
105             chan.send(d)
106     except EOFError:
107         # user hit ^Z or F6
108         pass

 

posted @ 2017-05-30 09:20  腐汝  阅读(271)  评论(0编辑  收藏  举报