Kaynağa Gözat

Add debug setting. In constructor set value to TRUE. Defult is FALSE.

gabriel 6 yıl önce
ebeveyn
işleme
2c6f48f690
2 değiştirilmiş dosya ile 29 ekleme ve 2 silme
  1. 26 1
      ObjectSsh.py
  2. 3 1
      ObjectTelnet.py

+ 26 - 1
ObjectSsh.py

@@ -17,6 +17,7 @@ class ObjectSsh(ObjectConnection):
         ObjectConnection.__init__(self)
         self.stdin = None
         self.stdout = None
+        self.debug = False
 
     def connect(self):
         """
@@ -41,6 +42,7 @@ class ObjectSsh(ObjectConnection):
         self.stdin = chan.makefile('wb')
         self.stdout = chan.makefile('r')
         # wait to olt to initialice terminal
+        print ("Logged in")
         self.read_data(self.olt.get_expected_initial())
         if self.file_name is not None:
             self.connection_file()
@@ -65,9 +67,15 @@ class ObjectSsh(ObjectConnection):
         """
         if command.lower() == self.olt.get_write_enable().lower():
             if self.olt.run_enable():
+                if self.debug:
+                    print "Send: "
+                    print self.olt.get_write_enable()
                 self.stdin.write(self.olt.get_write_enable() + self.NEW_LINE_UNIX)
                 if self.olt.run_enable_password():
                     self.read_data(self.olt.get_expected_enable_password())
+                    if self.debug:
+                        print "Send: "
+                        print self.password_enable
                     self.stdin.write(self.password_enable + self.NEW_LINE_UNIX)
                 self.read_data(self.olt.get_expected_cardinal())
             return True
@@ -81,10 +89,16 @@ class ObjectSsh(ObjectConnection):
         """
         if command.lower() == self.olt.get_write_exit().lower():
             try:
+                if self.debug:
+                    print "Send: "
+                    print self.olt.get_write_exit()
                 self.stdin.write(self.olt.get_write_exit() + self.NEW_LINE_UNIX)
                 position = self.read_data([self.olt.get_expected_cardinal(), self.olt.get_expected_exit()])
                 if position == 2:
                     # quit terminal
+                    if self.debug:
+                        print "Send: "
+                        print self.olt.get_write_exit_confirmation()
                     self.stdin.write(self.olt.get_write_exit_confirmation() + self.NEW_LINE_UNIX)
                     self.stdin.flush()
                     self.QUIT = False
@@ -102,9 +116,12 @@ class ObjectSsh(ObjectConnection):
         :param expected: Expected string to stop listening
         """
         self.command_print(command)
+        command = command.rstrip(self.NEW_LINE_WINDOWS).rstrip(self.NEW_LINE_UNIX)
         if not self.command_enable(command) and not self.command_quit(command):
-            command = command.rstrip(self.NEW_LINE_WINDOWS).rstrip(self.NEW_LINE_UNIX)
             if command.__len__() > 0:
+                if self.debug:
+                    print "Send: "
+                    print command
                 self.stdin.write(command + self.NEW_LINE_UNIX)
                 self.stdin.flush()
                 self.read_data(expected)
@@ -120,10 +137,18 @@ class ObjectSsh(ObjectConnection):
         position = 1
         if isinstance(character, types.StringType):
             character = [character]
+        if self.debug:
+            print "Expected: "
+            print character
+            print "Recived:"
         while not self.stdout.channel.exit_status_ready() and not stop:
             all_data += self.stdout.channel.recv(buffer_size)
+            if self.debug:
+                print all_data
             while self.stdout.channel.recv_ready():
                 all_data += self.stdout.channel.recv(buffer_size)
+                if self.debug:
+                    print all_data
             nc = 1
             for ch in character:
                 if ch in all_data:

+ 3 - 1
ObjectTelnet.py

@@ -16,6 +16,7 @@ class ObjectTelnet(ObjectConnection):
     def __init__(self):
         ObjectConnection.__init__(self)
         self.tn = None
+        self.debug = False
 
     def connect(self):
         """
@@ -38,7 +39,8 @@ class ObjectTelnet(ObjectConnection):
         print("Lock Acquire\n")
         print("Login in...\n")
         self.tn = telnetlib.Telnet(self.hostname, self.port)
-        # self.tn.set_debuglevel(1)
+        if self.debug:
+            self.tn.set_debuglevel(1)
         error = self.read_data([self.olt.get_expected_name()])
         if error != -1:
             self.tn.write(self.user + self.NEW_LINE_UNIX)