Python Forum
Python 2.7 passing variables from functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 2.7 passing variables from functions
#1
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.

    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_conn
For 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
unsynchronised
I appretiate any help

Thank you
Reply


Messages In This Thread
Python 2.7 passing variables from functions - by zetto33 - Mar-19-2020, 12:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 521 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Passing writable arguments to functions. Assembler 11 1,140 Jan-15-2024, 11:32 PM
Last Post: sgrey
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 780 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Passing string functions as arguments Clunk_Head 3 1,312 Jun-15-2022, 06:00 AM
Last Post: Gribouillis
  using variables with functions imported from different files. Scordomaniac 3 1,319 May-24-2022, 10:53 AM
Last Post: deanhystad
  Storing whole functions in variables dedesssse 3 2,134 Jul-29-2021, 09:17 PM
Last Post: deanhystad
  passing php variable to python file jerald 1 2,753 Jul-07-2021, 11:46 AM
Last Post: Larz60+
  Passing flags to python script, through a function xbit 4 4,061 Apr-20-2021, 06:32 AM
Last Post: ndc85430
  Getting parent variables in nested functions wallgraffiti 1 2,176 Jan-30-2021, 03:53 PM
Last Post: buran
  Passing Variables between files. victorTJ 3 2,311 Oct-17-2020, 01:45 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020