python 利用paramiko批量管理功能示例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import paramiko
import sys
import os
import socket
import select
import getpass
import tty
import termios
# from paramiko.py3compat import u py27需要注释
tran = paramiko.Transport(('192.168.1.124',22,))
tran.start_client()
tran.auth_password('root','123456')
#打开一个通道
chan = tran.open_session()
#获取一个通道
chan.get_pty()
#激活器
chan.invoke_shell()
#获取原tty属性,目的是为了在操作完以后恢复终端原型
oldtty = termios.tcgetattr(sys.stdin)
try:
#为tty设置新属性
#默认当前tty设备属性
#输入一行回车,执行
#ctrl +c 进程退出,遇到特殊字符,特殊处理
#这是为原始模式,不认识特殊字符号
#放置特殊字符应用在当前终端,如此设置,将所有的用户输入均发送到 远程服务器
tty.setraw(sys.stdin.fileno())
chan.settimeout(0.0)
while True:
#监视用户输入和服务器返回数据
#阻塞,直到句柄可读
readable,writeable,error = select.select(,[],[],1)
if chan in readable:
try:
#x = u(chan.recv(1024))
x = chan.recv(1024) #py27写法
if len(x) == 0:
print('\r\n*** EOF\r\n')
break
sys.stdout.write(x)
sys.stdout.flush()
except socket.timeout:
pass
if sys.stdin in readable:
inp = sys.stdin.read(1)
if len(inp) == 0:
break
chan.send(inp)
finally:
#重新设置终端属性
termios.tcsetattr(sys.stdin,termios.TCSADRAIN,oldtty)
chan.close()
tran.close()
页:
[1]