在远程linux系统中执行程序

2024-09-29 23:25:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想尝试在linux服务器上执行一个程序“kraken”。我上网查了一下,发现了一些密码Paramiko。然而,我没能运行程序,并把一些输入程序。我没有得到任何输出。它不会退出。 命令='cd kraken/kraken;/kraken../index\n' 这是我要执行的命令,进入“海怪”计划 执行上述命令后,输出必须如下所示:

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 11001000110100010100011101001001010100111101011000100101100000110000110011101010111101001001001010101001001001001001001010110010) 如何才能做到这一点,并显示和获得程序的输出。你知道吗

这是我试过的一些密码

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')

Tags: inechoselfcmdexecuteifbytesstdin

热门问题