创建使用libssh2访问远程服务器的python脚本
创建使用libssh2访问远程服务器的python脚本-我想创建访问远程服务器的python脚本,该远程服务器在同一会话中进一步访问另一台服务器。这将返回一个到主机组件的通道。然后我想执行此通道中的命令,例如access database。我已经用libssh2库尝试过了但是被卡住了。有人能帮我一下吗,谢谢。SSH2的实际Python模块是,它不使用libssh2。已经有了一个。也许它可以使您的任务更简单…使用Python绑定的Libssh2比Paramiko快得多 Python绑定:创建使用libssh2访问远程服务器的python脚本,python,libssh2,Python,Libssh2,创建使用libssh2访问远程服务器的python脚本-我想创建访问远程服务器的python脚本,该远程服务器在同一会话中进一步访问另一台服务器。这将返回一个到主机组件的通道。然后我想执行此通道中的命令,例如access database。我已经用libssh2库尝试过了但是被卡住了。有人能帮我一下吗,谢谢。SSH2的实际Python模块是,它不使用libssh2。已经有了一个。也许它可以使您的任务更简单…使用Python绑定的Libssh2比Paramiko快得多 Python绑定: API:
API:
#/usr/bin/python3
导入套接字
导入操作系统
导入时间
在eclipse中从ssh2.session导入会话#@unsolvedImport
类ssh_会话:
def _uinit _;(self,user,password,host=“localhost”,port=22):
sock=socket.socket(socket.AF\u INET,socket.sock\u流)
sock.connect((主机、端口))
self.s=Session()
自我握手(袜子)
self.s.userauth_密码(用户,密码)
self.chan=self.s.公开课()
self.chan.shell()
self.s.set_阻塞(假)#切换到非阻塞
self.received_字节=0
def read(self,minwait=0.01):#本地主机的等待时间足够10毫秒
buf=b“”#当您发送简单命令时
startread=time.time()
尽管如此:
大小,数据=self.chan.read()
如果大小>0:
buf+=数据
self.received_字节+=大小
睡眠时间(0.01)
如果minwait>0:#如果minwait大于wait
timedelta=time.time()-startread
如果timedelta>minwait:
打破
其他:
中断#非阻塞返回,零分钟等待
#重复while循环,直到minwait
返回(buf.decode())
def写入(self,cmd):
self.chan.write(cmd+“\n”)
user=“”
passwd=“”
contime=time.time()
时间差=0
con1=ssh_会话(用户,passwd)#创建连接实例
#跳过横幅aka os欢迎信息
#横幅应在1秒或超过200个接收字节后完成
当timedelta<1且con1.0接收的字节数<200时:
timedelta=time.time()-contime
con1.read()#只读不打印
#向主机发送命令
con1.写入(“pwd”)
#接收应答,在本地主机上连接只需要大约0.3ms
打印(con1.read())#您可以在此处通过以秒为单位的最短等待时间
#e、 g.控制1.读取(0.8)#等待0.8秒
#!/usr/bin/python3
import socket
import os
import time
from ssh2.session import Session #@UnresolvedImport in eclipse
class ssh_session:
def __init__(self,user, password, host = "localhost",port=22):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
self.s = Session()
self.s.handshake(sock)
self.s.userauth_password(user, password)
self.chan = self.s.open_session()
self.chan.shell()
self.s.set_blocking(False) #switch to non-blocking
self.received_bytes=0
def read(self,minwait=0.01): #10 ms wait are enough for localhost
buf = b"" #when you send simple commands
startread=time.time()
while True:
size, data = self.chan.read()
if size > 0:
buf+=data
self.received_bytes+=size
time.sleep(0.01)
if minwait > 0: # if we have a larger minwait than wait
timedelta = time.time()-startread
if timedelta > minwait:
break
else:
break # non-blocking return with zero minwait
#repeat the while loop until minwait
return (buf.decode())
def write(self,cmd):
self.chan.write(cmd+"\n")
user="<user>"
passwd="<pass>"
contime = time.time()
timedelta = 0
con1 = ssh_session(user,passwd) #create connection instance
#skip banner aka os welcome message
#banner should be done after 1 second or more than 200 received Bytes
while timedelta < 1 and con1.received_bytes < 200:
timedelta = time.time() - contime
con1.read() #just read but do not print
#send command to host
con1.write("pwd")
#receive answer, on localhost connection takes just about 0.3ms
print (con1.read()) #you can pass a minimum wait time in seconds here
#e.g. con1.read(0.8) # wait 0.8 seconds