0

I'm using Python 2.7.12 and trying this code.

clientNet = []
class Client:
    def __init__(self, host, user, password):
        self.host = host
        self.user = user
        self.password = password
        self.session = self.connect()
    def connect(self):
        try:
            s = pxssh.pxssh()
            s.login(self.host, self.user, self.password)
            return s
        except Exception, e:
            print e
            print '[-] Error Connecting'
    def botnetCommand(command):
        for client in clientNet:
            output = client.send_command(command)
            print '[*] Output from ' + client.host
            print '[+] ' + output + '\n'
    def send_command(self, cmd):
        self.session.sendline(cmd)
        self.session.prompt()
        return self.session.before
    def addClient(host, user, password):
        client = Client(host, user, password)
        clientNet.append(client)

addClient('192.168.1.94','root','root')

And

 Traceback (most recent call last):
  File "host.py", line 33, in <module>
    addClient('192.168.1.94','root','root')
NameError: name 'addClient' is not defined

I tried to run Client.addClient(..) but didn't resolve my problem. I think I need some help to understand this.. How cannot be defined if it is inside of the Class?

MSeifert
  • 145,886
  • 38
  • 333
  • 352

1 Answers1

1

You need to make an instance of the class to use its methods first:

client = Client('192.168.1.94','root','root')
client.addClient('192.168.1.95','root','root')

Otherwise you could use a static method if you define the method as:

...
@staticmethod
def addClient(host, user, password):
    client = Client(host, user, password)
    clientNet.append(client)

and use it like:

Client.addClient(...)

without having to make an instance.

Igor Pejic
  • 3,658
  • 1
  • 14
  • 32
  • 2
    This will still not work. `addClient` is an instance method but it doesn't reserve an argument for the instance. – DeepSpace Aug 09 '17 at 12:32
  • I am fully aware it won't fully work. However I answered his question and I have no need to solve his task. – Igor Pejic Aug 09 '17 at 12:39
  • Thanks for the answer, i don't understand why if I turn the method static I can use just add cliente and otherwise no – David Russo Aug 09 '17 at 13:30