簡體   English   中英

使用Paramiko在Python上用ssh實現交互式shell?

[英]Implement an interactive shell over ssh in Python using Paramiko?

我想編寫一個程序(在Windows 7上的Python 3.x中),它通過ssh在遠程shell上執行多個命令。 在查看paramikos的exec_command()函數之后,我意識到它不適合我的用例(因為在執行命令后通道被關閉),因為命令依賴於環境變量(由先前的命令設置)並且不能連接到一個exec_command()調用,因為它們將在程序中的不同時間執行。

因此,我想在同一個通道中執行命令。 我研究的下一個選項是使用paramikos的invoke_shell()函數實現交互式shell:

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

channel = ssh.invoke_shell()

out = channel.recv(9999)

channel.send('cd mivne_final\n')
channel.send('ls\n')

while not channel.recv_ready():
    time.sleep(3)

out = channel.recv(9999)
print(out.decode("ascii"))

channel.send('cd ..\n')
channel.send('cd or_fail\n')
channel.send('ls\n')

while not channel.recv_ready():
    time.sleep(3)

out = channel.recv(9999)
print(out.decode("ascii"))

channel.send('cd ..\n')
channel.send('cd simulator\n')
channel.send('ls\n')

while not channel.recv_ready():
    time.sleep(3)

out = channel.recv(9999)
print(out.decode("ascii"))

ssh.close() 

這段代碼存在一些問題:

  1. 第一次print並不總是打印ls輸出(有時它只在第二次print )。
  2. 第一個cdls命令總是存在於輸出中(我通過recv命令獲取它們,作為輸出的一部分),而有時會打印所有以下cdls命令,有時它們不會打印。
  3. 第二個和第三個cdls命令(打印時)總是出現在第一個ls輸出之前。

我對這種“非決定論”感到困惑,非常感謝你的幫助。

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, shout, sherr

我基本上使用你所有的代碼,只是添加一個for循環:

commands = ["ls","command2","command3"]
conn_one = ShellHandler(host,name,pwd)
for command in commands:
      conn_one.execute(command)

它使用正確的輸出執行2個命令,但它只是坐在那里。 我想知道我是否需要在代碼中的某處調用del

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM