python paramiko module remotely execute multiple commands

Has recently been executed with paramiko python remote command, found that if the words written directly after each command will execute an error when a semicolon, if not a semicolon, a sub-command to execute a command, no effect, such as switches paths, etc., followed by the internet to find relevant ways, as long as exec_command (cmd, get_pty = True) after the command statement added get_pty = True to. code show as below:

    def exec_command(self,cmd):
        client = paramiko.SSHClient()
        try:
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            client.connect(self.hostip, self.port, username=self.username, password=self.pwd, timeout=20)
            stdin, stdout, stderr = client.exec_command(cmd,get_pty=True)
            # logWriteToTxt(self.sitename + "执行"+cmd)
            res=""
            results = stdout.readlines()

            for line in results:
                res+=line
            try:
                err=stderr.readlines()
                for line in err:
                    res+=line
            except:
                pass
                # results = stdout.readlines()
            # logWriteToTxt("在" + self.sitename + "执行"+cmd + res)

            return res
        except:
            pass
        finally:
            client.close()

  

Guess you like

Origin www.cnblogs.com/linwenbin/p/10972632.html