0

Total noob here. I'm trying to create a python object and execute methods in an instance therein and it seems the code block I want to execute just won't run. The code block in question is run_job which when called just seems to do nothing. What am I doing wrong?

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()

2 Answers2

0

Your method contains a yield statement, which makes it a generator. Generators are evaluated lazily. Consider:

>>> def gen():
...   yield 10
...   yield 3
...   yield 42
... 
>>> result = gen()
>>> next(result)
10
>>> next(result)
3
>>> next(result)
42
>>> 

This is likely not what you intended to do.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • wasn't me but at a guess because it doesn't answer the question. I see you have made a point about generators but how does that relate to the OPs issue? – Paul Rooney Oct 11 '16 at 04:06
  • @PaulRooney can you really not infer, or are you implying that I should be more explicit? – juanpa.arrivillaga Oct 11 '16 at 04:16
  • @PaulRooney and anyway, while I'll admit it could use more elaboration, I believe that the statement "Your method contains a yield statement, which makes it a generator. Generators are evaluated lazily." correctly answers why "when called just [the method] seems to do nothing". – juanpa.arrivillaga Oct 11 '16 at 04:20
  • yes I can infer I was only implying it could be more explicit. – Paul Rooney Oct 11 '16 at 04:25
0

Yield is a keyword that is used like return, except the function will return a generator.

To read more about generators:
1) Understanding Generators in Python
2) What does the "yield" keyword do in Python?
3) Understanding the Yield Keyword in Python

All you need to do is, change:

yield ssh

To:

return ssh

So that, run_job will execute like a normal function, until it reaches its end, exception or return statement. However, if you want to run it without changing the yield statement. Here is how you can do it:

x = test.run_job()
x.next()
A.Sherif
  • 144
  • 2
  • 8