通过Python脚本执行交互式SSH命令

通过Python脚本执行交互式SSH命令,python,Python,每当代码运行时,它都会在交互命令部分停止,因为python脚本停止在这里,所以它不会将命令发送到服务器。需要知道是否有其他方法来编码这些需求 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 inter

每当代码运行时,它都会在交互命令部分停止,因为python脚本停止在这里,所以它不会将命令发送到服务器。需要知道是否有其他方法来编码这些需求

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

您可以尝试在发送任何其他命令之前,在程序的开头添加全局配置命令“file prompt quiet”。这将抑制任何是/否问题,并将其自动设置为默认值。只需确保在代码结束时使用“文件提示警报”将其关闭,以防止以后出现任何令人不快的意外情况


这适用于大多数Cisco IOS平台,如果CUCM中的命令不同,我相信会有一个等效命令来执行相同的操作。

在发送任何其他命令之前,您可以尝试在程序开头添加全局配置命令“file prompt quiet”。这将抑制任何是/否问题,并将其自动设置为默认值。只需确保在代码结束时使用“文件提示警报”将其关闭,以防止以后出现任何令人不快的意外情况


这适用于大多数Cisco IOS平台,如果CUCM中的命令不同,我相信会有一个等效的命令来执行相同的操作。

也许您已经解决了这个问题,但我看到,您有一个小类型,它可能会阻止脚本向前移动: 你有: 互动。(“管理员:”) 而不是:
互动。expect('admin:')

也许您已经解决了这个问题,但我发现,您有一个小类型,它可能会阻止脚本继续前进: 你有: 互动。(“管理员:”) 而不是:
互动。期待('admin:')

嗨,戴夫,谢谢你的回复。我检查了CUCM的命令,但不幸的是,没有这样的命令。不幸的是,我在mo,但当我回去工作时,我会询问我们的合作CCIE,看看他是否有任何想法。出于好奇,您是否尝试过向控制台发送新行?“/n”不确定如果默认值可接受,是否会按enter键,或者甚至不确定是否按“yes/n”来键入命令?2分钟就可以试试了,值得一试。嗨,戴夫,谢谢你的回复。我检查了CUCM的命令,但不幸的是,没有这样的命令。不幸的是,我在mo,但当我回去工作时,我会询问我们的合作CCIE,看看他是否有任何想法。出于好奇,您是否尝试过向控制台发送新行?“/n”不确定如果默认值可接受,是否会按enter键,或者甚至不确定是否按“yes/n”来键入命令?2分钟就可以尝试了,值得一试。
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]?