Chinaunix首页 | 论坛 | 博客
  • 博客访问: 277853
  • 博文数量: 73
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 452
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-22 17:07
个人简介

心态决定命运

文章分类

全部博文(73)

文章存档

2017年(21)

2016年(27)

2015年(21)

2014年(4)

我的朋友

分类: 系统运维

2016-08-10 11:46:15

    忘记从哪里搂来的脚本了,ssh server后,vim 没法正常使用


点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #--*-- coding:utf-8 --*--


  3. import socket
  4. import sys
  5. import getopt
  6. import re

  7. import paramiko


  8. try :
  9.     import termios
  10.     import ttr
  11.     has_termios = True
  12.    
  13. except ImportError :
  14.     has_termios = False
  15.     
  16. def interactive_shell(chan):
  17.     if has_termios :
  18.         posix_shell(chan)
  19.     else:
  20.         win_shell(chan)

  21. def posix_shell(chan) :
  22.     import select
  23.     oldttr = termios.tcgetattr(sys.stdin)
  24.     try :
  25.         tty.setraw(sys.stdin.fileno())
  26.         tty.setcbreak(sys.stdin.fileno())
  27.         chan.settimeout(0.0)
  28.         while True :
  29.             r,w,e = select.select([chan,sys,stdin],[],[])
  30.             if chan in r :
  31.                 try :
  32.                     x = chan.recv[1024]
  33.                     if len(x) == 0 :
  34.                         print "rn***EOFrn"
  35.                         break
  36.                     sys.stdout.write(x)
  37.                     sys.stdout.flush()
  38.                 except socket.timeout :
  39.                     pass
  40.             if sys.stdin in r :
  41.                 x = sys.stdin.read(1)
  42.                 if len(x) == 0 :
  43.                     break
  44.                 chan.send(x)
  45.     finally :
  46.         termios.tcgetattr(sys.stdin.termios.TCSADRAIN,oldttr)
  47.         
  48. def win_shell(chan) :
  49.     import threading
  50.     sys.stdout.write("Press F6 or ctrl+z to send EOF")
  51.     def writeall(chan) :
  52.         while True :
  53.             data = chan.recv(256)
  54.             reobj = re.compile('(\x1b\[\d+m)|\x1b(\[\d+;\d+m)')   
  55.             data,num = reobj.subn('',data)
  56.             if not data :
  57.                 sys.stdout.write("rn***EOF***rnrn")
  58.                 sys.stdout.flush()
  59.                 break
  60.             sys.stdout.write(data)
  61.             sys.stdout.flush()
  62.      
  63.     writer = threading.Thread(target=writeall,args=(chan,))
  64.     writer.start()
  65.     try:
  66.         while True :
  67.             d = sys.stdin.readline()
  68.             if not d :
  69.                 break
  70.             chan.send(d)
  71.     except EOFError :
  72.         pass
  73.             
  74. def main(host,pwd) :
  75.     try:
  76.         client = paramiko.SSHClient()
  77.         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  78.         client.connect(host,22,'root',pwd,timeout=5)
  79.         ch = client.invoke_shell()
  80.         interactive_shell(ch)
  81.         ch.close()
  82.         client.close()
  83.     except KeyboardInterrupt :
  84.         pass
  85.     except Exception as e :
  86.         print '*' * 20 ,'Exception error ,',e
  87.         

  88.     
  89. if __name__ == '__main__' :
  90.     main("192.168.125.136","123456")


阅读(1348) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~