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

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

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 以下是 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]?

最佳答案

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

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

关于python - 通过Python脚本执行交互式SSH命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56605325/

相关文章:

python - 在 Panda DataFrame 中使用 For 循环 (Python)

python - 将特定维度的对角线设置为 1 的最佳方法是什么?

python - != 和 <> 有什么区别?

python - 如何使用先前的滚动平均值填充 pandas 数据框中的后续空值?

python - 在python中覆盖类变量

python - JupyterHub 内核对用户安全吗?

python - 丢失 : why is my neural network not working?

python - 在Elasticsearch中更新文档时出错

python - 如何迭代 Tensorflow 中的张量?

python - 从 WSGI 读取 session /cookie