i am using cli ssh in one of my python code to get data from cli commanda.
i have two cli commands which is giving me output in a tabular form. so i want to store the data i am getting from the command in a dictionary and using the dictionary i want to combine with a key value to display my data.
What I have tried:
######## python snippet to find CMS WAE Config details KPI's
import getopt
import logging
import time
import datetime
import json
import re
from paramiko import SSHClient, AutoAddPolicy
import silo_common.snippets as em7_snippets
from collections import defaultdict
SNIPPET_NAME = 'WAE: Config Details'
DEVICEIP = self.ip
DID = self.did
DEBUG_STR= '%s : %s : %s' % ( SNIPPET_NAME, DID, DEVICEIP)
##uses ssh credential to establish an ssh session.
def ssh_conn():
##Set a default.
if self.cred_details['cred_port'] is not None:
ssh_port = self.cred_details['cred_port']
print "ssh_port is", ssh_port
else:
ssh_port = 2024
try:
ssh = SSHClient()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect(self.cred_details["cred_host"], username=self.cred_details["cred_user"], password=self.cred_details["cred_pwd"], port=ssh_port)
return ssh
except Exception as e:
PROBLEM_STR = "%s:%s: Exception Caught - %s: %s" % (DEBUG_STR,name, e.message, e.args)
self.logger.ui_debug('Exception Caught : %s' % PROBLEM_STR)
connection = []
return connection
##executes cli request return either in list of rows, or long string.
def ssh_shell_request(ssh,request,output=False):
res = []
try:
stdin, stdout, stderr = ssh.exec_command(request)
self.logger.ui_debug ('output of exec command, stdout=%s ' % (stdout))
if output is True:
if stdout is not None:
res = ''.join(stdout)
self.logger.ui_debug ('result of the exec command when output is true=%s' % (res))
return res
else:
for line in stdout:
#line = line.translate(None, "\n\r\"")
if (line == "" and SKIP_EMPTY_LINES):
continue
else:
res.append(line.strip())
self.logger.ui_debug ('result of the exec command when output is false =%s' % (res))
return res
except Exception as e:
PROBLEM_STR = "%s:%s: Exception Caught - %s: %s" % (DEBUG_STR,name, e.message, e.args)
self.logger.ui_debug('Exception Caught : %s' % PROBLEM_STR)
return res
##handles all the static calls and formats into cache object so we can save
def get_data(conn):
res_data = {}
arr_lines=[]
try:
##We are only getting running containers by default.. add -a to see all containsers. (not tested)
request = "switch cli \n show ncs-state daemon-status ; show packages package oper-status"
print "request is ", request
lines = ssh_shell_request(conn,request)
self.logger.ui_debug ('output of the shell request =%s' % (lines))
if lines is not None and isinstance(lines, list):
for line in lines:
arr_lines.append(line)
self.logger.ui_debug ('arr lines inside the get data =%s' % (arr_lines))
return arr_lines
except Exception as e:
PROBLEM_STR = "%s:%s: Exception Caught - %s: %s" % (DEBUG_STR,name, e.message, e.args)
self.logger.ui_debug('Exception Caught : %s' % PROBLEM_STR)
return arr_lines
def get_data_version(conn):
arr_version = []
##We are only getting running containers by default.. add -a to see all containsers. (not tested)
request = " switch cli \n show packages package"
lines= ssh_shell_request(conn,request)
self.logger.ui_debug ('output of the shell request =%s' % (lines))
if lines is not None and isinstance(lines, list):
for line in lines:
arr_version.append(line)
self.logger.ui_debug ('arr lines inside the get data =%s' % (arr_version))
return arr_version
#Main
daemonstatus = "started"
pck_name = {}
pck_status = {}
pck_version = {}
dd=defaultdict(list)
if self.cred_details['cred_type'] == 6:
if self.cred_details['cred_host'] is not None \
and self.cred_details['cred_pwd'] is not None \
and self.cred_details['cred_user'] is not None:
try:
conn = ssh_conn()
if conn is not None:
arr_lines = get_data(conn)
arr_run_lines = get_data_version(conn)
#arr_oper_lines = get_oper_datastore_data(conn)
for line in arr_lines:
self.logger.ui_debug ('inside the arr lines array =%s' % (line))
line = line.lower()
self.logger.ui_debug ('output of the get data in main loop in lower case =%s' % (line))
if line.find('daemon-status started') > 0:
daemonstatus='started'
self.logger.ui_debug ('daemon-status line=%s,daemonstatus=%s ' % (line,daemonstatus))
elif 'package' in line:
package_name.append((cnt,line.split(" ")[2]))
elif 'oper-status' in line:
pck_status.append((cnt,line.split(" ")[1]))
cnt = cnt + 1
except Exception as e:
PROBLEM_STR = "%s:%s: Exception Caught - %s: %s" % (DEBUG_STR,name, e.message, e.args)
self.logger.ui_debug('Exception Caught : %s' % PROBLEM_STR)
status=[]
status.append((0,daemonstatus))
result_handler['daemon_status'] = status
result_handler['package_name']= package_name.items()
result_handler['package_status'] = pck_status.items()
result_handler['package_version'] = pck_version