我正在使用Python 2.7.12并尝试使用此代码。
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')
并且
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
我试图运行Client.addClient(..)
,但没有解决我的问题。
我想我需要一些帮助来理解这一点。如果它在Class里面怎么定义?
答案 0 :(得分:1)
您需要首先使用类的实例来使用其方法:
client = Client('192.168.1.94','root','root')
client.addClient('192.168.1.95','root','root')
如果您将方法定义为:
,则可以使用静态方法...
@staticmethod
def addClient(host, user, password):
client = Client(host, user, password)
clientNet.append(client)
并使用它:
Client.addClient(...)
无需制作实例。