在远程linux服务器上执行程序

时间:2019-06-28 09:16:11

标签: python paramiko

我想尝试在Linux服务器上执行程序“ kraken”。我四处上网,我发现了一些代码Paramiko。但是我无法运行程序,并向程序输入了一些信息。我没有得到任何输出。它不会退出。 命令='cd kraken / Kraken; ./ kraken ../indexes \ n' 这就是我想要运行以进入程序kraken的命令 运行上述命令后,输出必须是这样的:

Device: /dev/sdb 40
/dev/sdb
Allocated 41404056 bytes: ../indexes/132.idx
Allocated 41301076 bytes: ../indexes/260.idx
Allocated 41257060 bytes: ../indexes/108.idx
Allocated 41269576 bytes: ../indexes/140.idx
Allocated 41248788 bytes: ../indexes/404.idx
Allocated 41243292 bytes: ../indexes/196.idx
Allocated 41257176 bytes: ../indexes/388.idx
Allocated 41249180 bytes: ../indexes/156.idx
Allocated 41250740 bytes: ../indexes/292.idx
Allocated 41230652 bytes: ../indexes/220.idx
Allocated 41236892 bytes: ../indexes/372.idx
Allocated 41252956 bytes: ../indexes/172.idx
Allocated 41253772 bytes: ../indexes/500.idx
Allocated 41235184 bytes: ../indexes/180.idx
Allocated 41256180 bytes: ../indexes/436.idx
Allocated 41260184 bytes: ../indexes/428.idx
Allocated 41279240 bytes: ../indexes/188.idx
Allocated 41239116 bytes: ../indexes/492.idx
Allocated 41241432 bytes: ../indexes/164.idx
Allocated 41259888 bytes: ../indexes/324.idx
Allocated 41246592 bytes: ../indexes/356.idx
Allocated 41239636 bytes: ../indexes/204.idx
Allocated 41253276 bytes: ../indexes/212.idx
Allocated 41247816 bytes: ../indexes/420.idx
Allocated 41254252 bytes: ../indexes/412.idx
Allocated 41236164 bytes: ../indexes/348.idx
Allocated 41240688 bytes: ../indexes/396.idx
Allocated 41235072 bytes: ../indexes/100.idx
Allocated 41257276 bytes: ../indexes/230.idx
Allocated 41249048 bytes: ../indexes/340.idx
Allocated 41274520 bytes: ../indexes/124.idx
Allocated 41246480 bytes: ../indexes/364.idx
Allocated 41239444 bytes: ../indexes/238.idx
Allocated 41247488 bytes: ../indexes/116.idx
Allocated 41248632 bytes: ../indexes/268.idx
Allocated 41248652 bytes: ../indexes/332.idx
Allocated 41237644 bytes: ../indexes/148.idx
Allocated 41281052 bytes: ../indexes/250.idx
Allocated 41314028 bytes: ../indexes/380.idx
Allocated 41235976 bytes: ../indexes/276.idx
Tables: 132,260,108,140,404,196,388,156,292,220,372,172,500,180,436,428,188,492,164,324,356,204,212,420,412,348,396,100,230,340,124,364,238,116,268,332,148,250,380,276
Commands are: crack test quit

Kraken>

现在我想将输入放入“ Kraken>”之后的程序中,例如(crack 1100100011010001010000111001010010101001111010110001010100011100110010011101010110111101001010101001000010110010) 我该怎么做,显示并获取程序的输出。

这是我尝试过的一些代码

import paramiko
import re
class ShellHandler:

    def __init__(self, host, user, psw):
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(host, username=user, password=psw, port=22)

        channel = self.ssh.invoke_shell()
        self.stdin = channel.makefile('wb')
        self.stdout = channel.makefile('r')

    def __del__(self):
        self.ssh.close()

    def execute(self, cmd):
        """

        :param cmd: the command to be executed on the remote computer
        :examples:  execute('ls')
                    execute('finger')
                    execute('cd folder_name')
        """
        cmd = cmd.strip('\n')
        self.stdin.write(cmd + '\n')
        finish = 'end of stdOUT buffer. finished with exit status'
        echo_cmd = 'echo {} $?'.format(finish)
        self.stdin.write(echo_cmd + '\n')
        shin = self.stdin
        self.stdin.flush()

        shout = []
        sherr = []
        exit_status = 0
        for line in self.stdout:
            if str(line).startswith(cmd) or str(line).startswith(echo_cmd):
                # up for now filled with shell junk from stdin
                shout = []
            elif str(line).startswith(finish):
                # our finish command ends with the exit status
                exit_status = int(str(line).rsplit(maxsplit=1)[1])
                if exit_status:
                    # stderr is combined with stdout.
                    # thus, swap sherr with shout in a case of failure.
                    sherr = shout
                    shout = []
                break
            else:
                # get rid of 'coloring and formatting' special characters
                shout.append(re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]').sub('', line).
                             replace('\b', '').replace('\r', ''))

        # first and last lines of shout/sherr contain a prompt
        if shout and echo_cmd in shout[-1]:
            shout.pop()
        if shout and cmd in shout[0]:
            shout.pop(0)
        if sherr and echo_cmd in sherr[-1]:
            sherr.pop()
        if sherr and cmd in sherr[0]:
            sherr.pop(0)

        return shin, print(shout) , sherr
host = "xxx"
name = "xxx"
pwd = "xxx"
command = 'cd kraken/Kraken;./kraken ../indexes \n'
conn_one = ShellHandler(host,name,pwd)
conn_one.execute(command)
conn_one.execute('quit \n')

0 个答案:

没有答案