无法杀死Python脚本

时间:2016-09-21 02:28:28

标签: python ssh paramiko

我希望我的脚本在sendShell函数中的所有命令完成执行commandfactory列表中的所有命令后停止。我可以成功关闭我的传输连接,但在运行所有命令并关闭传输连接后,脚本似乎挂起。当所有命令都执行完毕并且传输会话关闭时,任何人都可以帮我找到一种方法来停止我的脚本吗?

我试图通过添加而不是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()

1 个答案:

答案 0 :(得分:0)

我知道这已经老了......但我试图找出同样的事情一段时间。

就像其他一条评论中说的那样......删除"而真实:"在底部。

此外,您的全球连接" line被注释掉了。不评论。并将其添加到脚本底部以关闭连接。

connection.closeConnection(连接)