-3

After much effort on python class now my code working as expected.

But I used below code in every place in my script:

        buff = ''
        while not buff.endswith('#'):
            resp = self.remote_conn.recv(9999)
            buff += resp
            print resp

Any one can please help me to avoid using above code multiple times, in the below code

class Asr:
    def __init__(self):

        ''' SSH connection Establish '''
        self.hostname = hostname
        self.net_username = net_username
        self.net_password = net_password
    def remote(self):
        self.remote_conn_pre = paramiko.SSHClient()
        self.remote_conn_pre.set_missing_host_key_policy(
             paramiko.AutoAddPolicy())
        self.remote_conn_pre.connect(self.hostname, username=self.net_username, password=self.net_password, look_for_keys=False, allow_agent=False)
        self.remote_conn = self.remote_conn_pre.invoke_shell()
    def disable_paging(self):
        self.remote_conn.send("terminal length 0\n")
    def inventory(self):

        self.remote()
        self.remote_conn.send("\n")
        self.remote_conn.send("ping 1.1.1.1\n")
        buff = ''
        while not buff.endswith('#'):
            resp = self.remote_conn.recv(9999)
            buff += resp
            print resp
        self.remote_conn.send("ping 1.1.1.1\n")
        buff = ''
        while not buff.endswith('#'):
            resp = self.remote_conn.recv(9999)
            buff += resp
            print resp
        self.remote_conn.send("ping 1.1.1.1\n")
        buff = ''
        while not buff.endswith('#'):
            resp = self.remote_conn.recv(9999)
            buff += resp
            print resp
        self.remote_conn.close()
    def version(self):
        self.remote()
        self.remote_conn.send("\n")
        self.remote_conn.send("ping 1.1.1.1\n")
        buff = ''
        while not buff.endswith('#'):
            resp = self.remote_conn.recv(9999)
            buff += resp
            print resp
        self.disable_paging()
        self.remote_conn.send("show inventory\n")
        buff = ''
        while not buff.endswith('#'):
            resp = self.remote_conn.recv(9999)
            buff += resp
            print resp
        self.remote_conn.close()
asr = Asr()
asr.inventory()
asr.version()
3
  • 2
    Ummm.. make a function? Commented Jun 3, 2017 at 17:16
  • I dont know for some reason if make function it is not working. I mean paramiko is disconnecting . Let me try in paramiko level what is happening Commented Jun 3, 2017 at 17:32
  • See stackoverflow.com/questions/26785952/… for additional encouragement & theoretical base. Commented Jun 3, 2017 at 17:58

1 Answer 1

0

Use a function same as above Eg:

def check_buff(self, buff):

    while not buff.endswith('#'):
        resp = self.remote_conn.recv(9999)
        buff += resp
        print resp

call the function and pass the buff parameter each time. Eg:

buff = ''
self.check_buff(buff)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.