-2

I'm a newbie to python and have been reading and surfing the net to accomplish my task.

I'm writing a function which will do a ssh to my device, execute few commands and display the result both in terminal and into a log file.

i have written something like this:

class logger(object):
    def __init__(self, filename="Default.log"):
        print 'Inside Logger Class' 
        self.terminal = sys.stdout 
        self.log = open(filename, "a")  

class simpleTelnet(logger):
    def __init__(self):
        print 'Inside simpleTelnt Constructor' 
        logger.__init__(self,"myfilename.txt") 
        self.log.write = 'Writing into the log file' 

    def telnetSession(self):
        p=pexpect.spawn('ssh admin@<ip address>') 
        p.logfile = sys.stdout 
        p.expect('Password:') 
        p.sendline('password') 
        time.sleep(2) 
        p.sendline('show version | no-more') 
        expect(pexpect.EOF, timeout = None) 
        out = p.before()
        self.log.write(p.logfile)
        p.close()
        return out


if __name__ == "__main__": 
    output = simpleTelnet()  
    cmd = output.telnetSession() 

Here i'm trying to login to a device and print the output on both stdout and also write to a file. I'm able to print in the stdout and log to a file but after executing the command, although i close the spawn class with p.close(), it does not close and end the script execution. the program stays there for ever. How do i close the program after executing these commands.

user596922
  • 1,501
  • 3
  • 18
  • 27
  • Did you actually read the traceback, and make any effort to try to understand it? It tells you simply and precisely what you have done wrong. Also, six lines above the line where the error occurs is an almost identical line that demonstrates how to do it right. – ekhumoro Dec 26 '14 at 16:49
  • oops.. got it..I should have used self.log.write('writing into the file') instead. this works. – user596922 Dec 26 '14 at 16:57

1 Answers1

1

.write is a method, not an attribute, so you should do variable.write("anything you want") and not `variable.write = "anything you want". Python wouldn't be able to change the content if you do not call a function.

So instead of doing:

class simpleTelnet(logger):
    def __init__(self):
        print 'Inside simpleTelnt Constructor' 
        logger.__init__(self,"myfilename.txt") 
        self.log.write = 'Writing into the log file

You'd do:

class simpleTelnet(logger):
    def __init__(self):
        print 'Inside simpleTelnt Constructor' 
        logger.__init__(self,"myfilename.txt") 
        self.log.write('Writing into the log file')

As the other user pointed out, you have the same line of code but with the correct syntax up there 6 lines above. You have to revise your code before posting a question. Try to do so next time.

  • This is completely wrong. The error has nothing to do with file permissions. – ekhumoro Dec 26 '14 at 16:53
  • @ekhumoro are you sure about that? –  Dec 26 '14 at 16:55
  • You can easily read the traceback for yourself, and see what the real error is. – ekhumoro Dec 26 '14 at 16:57
  • can i do something like this to write into a file? self.log.write(p.logfile) – user596922 Dec 26 '14 at 17:05
  • sorry @user596922? I didn't get that. Your code will work if you replace the wrong code you've got with my correct code. –  Dec 26 '14 at 17:08
  • @user596922 in fact if you are too lazy I edited my answer and made a copy-paste version. –  Dec 26 '14 at 17:11
  • yes..Juan .I got how to write into a file .. but the spawn class output i want to store in a file. I was reading through the documentation and the logfile stores all the output . so i was curious to know if i can write something like this... p.logfile = sys.stdout self.log.write(p.logfile) – user596922 Dec 26 '14 at 17:12
  • @user596922 sorry I do not understand. Please could you explain it again so I can help you? Thank you very much. Consider editing your answer if you think your question is not clear –  Dec 26 '14 at 17:15
  • I have modified the script. I;m able to write to a log file also. But could not come out. am i missing something here? please advise. – user596922 Dec 26 '14 at 17:39