我正在使用Python 2.7.12並嘗試此代碼。名稱未在Python中定義
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(..)
,但並沒有解決我的問題。 我想我需要一些幫助來理解這個..如果它不在類裏面怎麼定義?
您需要首先創建'Client'實例...'myClient = Client('192.168.1.94','root','root')' –
除了所有其他答案,您還需要添加' self'到'addClient'的參數(並且可能也適用於其他方法) – DeepSpace
請參閱https://stackoverflow.com/questions/735975/static-methods-in-python –