忘记从哪里搂来的脚本了,ssh server后,vim 没法正常使用
-
#!/usr/bin/python
-
#--*-- coding:utf-8 --*--
-
-
-
import socket
-
import sys
-
import getopt
-
import re
-
-
import paramiko
-
-
-
try :
-
import termios
-
import ttr
-
has_termios = True
-
-
except ImportError :
-
has_termios = False
-
-
def interactive_shell(chan):
-
if has_termios :
-
posix_shell(chan)
-
else:
-
win_shell(chan)
-
-
def posix_shell(chan) :
-
import select
-
oldttr = termios.tcgetattr(sys.stdin)
-
try :
-
tty.setraw(sys.stdin.fileno())
-
tty.setcbreak(sys.stdin.fileno())
-
chan.settimeout(0.0)
-
while True :
-
r,w,e = select.select([chan,sys,stdin],[],[])
-
if chan in r :
-
try :
-
x = chan.recv[1024]
-
if len(x) == 0 :
-
print "rn***EOFrn"
-
break
-
sys.stdout.write(x)
-
sys.stdout.flush()
-
except socket.timeout :
-
pass
-
if sys.stdin in r :
-
x = sys.stdin.read(1)
-
if len(x) == 0 :
-
break
-
chan.send(x)
-
finally :
-
termios.tcgetattr(sys.stdin.termios.TCSADRAIN,oldttr)
-
-
def win_shell(chan) :
-
import threading
-
sys.stdout.write("Press F6 or ctrl+z to send EOF")
-
def writeall(chan) :
-
while True :
-
data = chan.recv(256)
-
reobj = re.compile('(\x1b\[\d+m)|\x1b(\[\d+;\d+m)')
-
data,num = reobj.subn('',data)
-
if not data :
-
sys.stdout.write("rn***EOF***rnrn")
-
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.readline()
-
if not d :
-
break
-
chan.send(d)
-
except EOFError :
-
pass
-
-
def main(host,pwd) :
-
try:
-
client = paramiko.SSHClient()
-
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
-
client.connect(host,22,'root',pwd,timeout=5)
-
ch = client.invoke_shell()
-
interactive_shell(ch)
-
ch.close()
-
client.close()
-
except KeyboardInterrupt :
-
pass
-
except Exception as e :
-
print '*' * 20 ,'Exception error ,',e
-
-
-
-
if __name__ == '__main__' :
-
main("192.168.125.136","123456")
阅读(1400) | 评论(0) | 转发(0) |