簡體   English   中英

如何使用paramiko保持ssh會話未過期?

[英]How to keep ssh session not expired using paramiko?

我打算使用paramiko在遠程主機上運行幾個命令,但運行命令后ssh會話關閉。
下面列出的代碼:

from paramiko import SSHClient  
import paramiko  
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, 22, user, passwd, timeout=3)
stdin, stdout, stderr = ssh.exec_command('uname -a')

那么有什么方法可以阻止ssh會話結束? 或帕拉米科的任何替代品?

更新
當連接到Linux服務器時,我能夠在我的Macbook上調用exec_command ,但是當連接到交換機時,在Linux服務器上執行exec_command一次后,ssh會話自動關閉並引發
SSHException: paramiko.ssh_exception.SSHException: SSH session not active

>>> print ssh.get_transport()  
>>> <paramiko.Transport at 0xf00216d0L (unconnected)>  
>>> print ssh.get_transport().is_active()  
>>> False  
>>> print ssh.get_transport().is_authenticated()  
>>> False

是否有任何方法可以讓paramiko ssh會話始終保持活動狀態?

paramiko調試模式信息返回如下:

啟動線程(客戶端模式):0x2657e10L
已連接(版本1.99,客戶端Comware-5.20)
kex algos:[u'diffie-hellman-group-exchange-sha1',u'diffie-hellman-group14-sha1',u'diffie-hellman-group1-sha1']服務器密鑰:[u'ssh-rsa']客戶端加密:[u'aes128-cbc',u'3des-cbc',u'des-cbc']服務器加密:[u'aes128-cbc',u'3des-cbc',u'des-cbc']客戶端mac:[u'hmac-sha1',u'hmac-sha1-96',u'hmac-md5',u'hmac-md5-96']服務器mac:[u'hmac-sha1',u'hmac -sha1-96',u'hmac-md5',u'hmac-md5-96']客戶端壓縮:[u'none']服務器壓縮:[u'none']客戶端lang:[u'']服務器lang :[u''] kex跟隨?錯誤
密碼同意:local = aes128-cbc,remote = aes128-cbc
使用kex diffie-hellman-group14-sha1; 服務器密鑰類型ssh-rsa; 密碼:本地aes128-cbc,遠程aes128-cbc; mac:local hmac-sha1,remote hmac-sha1; compression:local none,remote none
切換到新密鑰......
userauth沒問題
身份驗證(密碼)成功!
[chan 0]最大數據包:32768字節
[chan 1]最大數據包:32768字節
[chan 0]最大數據包輸出:32496字節
Secsh頻道0已打開。
Secsh channel 2 open FAILED:
資源短缺:資源短缺
[chan 0] Sesch頻道0請求確定
[chan 0] EOF發送(0)

您可以使用paramiko實現交互式shell,這樣在遠程shell上執行命令后通道不會關閉。

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

    @staticmethod
    def _print_exec_out(cmd, out_buf, err_buf, exit_status):
        print('command executed: {}'.format(cmd))
        print('STDOUT:')
        for line in out_buf:
            print(line, end="")
        print('end of STDOUT')
        print('STDERR:')
        for line in err_buf:
            print(line, end="")
        print('end of STDERR')
        print('finished with exit status: {}'.format(exit_status))
        print('------------------------------------')
        pass

    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)

        self._print_exec_out(cmd=cmd, out_buf=shout, err_buf=sherr, exit_status=exit_status)
        return shin, shout, sherr

我看到你在connect調用中使用了timeout參數:

ssh.connect(host, 22, user, passwd, timeout=3)

從文檔:

timeout(float) - TCP連接的可選超時(以秒為單位)

在我的一個腳本中,我只是這樣做:

ssh = paramiko.SSHClient()
ssh.connect(host, username=settings.user)

這使得連接保持打開狀態直到我打電話

ssh.close()

暫無
暫無

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

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