Ver Fonte

Fix some bug on brand Furukawa

gabriel há 6 anos atrás
pai
commit
377b3ba944
4 ficheiros alterados com 77 adições e 55 exclusões
  1. 2 2
      Furukawa.py
  2. 1 0
      ObjectConnection.py
  3. 13 8
      ObjectSsh.py
  4. 61 45
      ObjectTelnet.py

+ 2 - 2
Furukawa.py

@@ -6,13 +6,13 @@ class Furukawa(OLTBase):
         OLTBase.__init__(self, model, ssh)
 
     def get_expected_name(self):
-        return "Login:"
+        return "login:"
 
     def get_expected_password(self):
         return "Password:"
 
     def get_write_exit(self):
-        return "quit"
+        return "exit"
 
     def run_enable(self):
         return True

+ 1 - 0
ObjectConnection.py

@@ -1,6 +1,7 @@
 class ObjectConnection:
     def __init__(self):
         self.RUN_OK = 0
+        self.RUN_ERROR = 1
         self.QUIT = True
         self.olt = None
         self.brand = None

+ 13 - 8
ObjectSsh.py

@@ -1,5 +1,5 @@
 import types
-
+import errno
 import paramiko
 import sys
 import time
@@ -80,13 +80,18 @@ class ObjectSsh(ObjectConnection):
         :return: Return True if enable otherwise False
         """
         if command.lower() == self.olt.get_write_exit().lower():
-            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
-                self.stdin.write(self.olt.get_write_exit_confirmation() + self.NEW_LINE_UNIX)
-                self.stdin.flush()
-                self.QUIT = False
+            try:
+                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
+                    self.stdin.write(self.olt.get_write_exit_confirmation() + self.NEW_LINE_UNIX)
+                    self.stdin.flush()
+                    self.QUIT = False
+            # except self.tn.error, e:
+            except IOError, e:
+                if e.errno == errno.EPIPE:
+                    self.QUIT = False
             return True
         return False
 

+ 61 - 45
ObjectTelnet.py

@@ -1,3 +1,4 @@
+import errno
 import sys
 import telnetlib
 import time
@@ -37,27 +38,31 @@ class ObjectTelnet(ObjectConnection):
         print("Lock Acquire\n")
         print("Login in...\n")
         self.tn = telnetlib.Telnet(self.hostname, self.port)
-        # self.tn.set_debuglevel(1)
-        self.read_data([self.olt.get_expected_name()])
-        self.tn.write(self.user + self.NEW_LINE_UNIX)
-        self.read_data([self.olt.get_expected_password()])
-        self.tn.write(self.password + self.NEW_LINE_UNIX)
-        print("Loged in...\n")
-        print self.tn.read_very_lazy()
-        self.read_data([self.olt.get_expected_initial()])
-        if self.file_name is not None:
-            self.connection_file()
-        elif self.data is not None:
-            self.connection_data()
-        else:
-            self.connection_old()
-        print("fin-----")
-        while self.QUIT:
-            # send quit to terminal until exit confirmation
-            self.command_quit(self.olt.get_write_exit())
-        self.tn.close()
-        self.save_log()
-        exit(self.RUN_OK)
+        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)
+            error = self.read_data([self.olt.get_expected_password()])
+            if error != -1:
+                self.tn.write(self.password + self.NEW_LINE_UNIX)
+                print("Loged in...\n")
+                print self.tn.read_very_lazy()
+                error = self.read_data([self.olt.get_expected_initial()])
+                if error != -1:
+                    if self.file_name is not None:
+                        self.connection_file()
+                    elif self.data is not None:
+                        self.connection_data()
+                    else:
+                        self.connection_old()
+                    print("fin-----")
+                    while self.QUIT:
+                        # send quit to terminal until exit confirmation
+                        self.command_quit(self.olt.get_write_exit())
+                    self.tn.close()
+                    self.save_log()
+                    exit(self.RUN_OK)
+        exit(self.RUN_ERROR)
 
     def connection_old(self):
         """
@@ -111,21 +116,26 @@ class ObjectTelnet(ObjectConnection):
         :return: Return True if enable otherwise False
         """
         if command.lower() == self.olt.get_write_exit().lower():
-            self.tn.write(self.olt.get_write_exit() + self.NEW_LINE_UNIX)
-            read_data = None
-            with_confirmation = False
-            if self.olt.get_expected_exit() is not None:
-                read_data = [self.olt.get_expected_cardinal(), self.olt.get_expected_exit()]
-                with_confirmation = True
-            if self.olt.get_expected_exit_with_confirmation() is not None:
-                read_data = [self.olt.get_expected_cardinal(), self.olt.get_expected_exit_with_confirmation()]
+            try:
+                self.tn.write(self.olt.get_write_exit() + self.NEW_LINE_UNIX)
+                read_data = None
                 with_confirmation = False
-            if read_data is not None:
-                position = self.read_data(read_data)
-                if position == 2:
-                    # quit terminal
-                    if with_confirmation:
-                        self.tn.write(self.olt.get_write_exit_confirmation() + self.NEW_LINE_UNIX)
+                if self.olt.get_expected_exit() is not None:
+                    read_data = [self.olt.get_expected_cardinal(), self.olt.get_expected_exit()]
+                    with_confirmation = True
+                if self.olt.get_expected_exit_with_confirmation() is not None:
+                    read_data = [self.olt.get_expected_cardinal(), self.olt.get_expected_exit_with_confirmation()]
+                    with_confirmation = False
+                if read_data is not None:
+                    position = self.read_data(read_data)
+                    if position == 2:
+                        # quit terminal
+                        if with_confirmation:
+                            self.tn.write(self.olt.get_write_exit_confirmation() + self.NEW_LINE_UNIX)
+                        self.QUIT = False
+            # except self.tn.error, e:
+            except IOError, e:
+                if e.errno == errno.EPIPE:
                     self.QUIT = False
             return True
         return False
@@ -150,14 +160,20 @@ class ObjectTelnet(ObjectConnection):
         Read channel waiting parameter character
         :param expected: List of expected string
         """
-        position = 1
-        (i, obj, all_data) = self.tn.expect(expected, 10)
-        nc = 1
-        for ch in expected:
-            if ch in all_data:
-                position = nc
-                break
-            nc += 1
-        print(str(all_data))
-        self.all_data = self.all_data + str(all_data)
+        iteration = 0
+        position = -1
+        while iteration < 6:
+            (i, obj, all_data) = self.tn.expect(expected, 10)
+            nc = 1
+            for ch in expected:
+                if ch in all_data:
+                    position = nc
+                    break
+                nc += 1
+            if position == -1:
+                iteration += 1
+            else:
+                iteration = 6
+                print(str(all_data))
+                self.all_data = self.all_data + str(all_data)
         return position