我的sendShell對象在列表commandfactory []中運行後,我需要幫助終止我的SSH會話。Python - PARAMIKO SSH close session
我有一個python腳本,我使用paramiko通過ssh連接到思科實驗室路由器;在commandfactory []中執行命令;並將結果輸出到標準輸出。一切似乎都起作用,除非在我的所有命令運行後,我似乎無法讓SSH會話關閉。會話保持打開狀態,直到我終止我的腳本。
import threading, paramiko, re, os
class ssh:
shell = None
client = None
transport = None
def __init__(self, address, username, password):
print("Connecting to server on ip", str(address) + ".")
self.client = paramiko.client.SSHClient()
self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
self.client.connect(address, username=username, password=password, look_for_keys=False)
self.transport = paramiko.Transport((address, 22))
self.transport.connect(username=username, password=password)
thread = threading.Thread(target=self.process)
thread.daemon = True
thread.start()
def closeConnection(self):
if(self.client != None):
self.client.close()
self.transport.close()
def openShell(self):
self.shell = self.client.invoke_shell()
def sendShell(self):
self.commandfactory = []
print("\nWelcome to Command Factory. Enter Commands you want to execute.\nType \"done\" when you are finished:")
while not re.search(r"done.*", str(self.commandfactory)):
self.commandfactory.append(input(":"))
if self.commandfactory[-1] == "done":
del self.commandfactory[-1]
break
print ("Here are the commands you're going to execute:\n" + str(self.commandfactory))
if(self.shell):
self.shell.send("enable" + "\n")
self.shell.send("ilovebeer" + "\n")
self.shell.send("term len 0" + "\n")
for cmdcnt in range(0,len(self.commandfactory)):
self.shell.send(self.commandfactory[cmdcnt] + "\n")
self.shell.send("exit" + "\n")
self.shell.send("\n")
else:
print("Shell not opened.")
def process(self):
global connection
while True:
# Print data when available
if self.shell != 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.strip()
print(strdata, end = "")
sshUsername = "adrian"
sshPassword = "ilovebeer"
sshServer = "10.10.254.129"
connection = ssh(sshServer, sshUsername, sshPassword)
connection.openShell()
while True:
connection.sendShell()
我想一直跑了(下面的代碼)SSH會話終止一旦所有的命令,在我的「的CommandFactory」名單。
def sendShell(self):
self.commandfactory = []
print("\nWelcome to Command Factory. Enter Commands you want to execute.\nType \"done\" when you are finished:")
while not re.search(r"done.*", str(self.commandfactory)):
self.commandfactory.append(input(":"))
if self.commandfactory[-1] == "done":
del self.commandfactory[-1]
break
print ("Here are the commands you're going to execute:\n" + str(self.commandfactory))
if(self.shell):
self.shell.send("enable" + "\n")
self.shell.send("ilovebeer" + "\n")
self.shell.send("term len 0" + "\n")
for cmdcnt in range(0,len(self.commandfactory)):
self.shell.send(self.commandfactory[cmdcnt] + "\n")
self.shell.send("exit" + "\n")
self.shell.send("\n")
我的代碼主要取自https://daanlenaerts.com/blog/2016/07/01/python-and-ssh-paramiko-shell/。非常感謝Daan Lenaerts建立一個好的博客。我做了自己的修改以適應我的需求。
VB,OMG,它一直在我面前。我知道要使用self.transport.close(),但我不知道在哪裏,直到我看到您的回覆;出於某種奇怪的原因,這正是我弄明白的原因。爲了得到我想要的,我將transport.close()放在我的commandfactory迭代器之後。 現在,如果我可能會問,如果我關閉會話,那麼我會如何打破我的循環並結束我的python腳本? – adrian