Mar-19-2020, 12:50 PM
Hi, this is my first thread. I am beginning with Python, and I did not fully understand how can I transfer variables in between classes, nor outside classes.
I am using python-2.7.17.amd64 on a Windows 2012 R2.
My intention is to perform a basic check on some Cisco appliances.
In order to do the same I am using paramiko.
Below code display the results as expected, occurs that my intention is to store the results in strings (outside class), but I am not able to make it works.
My intention was to call def runAccess only once, store def runFilter outside Class/Function, then call runAccess multiple times (pulling data either from a DB, or txt file)
This is the entire code
Thank you
I am using python-2.7.17.amd64 on a Windows 2012 R2.
My intention is to perform a basic check on some Cisco appliances.
In order to do the same I am using paramiko.
Below code display the results as expected, occurs that my intention is to store the results in strings (outside class), but I am not able to make it works.
def runAccess(self):
#Access the system
remote_conn_pre = self.paramiko.SSHClient()
remote_conn_pre
remote_conn_pre.set_missing_host_key_policy(self.paramiko.AutoAddPolicy())
(...)
remote_conn = remote_conn_pre.invoke_shell()
(...)
self.remote_conn = remote_conn
return self.remote_conn
def runFilter(self):
runcommand = self.runAccess()
remote_conn = self.remote_connFor example, I need to pass string remote_conn to Function def runFilter(self), but instead of doing it, I am calling it via runcommand = self.runAccess() inside runFilter(self).My intention was to call def runAccess only once, store def runFilter outside Class/Function, then call runAccess multiple times (pulling data either from a DB, or txt file)
This is the entire code
#
!/usr/bin/env python
import paramiko
import time
import os
import re
sshUsername = "admin" #sshUsername = sshuser
sshPassword = "password1234" #sshPassword = sshpasw
sshIP = "192.168.1.201" #sshIP = sship
sshHost = sshIP #sshHost = sshhost
CommandToApply = "utils ntp status" #CommandToApply = comtoap
WaitTimeAfterCommand = 5 # WaitTimeAfterCommand = waitcom
KeyWordToFilter = "unsynchronised" #KeyWordToFilter = keywflt
GetHowManyWordsAfterKeyword = 2 # GetHowManyWordsAfterKeyword = getword
commout = ""
#Classes
class Runcommands:
def __init__(self, keywflt, getword, commout, sship, sshhost, sshuser, sshpasw, comtoap, waitcom, paramiko):
self.keywflt = keywflt
self.getword = getword
self.output = commout
self.ntpoutput = ""
self.sshIP = sship
self.sshHost = sshhost
self.sshUsername = sshuser
self.sshPassword = sshpasw
self.CommandToApply = comtoap
self.WaitTimeAfterCommand = waitcom
self.paramiko = paramiko
def runAccess(self):
#Access the system
remote_conn_pre = self.paramiko.SSHClient()
remote_conn_pre
remote_conn_pre.set_missing_host_key_policy(self.paramiko.AutoAddPolicy())
remote_conn_pre.connect(self.sshIP, username=self.sshUsername, password=self.sshPassword, look_for_keys=False, allow_agent=False)
print("SSH connection established to " + self.sshHost)
remote_conn = remote_conn_pre.invoke_shell()
print("Interactive SSH session established")
output = remote_conn.recv(1000)
#Waiting time to login into system
time.sleep(10)
self.remote_conn = remote_conn
return self.remote_conn
def runFilter(self):
runcommand = self.runAccess()
remote_conn = self.remote_conn
#Apply Commands into System
remote_conn.send("\n")
remote_conn.send(self.CommandToApply + "\n")
time.sleep(self.WaitTimeAfterCommand)
output = remote_conn.recv(5000)
if self.keywflt in output:
count = 0
ntp = re.search(self.keywflt, output)
keywordstart = ntp.start()
keywordend = ntp.end()
keywordend = keywordend + self.getword
for x in output:
count = count + 1
if count > keywordstart and count <= keywordend:
self.ntpoutput = self.ntpoutput + x
ntpoutput = self.ntpoutput
return(ntpoutput)
else:
print("Could not read output from previous comand: " + output)
#remote_conn.close()
resultado2 = Runcommands(KeyWordToFilter, GetHowManyWordsAfterKeyword, commout, sshIP, sshHost, sshUsername, sshPassword, CommandToApply, WaitTimeAfterCommand, paramiko).runFilter()
print(resultado2)Results are display as expected, but code still not doing what I need. Meanwhile I try to store only the strings I have different errors, but mostly the same is not passing.C:\Users\Administrator\Desktop\Script>python ssh5.py SSH connection established to 192.168.1.201 Interactive SSH session established unsynchronisedI appretiate any help
Thank you
