簡體   English   中英

通過 Python 腳本執行交互式 SSH 命令

[英]Executing Interactive SSH Command via Python script

I am trying to automate to collect the logs from the Cisco Call Manager via CLI by using the from paramiko_expect import SSHClientInteraction where I am not able to send the interactive command to the server. 

While trying to download the logs, it will ask information like SFTP IP address, username, password and directory which needs to send an interactive command.

每當代碼運行時,它都會在交互式命令部分停止,在那里它不會將命令發送到服務器,因為 python 腳本在這里停止。 需要知道有沒有其他方法可以對這些要求進行編碼。

for example

Below section is interactive shell where I have to type y/xx.xx.xx.xx/22/User ID/Password/Directory but I can't do the same. 

I need help here.. to send the command 

+++++++++++++++++++++++++++++++++
Would you like to proceed [y/n]? y
SFTP server IP: xx.xx.xx.xx
SFTP server port [22]: 22
User ID: *****
Password: *****
Download directory: /
+++++++++++++++++++++++++++++++++

Command Line Interface is starting up, please wait ...

   Welcome to the Platform Command Line Interface

VMware Installation:
    4 vCPU: Intel(R) Xeon(R) Platinum 8180 CPU @ 2.50GHz
    Disk 1: 110GB, Partitions aligned
    6144 Mbytes RAM

admin:file get activelog /syslog/AlternateSyslog
Please wait while the system is gathering files info ...
 Get file: active/syslog/AlternateSyslog
done.
Sub-directories were not traversed.
Number of files affected: 5
Total size in Bytes: 23354752
Total size in Kbytes: 22807.375
Would you like to proceed [y/n]? y
SFTP server IP: xx.xx.xx.xx
SFTP server port [22]: 
User ID: *****
Password: *****
Download directory: /

The authenticity of host 'xx.xx.xx.xx (xx.xx.xx.xx)' can't be established.
Are you sure you want to continue connecting (yes/no)? yes
.....
Transfer completed.
admin:


I am able to get the show command output but not able to download the logs.



    #!/usr/bin/python
    # PSFL license
    # Importing SSHClientInteraction from paramiko 
    import paramiko
    from paramiko_expect import SSHClientInteraction
    import threading
    # Specify connection info for each node in square brackets: ["IP ADDRESS", "USERNAME", "PASSWORD"]
    connection = [["xx.xx.xx.xx", "userid", "password"]]

    # Define function which is responsible for opening SSH connection and running specified commands
    def cucm(ip, username, password):
        sshsession = paramiko.SSHClient()
        sshsession.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        sshsession.connect(ip, username=username, password=password)
         # "display=True" is just to show you what script does in real time. While in production you can set it to False
        interact = SSHClientInteraction(ssh, timeout=600, display=True)
        # program will wait till session is established and CUCM returns admin prompt
        interact.expect('admin:') 
        # program runs show status command
        interact.send('show status')
        # program waits for show status command to finish (this happen when CUCM returns admin prompt) 
        interact.except('admin:') 
        # program sends syslog to download the file
        interact.send('file get activelog /syslog/AlternateSyslog')
        if interact.last_match == 'Would you like to proceed [y/n]? ': # program matches prompted command by using if command and will send interact command to it. 
            interact.send('y')
        if interact.last_match == 'SFTP server IP:':
            interact.send('xx.xx.xx.xx')
        if interact.last_match == 'SFTP server port [22]:':
            interact.send('22')
        if interact.last_match == 'User ID:':
            interact.send('userid')
        if interact.last_match == 'Password:':
            interact.send('password')
        if interact.last_match == 'Download directory:':
            interact.send('/')
        interact.expect('admin:')
        output = interact.current_output_clean # program saves output of show status command to the "output" variable
        sshsession.close()

    # Run loop which will open separate thread for each node specified in the connection list. This targets "session" function defined at the beginning 
    for i in connection:
        t = threading.Thread(target = cucm, args = (i[0], i[1], i[2]))
        t.daemon = True
        t.start()

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++ 下面是python腳本的輸出。

there is no error message but it stops at Would you like to proceed [y/n]?  here

Command Line Interface is starting up, please wait ...

   Welcome to the Platform Command Line Interface

VMware Installation:
    4 vCPU: Intel(R) Xeon(R) Platinum 8180 CPU @ 2.50GHz
    Disk 1: 110GB, Partitions aligned
    6144 Mbytes RAM

admin:file get activelog /syslog/AlternateSyslog
Please wait while the system is gathering files info ...
 Get file: active/syslog/AlternateSyslog
done.
Sub-directories were not traversed.
Number of files affected: 1
Total size in Bytes: 2261400
Total size in Kbytes: 2208.3984
Would you like to proceed [y/n]?

在發送任何其他命令之前,您可以嘗試在程序的開頭添加全局配置命令“file prompt quiet”。 這將抑制任何是/否問題並將它們自動設置為默認值。 只需確保在代碼結束時將其關閉,以防止以后使用“文件提示警報”出現任何令人討厭的意外。

這適用於大多數 Cisco IOS 平台,如果 CUCM 中的命令不同,我相信會有一個等效的命令來做同樣的事情。

也許你已經解決了這個問題,但我看到,你有一個小類型,它可以阻止前進的腳本:你在那里:交互。 除了('admin:') 而不是:交互。 期望('管理員:')

暫無
暫無

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

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