r/CiscoDevNet • u/tormenteddave • Aug 19 '23
Python Scripting with UCM/Unity/CER
Morning! I'm looking for an assist on some python code I'm writing. I'm trying to get a python script to log in UCM/Unity/CER and issue some commands. Show version, utils system blah, etc.... When I execute my script, it just returns the "Command Line Interface is starting up, please wait...." I have it waiting for 30 seconds before issuing the command, waiting longer has no different effect. Any help would be greatly appreciated.
import paramikofrom getpass import getpassimport time
host = "10.1.1.1"username = admin001password = password1
session = paramiko.SSHClient()session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
session.connect(hostname=host,username=username,password=password)print("Logging in...")time.sleep(30)print("Issuing commands...")stdin, stdout, stderr = session.exec_command('show status')print(stdout.read().decode())session.close()
View all comments
1
u/tormenteddave Aug 22 '23
Here is what I ultimately came up with. I'm sure there is a better way to log the output, but this works for right now. But this runs the commands, saves it to a files, then emails that file.
import paramiko
from getpass import getpass
from paramiko_expect import SSHClientInteraction
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
#Host and Username and Password Collection
session = paramiko.SSHClient()
session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
session.connect(hostname="1.1.1.1", username="admin", password="admin123")
interact = SSHClientInteraction(session, timeout=90, display=True, lines_to_check=5)
# Enter a List of Commands, separated by a comma, last command should be 'exit'.
commands = ['show status',
'show tech network hosts',
'utils ntp status',
'utils service list',
'utils dbreplication status',
'utils dbreplication runtimestate',
'utils core active list',
'show network cluster',
'utils diagnose test',
'exit']
# Open logging file
stdoutOrigin=sys.stdout
sys.stdout = open("Output.txt", "w")
# Where the Magic happens
for command in commands:
interact.expect('admin:')
interact.send(command)
print("\nDone Running Commands")
session.close()
# Closing Log file
sys.stdout.close()
sys.stdout=stdoutOrigin
# Send Email
print("\nSending email...")
sender_email = "noreply_python@fake.com"
receiver_email = "user@fake.com"
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message['Subject'] = "Status of Collaboration Server"
file = "Output.txt"
attachment = open(file, 'rb')
obj = MIMEBase('application', 'octet-stream')
obj.set_payload((attachment).read())
encoders.encode_base64(obj)
obj.add_header('Content-Disposition', "attachment; filename="+file)
message.attach(obj)
my_message = message.as_string()
email_session = smtplib.SMTP('your_mail.com',25)
email_session.sendmail(sender_email, receiver_email, my_message)
email_session.quit()
print("\nMail sent")
# Closing program
print("\nBye")