Python - connecting SSH wrappers to remote devices with threading












0















The problem I would like to solve is get the following:




  • instantiate 5-10 host ssh sessions simultaneously (threading??)

  • leave the connections established and awaiting for external calls

  • execute predifined methods under the Host class (such as getUberImportantDetails() )


  • expose information and selected methods for ssh wrappers with flask rest API.



    class Host():
    def __init__(self, host, user, psw):
    super().__init__(host, user, psw)
    self.bypass = ''
    self.state = "IDLE"

    def connect(self):
    self.ssh = paramiko.SSHClient()
    <a lot of regexp based SSH processing>

    def getUberImportantDetails(self):
    output = self.execute("show very important data")
    return SRE4BGPNeighborParser(output)



Host's init() method has to get host ip address, username and password.
Before getting any information out of the object i need to run the connect method to establish the SSH connection over a range of jumphosts.
When Host class connect routing is finished, I am ready to interact with the remote device.



I was able to initialize the class object with a thread wrapper, but I also want to leave the threads running after connect() function done all the work and the handler is ready for interaction with the remote device. How do i do it nicely?
Is there a better way to address my problem?



def threaded(fn):
def wrapper(*args, **kwargs):
thread = threading.Thread(target=fn, args=args, kwargs=kwargs)
thread.start()
return thread
return wrapper


class ThreadHost(threading.Thread, Host):
def __init__(self, host, user, psw):
super(Host, self).__init__(host, user, psw)
self.bypass = ''
self.state = "IDLE"

@threaded
def run(self):
print("Starting connection to {}".format(self.host))
self.connect()









share|improve this question



























    0















    The problem I would like to solve is get the following:




    • instantiate 5-10 host ssh sessions simultaneously (threading??)

    • leave the connections established and awaiting for external calls

    • execute predifined methods under the Host class (such as getUberImportantDetails() )


    • expose information and selected methods for ssh wrappers with flask rest API.



      class Host():
      def __init__(self, host, user, psw):
      super().__init__(host, user, psw)
      self.bypass = ''
      self.state = "IDLE"

      def connect(self):
      self.ssh = paramiko.SSHClient()
      <a lot of regexp based SSH processing>

      def getUberImportantDetails(self):
      output = self.execute("show very important data")
      return SRE4BGPNeighborParser(output)



    Host's init() method has to get host ip address, username and password.
    Before getting any information out of the object i need to run the connect method to establish the SSH connection over a range of jumphosts.
    When Host class connect routing is finished, I am ready to interact with the remote device.



    I was able to initialize the class object with a thread wrapper, but I also want to leave the threads running after connect() function done all the work and the handler is ready for interaction with the remote device. How do i do it nicely?
    Is there a better way to address my problem?



    def threaded(fn):
    def wrapper(*args, **kwargs):
    thread = threading.Thread(target=fn, args=args, kwargs=kwargs)
    thread.start()
    return thread
    return wrapper


    class ThreadHost(threading.Thread, Host):
    def __init__(self, host, user, psw):
    super(Host, self).__init__(host, user, psw)
    self.bypass = ''
    self.state = "IDLE"

    @threaded
    def run(self):
    print("Starting connection to {}".format(self.host))
    self.connect()









    share|improve this question

























      0












      0








      0








      The problem I would like to solve is get the following:




      • instantiate 5-10 host ssh sessions simultaneously (threading??)

      • leave the connections established and awaiting for external calls

      • execute predifined methods under the Host class (such as getUberImportantDetails() )


      • expose information and selected methods for ssh wrappers with flask rest API.



        class Host():
        def __init__(self, host, user, psw):
        super().__init__(host, user, psw)
        self.bypass = ''
        self.state = "IDLE"

        def connect(self):
        self.ssh = paramiko.SSHClient()
        <a lot of regexp based SSH processing>

        def getUberImportantDetails(self):
        output = self.execute("show very important data")
        return SRE4BGPNeighborParser(output)



      Host's init() method has to get host ip address, username and password.
      Before getting any information out of the object i need to run the connect method to establish the SSH connection over a range of jumphosts.
      When Host class connect routing is finished, I am ready to interact with the remote device.



      I was able to initialize the class object with a thread wrapper, but I also want to leave the threads running after connect() function done all the work and the handler is ready for interaction with the remote device. How do i do it nicely?
      Is there a better way to address my problem?



      def threaded(fn):
      def wrapper(*args, **kwargs):
      thread = threading.Thread(target=fn, args=args, kwargs=kwargs)
      thread.start()
      return thread
      return wrapper


      class ThreadHost(threading.Thread, Host):
      def __init__(self, host, user, psw):
      super(Host, self).__init__(host, user, psw)
      self.bypass = ''
      self.state = "IDLE"

      @threaded
      def run(self):
      print("Starting connection to {}".format(self.host))
      self.connect()









      share|improve this question














      The problem I would like to solve is get the following:




      • instantiate 5-10 host ssh sessions simultaneously (threading??)

      • leave the connections established and awaiting for external calls

      • execute predifined methods under the Host class (such as getUberImportantDetails() )


      • expose information and selected methods for ssh wrappers with flask rest API.



        class Host():
        def __init__(self, host, user, psw):
        super().__init__(host, user, psw)
        self.bypass = ''
        self.state = "IDLE"

        def connect(self):
        self.ssh = paramiko.SSHClient()
        <a lot of regexp based SSH processing>

        def getUberImportantDetails(self):
        output = self.execute("show very important data")
        return SRE4BGPNeighborParser(output)



      Host's init() method has to get host ip address, username and password.
      Before getting any information out of the object i need to run the connect method to establish the SSH connection over a range of jumphosts.
      When Host class connect routing is finished, I am ready to interact with the remote device.



      I was able to initialize the class object with a thread wrapper, but I also want to leave the threads running after connect() function done all the work and the handler is ready for interaction with the remote device. How do i do it nicely?
      Is there a better way to address my problem?



      def threaded(fn):
      def wrapper(*args, **kwargs):
      thread = threading.Thread(target=fn, args=args, kwargs=kwargs)
      thread.start()
      return thread
      return wrapper


      class ThreadHost(threading.Thread, Host):
      def __init__(self, host, user, psw):
      super(Host, self).__init__(host, user, psw)
      self.bypass = ''
      self.state = "IDLE"

      @threaded
      def run(self):
      print("Starting connection to {}".format(self.host))
      self.connect()






      python multithreading ssh






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 3:44









      Dvalin SwampDvalin Swamp

      286




      286
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292896%2fpython-connecting-ssh-wrappers-to-remote-devices-with-threading%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292896%2fpython-connecting-ssh-wrappers-to-remote-devices-with-threading%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values