0
私のsendShell関数内のすべてのコマンドがmy commandfactoryリスト内のすべてのコマンドの実行を終了すると、スクリプトを停止します。私は正常に私のトランスポート接続をシャットダウンすることができますが、スクリプトはすべてのコマンドが実行され、トランスポート接続が閉じた後にハングするようです。誰も私のスクリプトを停止する方法を見つけるのを助けることができますすべてのコマンドが実行を終了し、転送セッションが終了しますか?Pythonスクリプトをkillすることができません
私は出力ループにself.transport.close()を追加しないでスクリプトを削除しようとしました。
def process(self):
while not self.transport.close(): #Kill script here !!!
# 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 = alldata.decode("utf8")
strdata.rstrip("\r\n")
print(strdata, end = ""),
コード:
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.transport.close()
else:
print("Shell not opened.")
def process(self):
# global connection
while not self.transport.close():
# 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 = alldata.decode("utf8")
strdata.rstrip("\r\n")
print(strdata, end = ""),
sshUsername = "xxxxx"
sshPassword = "xxxxxxx"
sshServer = "xxxxxxxx"
connection = ssh(sshServer, sshUsername, sshPassword)
connection.openShell()
while True:
connection.sendShell()
あなたがここに投稿した本当のログイン資格情報ではないことを願っていますか? – benten
@benten、 彼らは閉鎖されたVMテストルータのログインですが、誰かが緊張した場合に備えて削除しました:) – adrian
'while True:'を削除してください。 –