Выполнение интерактивной команды SSH через скрипт PythonPython

Программы на Python
Anonymous
 Выполнение интерактивной команды SSH через скрипт Python

Сообщение Anonymous »

Я пытаюсь автоматизировать сбор журналов из Cisco Call Manager через CLI, используя команду from paramiko_expect import SSHClientInteraction, где я не могу отправить интерактивную команду на сервер.
При попытке загрузить журналы запрашивается такая информация, как IP-адрес SFTP, имя пользователя, пароль и каталог, который должен отправить интерактивную команду.
каждый раз, когда код запускается, он останавливается в разделе интерактивных команд, где он не отправляет команду на сервер, из-за чего скрипт Python останавливается здесь. Нужно знать, есть ли другой способ закодировать эти требования.
например,
Ниже раздела представлена ​​интерактивная оболочка, где мне нужно ввести y/xx.xx.xx.xx/22/User ID/Password/Directory, но я не могу сделать то же самое.
Мне нужна помощь здесь... чтобы отправить команду
+++++++++++++++++++++++++++++++++
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:

Я могу получить выходные данные команды show, но не могу загрузить журналы.
#!/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]?


Подробнее здесь: https://stackoverflow.com/questions/566 ... hon-script

Вернуться в «Python»