我正在Windows PC和Linux设备之间运行SSH会话。 SSH连接的创建是通过Python完成的。 连接后,SSH会话将从Thar接收来自Thar的命令。 这是一般想法。 一切正常,但是在我发送命令之后,它似乎在流中“卡住了”,而我正在发送的所有其他命令正在发送先前的值。 我试过使用冲洗(代码中提到),但仍然没有结果:
用于建立SSH会话的Python代码:
导入线程,paramiko 从paramiko导入客户端 导入时间 ssh类: 客户=无 外壳=无
SELECT oo.ObjectId
FROM dbo.ObjectOwner oo
GROUP BY oo.ObjectId
HAVING NOT EXISTS (
SELECT 1
FROM dbo.ObjectOwner
WHERE dbo.ObjectOwner.EndDate IS NULL
)
我编写的TCL流程如下:
def __init__(self, address, username):
print("Connecting to server.")
cert = paramiko.RSAKey.from_private_key_file("QA-SIP-Evgenyz.pem")
self.client = client.SSHClient()
self.client.set_missing_host_key_policy(client.AutoAddPolicy())
self.client.connect(address, username=username, pkey=cert)
self.transport = paramiko.Transport((address, 22))
self.transport.connect(username=username, pkey=cert)
thread = threading.Thread(target=self.process)
thread.daemon = True
thread.start()
print("Connected")
def closeConnection(self):
if self.client is not None:
self.client.close()
self.transport.close()
def openShell(self):
self.shell = self.client.invoke_shell()
def sendShell(self, command):
if self.shell:
#print("trying to run command " + command)
self.shell.send(command + "\r")
time.sleep(0.6)
#self.shell.send("\r")
else:
print("Shell not opened.")
def process(self):
global connection
while True:
# Print data when available
if self.shell is not None and self.shell.recv_ready():
alldata = self.shell.recv(1024)
while self.shell.recv_ready():
alldata += self.shell.recv(1024)
strdata = str(alldata, "utf8")
strdata.replace('\r', '')
print(strdata, end = "")
if strdata.endswith("$ "):
print("\n$ ", end = "")
def main():
IP = '54.171.226.239'
user = 'ubuntu'
a = 0
command = ''
connection = ssh(IP, user)
connection.openShell()
i=0
while 1:
a = a+1
if a == 1:
command ='ls -l'
connection.sendShell(command)
command = input('$ ')
上面的proc返回我将用于向外壳发送其他命令的通道ID
proc open_ssh_connection {} {
set pystream [open "|bin/python -i MgrLoginCLass.py" r+]
fconfigure $pystream -blocking 0
set flag 0
while {1} {
set L [gets $pystream OutPut]
# Print it to the screen in Tcl
if {$OutPut != ""} {
puts "$OutPut \n"
if {[regexp ".*drwxrwxr-x" $OutPut]== 1} {
set flag 1
}
if {($flag == 1) && ([regexp ".*194:" $OutPut]== 1)} {
puts "here11"
set flag 2
break
}
}
}
return $pystream }
从上面的可以看出,先前的命令再次发送了一次,而不是存在的。 我怀疑通道缓冲区\ flush命令,但是更改它没有帮助,也许我没有按预期更改它。