簡體   English   中英

退出 Python 中的控制台連接

[英]Exit out of a console connection in Python

我正在編寫一些 python 代碼以登錄到包含虛擬應用程序 (VTA) 的白盒設備。 將安裝兩個不同的 VTA,代碼將登錄到物理設備,然后使用 virsh 控制台(VTA 的名稱)登錄到 VTA。

我遇到的問題是退出一個 VTA,然后 virsh 控制台進入另一個。 exit 命令只是讓我再次進入登錄提示,但不會退出控制台連接。

為此,我必須發送一個“control + ]”才能跳出控制台。 我一直在網上搜索以嘗試找到解決方案,但我發現的唯一選擇是發送並“退出”,然后是“\x1b”。 但是,這實際上並沒有脫離控制台 window。 相反,它結束了 session,這不是我想要的。

有沒有辦法在 python 中發送“Control + ]”?

以下是一些顯示步驟的代碼:

from paramiko import SSHClient, AutoAddPolicy
import time
import re
import os
import sys
import progressbar
import stat

def create_connection(host):
    username = ''
    password = ''
    port = 22
    connection_info = {
        'port': port,
        'username': username,
        'password': password
    }

    client = SSHClient()
    client.set_missing_host_key_policy(AutoAddPolicy())
    client.connect(host, timeout=10, auth_timeout=10, **connection_info)
    ssh_session = client.invoke_shell()
    ssh_session.settimeout(10.0)

    return ssh_session


def send_commands(ssh_session, commands, sleep_time=1):
    for command in commands:
        ssh_session.send(command)
        time.sleep(sleep_time)

    output = ssh_session.recv(1048576)
    decoded_output = output.decode()
    return decoded_output


console_commands = [
        'virsh console vw-vta\n',
        '\n',
        '\n',  # Place the username of the VTA here
        '\n'  # Place the password of the VTA here
    ]
show_mgmt_commands = [
        'ifconfig ens2 | grep Bcast\n'
    ]

exit_console = [
        'exit\n'
        '\x1b'
    ]
validate_commands = [
        'virsh list\n'
    ]


def validation():
    host = input('What is the IP address? ')
    print('\n')
    print(f'\033[1;33m--< Checking {host} for a valid VTA >------------\033[0m')

    try:
        ssh_session = create_connection(host)
    except Exception as l:
        print(f"\033[1;31mCannot connect to {host}!\033[0m")
        return

    validate = send_commands(ssh_session, validate_commands)

    if 'y1564' in validate:
        print(f"\033[1;32mThere is a Y1564 VTA running! Obtaining information...\033[0m")
        ssh_session = create_connection(host)
        console = send_commands(ssh_session, console_commands)

        try:
            show_mgmt = send_commands(ssh_session, show_mgmt_commands, sleep_time=2)

        except Exception as e:
            print(f"\033[1;31mCouldn't reach the console on " f"\033[1;33m{host}\033[0m"f"\033[1;31m. This VTA will need to be rebuilt.\033[0m")



        if 'Login incorrect' in show_mgmt:
            print(f"\033[1;31m--< Begin ERROR MESSAGE >------------\033[0m")
            print(show_mgmt)
            print(f"\033[1;31m--< End ERROR MESSAGE >------------\033[0m")
            print(f"\033[1;31mThis VTA has the incorrect password!\033[0m")
            print(f'{host},VTA Password Error', file=f)
            exit = send_commands(ssh_session, exit_console)
            return
        else:
            try:
                mgmt = show_mgmt.split('addr:')[1].split(" ")[0]
            except Exception as g:
                print(f"\033[1;31mThis VTA is corrupt and will need to be rebuilt!\033[0m")
                exit = send_commands(ssh_session, exit_console)
                return
            print("Y1564 VTA IP: ", mgmt)
            exit = send_commands(ssh_session, exit_console)

    else:
        print("\033[1;33mThere is NOT a Y1564 VTA running on \033[0m"f"\033[1;34m {host}\033[0m")
    
    ssh_session.close()

if __name__ == '__main__':
    full_check()

當這個 function 完成時,代碼的第二部分調用類似的 function。 但是它失敗了,因為之前的 function 沒有斷開控制台連接。 代碼嘗試發送下一個 function 的命令,而它仍在前一個 VTA 中。

這是一個 output 顯示它在做什么:

What is the IP address? 1.1.1.1


--< Checking 1.1.1.1 for a valid VTA >------------
There is a Y1564 VTA running! Obtaining information...
Y1564 VTA IP:  10.10.10.10
exit
logout


--< Checking 1.1.1.1 for a valid VTA >------------
Ubuntu 16.04.6 LTS y1564 ttyS0

y1564 login:

virsh list
Password:
There is NOT a VTA running on 1.1.1.1

上面的 output 顯示,當運行 exit 命令並后跟 \x1b 時,它沒有正確退出,而是嘗試從下一部分發送“virsh list”命令。

您用於發送 ctrl+] 的十六進制字符是錯誤的。 將您的退出控制台命令更新為: exit_console = [ 'exit\n', '\x01D' ]

ASCII 參考: http://www.physics.udel.edu/~watson/scen103/ascii.html

看起來您正在使用invoke_shell()方法,您可以使用close()方法退出該特定 shell 並根據需要建立一個新的。

帕拉米科參考: http://docs.paramiko.org/en/stable/api/channel.html

暫無
暫無

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

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