Tony Montana
Tony Montana

Reputation: 357

AttributeError: 'Object has no attribute'

I have following block of code:

class HwSwitch(object):

    def __init__(self):
        pass

    def _create_channel(self):
        try:
            self.channel = self.ssh.invoke_shell()
        except SSHException:
            raise SSHException("Unable to invoke the SSH Command shell")

    def _send_cmd_to_channel(self, cmd):
        try:
            time.sleep(1)
            self.channel.send(cmd + '\r\n')
            out = self.channel.recv(9999)
        except SSHException:
            raise SSHException("Execution of command '%s' failed" % cmd)
        return str(out)

But I get always error which says: AttributeError: 'HwSwitch' object has no attribute 'channel'. It seems that problem is somewhere at self.channel.send(cmd + '\r\n') but I cannot see where. Is there something wrong (maybe indentation?). Thanks

Upvotes: 0

Views: 2020

Answers (1)

mjkool
mjkool

Reputation: 230

You are accessing 'channel' as an instance variable, either create it in the __init__ or call _create_channel before calling _send_cmd_to_channel.

Also refer this

Upvotes: 1

Related Questions