0

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

Tony Montana
  • 357
  • 2
  • 5
  • 16
  • Can you add an example of when you run the code? I imagine you haven't called `_create_channel` – FChm Feb 20 '19 at 15:01
  • N.B. If it were indentation, an IndentationError or SyntaxError would have been raised instead, not an AttributeError. – TrebledJ Feb 20 '19 at 15:02
  • 2
    you set the `self.channel` only in `_create_channel` method, not in the `__init__` constructor. This means if you touch it without calling the `_create_channel` method, you'll face that error, i guess this what happens.. Either add is to `__init__` or call `_create_channel` from `__init__` – Aaron_ab Feb 20 '19 at 15:07

1 Answers1

1

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

Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
mjkool
  • 230
  • 2
  • 9