Insgesamt noob hier. Ich versuche, ein Python-Objekt zu erstellen und Methoden in einer Instanz darin auszuführen, und es scheint, dass der Code-Block, den ich ausführen möchte, einfach nicht ausgeführt wird. Der fragliche Codeblock ist run_job, der, wenn er aufgerufen wird, scheinbar nichts zu tun hat. Was mache ich falsch?Python-Methode nicht ausgeführt
import datetime
import uuid
import paramiko
class scan_job(object):
def __init__(self, protocol, target, user_name, password, command):
self.guid = uuid.uuid1()
self.start_time = datetime.datetime.now()
self.target = target
self.command = command
self.user_name = user_name
self.password = password
self.protocol = protocol
self.result = ""
def run_job(self):
if self.protocol == 'SSH':
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
print "creating connection"
ssh.connect(self.target, self.user_name, self.password)
print "connected"
stdin, stdout, stderr = ssh.exec_command(self.command)
for line in stdout:
print '... ' + line.strip('\n')
self.result += line.strip('\n')
yield ssh
finally:
print "closing connection"
ssh.close()
print "closed"
else:
print "Unknown protocol"
def show_command(self):
print self.command
test = scan_job('SSH', '192.168.14.10', 'myuser', 'mypassword', 'uname -n')
test.show_command()
test.run_job()
Sie haben eine Generator-Methode geschrieben, mit einer 'Yield' Aussage. Wolltest du das machen? Generatoren erstellen faule Iterables. –
Entfernen Sie den 'Ertrag ssh'. Ich weiß nicht, warum du es dort hast. –