Sending an uptime to a remote server through SSH with the method pxssh

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I have this code that I'm putting into a library script.



The aim of the code is the following:



  • The method is to connect to a remote server and run uptime on the remote server. The uptime will be displayed on the host machine, to see for how long the server has been running.

The aim in a near future, is to uptime-check several servers and have the results of my program, on the running machine.



I put the the method under a main to not run it when the library is imported.



from pexpect import pxssh
import pexpect

# conn_up is a function for connecting to a remote server and doing an uptime.

def conn_up():
try:
s = pxssh.pxssh()
hostname = '192.168.1.32'
username = 'pi'
s.login(hostname, username,ssh_key='/home/miaou/priv_keys')
s.sendline('uptime') # run a command
s.prompt() # match the prompt
print(s.before.decode()) # print everything before the prompt.
except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.")
print(e)


if __name__ == '__main__':
conn_up()


Currently I have only one method, conn_up().



But if I want to create additional methods to broaden the tools available for my administration, should I list them all of them under __name__ == '__main__' or is there a simpler way to call all the future methods all at once?







share|improve this question

















  • 1




    This is an awkward library. Why do these two pieces of functionality belong together in the same program or library? Also, the conn_wr() function looks like a hypothetical example or placeholder for some other functionality. I feel like you're hiding something from us in this question, and it's hard to advise you properly.
    – 200_success
    Jun 25 at 16:18






  • 2




    This question has been discussed on Meta.
    – 200_success
    Jun 26 at 6:03






  • 2




    I've updated my question with better context. Please have a look and let me know if I need to improve anything. I would like to have an answer on my question. Cheers.
    – Andy K
    Jun 26 at 14:42











  • With Rev 8, the question has once again veered into the hypothetical realm, asking for advice about code not yet written.
    – 200_success
    Jun 26 at 17:48
















up vote
0
down vote

favorite












I have this code that I'm putting into a library script.



The aim of the code is the following:



  • The method is to connect to a remote server and run uptime on the remote server. The uptime will be displayed on the host machine, to see for how long the server has been running.

The aim in a near future, is to uptime-check several servers and have the results of my program, on the running machine.



I put the the method under a main to not run it when the library is imported.



from pexpect import pxssh
import pexpect

# conn_up is a function for connecting to a remote server and doing an uptime.

def conn_up():
try:
s = pxssh.pxssh()
hostname = '192.168.1.32'
username = 'pi'
s.login(hostname, username,ssh_key='/home/miaou/priv_keys')
s.sendline('uptime') # run a command
s.prompt() # match the prompt
print(s.before.decode()) # print everything before the prompt.
except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.")
print(e)


if __name__ == '__main__':
conn_up()


Currently I have only one method, conn_up().



But if I want to create additional methods to broaden the tools available for my administration, should I list them all of them under __name__ == '__main__' or is there a simpler way to call all the future methods all at once?







share|improve this question

















  • 1




    This is an awkward library. Why do these two pieces of functionality belong together in the same program or library? Also, the conn_wr() function looks like a hypothetical example or placeholder for some other functionality. I feel like you're hiding something from us in this question, and it's hard to advise you properly.
    – 200_success
    Jun 25 at 16:18






  • 2




    This question has been discussed on Meta.
    – 200_success
    Jun 26 at 6:03






  • 2




    I've updated my question with better context. Please have a look and let me know if I need to improve anything. I would like to have an answer on my question. Cheers.
    – Andy K
    Jun 26 at 14:42











  • With Rev 8, the question has once again veered into the hypothetical realm, asking for advice about code not yet written.
    – 200_success
    Jun 26 at 17:48












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have this code that I'm putting into a library script.



The aim of the code is the following:



  • The method is to connect to a remote server and run uptime on the remote server. The uptime will be displayed on the host machine, to see for how long the server has been running.

The aim in a near future, is to uptime-check several servers and have the results of my program, on the running machine.



I put the the method under a main to not run it when the library is imported.



from pexpect import pxssh
import pexpect

# conn_up is a function for connecting to a remote server and doing an uptime.

def conn_up():
try:
s = pxssh.pxssh()
hostname = '192.168.1.32'
username = 'pi'
s.login(hostname, username,ssh_key='/home/miaou/priv_keys')
s.sendline('uptime') # run a command
s.prompt() # match the prompt
print(s.before.decode()) # print everything before the prompt.
except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.")
print(e)


if __name__ == '__main__':
conn_up()


Currently I have only one method, conn_up().



But if I want to create additional methods to broaden the tools available for my administration, should I list them all of them under __name__ == '__main__' or is there a simpler way to call all the future methods all at once?







share|improve this question













I have this code that I'm putting into a library script.



The aim of the code is the following:



  • The method is to connect to a remote server and run uptime on the remote server. The uptime will be displayed on the host machine, to see for how long the server has been running.

The aim in a near future, is to uptime-check several servers and have the results of my program, on the running machine.



I put the the method under a main to not run it when the library is imported.



from pexpect import pxssh
import pexpect

# conn_up is a function for connecting to a remote server and doing an uptime.

def conn_up():
try:
s = pxssh.pxssh()
hostname = '192.168.1.32'
username = 'pi'
s.login(hostname, username,ssh_key='/home/miaou/priv_keys')
s.sendline('uptime') # run a command
s.prompt() # match the prompt
print(s.before.decode()) # print everything before the prompt.
except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.")
print(e)


if __name__ == '__main__':
conn_up()


Currently I have only one method, conn_up().



But if I want to create additional methods to broaden the tools available for my administration, should I list them all of them under __name__ == '__main__' or is there a simpler way to call all the future methods all at once?









share|improve this question












share|improve this question




share|improve this question








edited Jun 26 at 17:46









200_success

123k14143399




123k14143399









asked Jun 25 at 13:53









Andy K

282415




282415







  • 1




    This is an awkward library. Why do these two pieces of functionality belong together in the same program or library? Also, the conn_wr() function looks like a hypothetical example or placeholder for some other functionality. I feel like you're hiding something from us in this question, and it's hard to advise you properly.
    – 200_success
    Jun 25 at 16:18






  • 2




    This question has been discussed on Meta.
    – 200_success
    Jun 26 at 6:03






  • 2




    I've updated my question with better context. Please have a look and let me know if I need to improve anything. I would like to have an answer on my question. Cheers.
    – Andy K
    Jun 26 at 14:42











  • With Rev 8, the question has once again veered into the hypothetical realm, asking for advice about code not yet written.
    – 200_success
    Jun 26 at 17:48












  • 1




    This is an awkward library. Why do these two pieces of functionality belong together in the same program or library? Also, the conn_wr() function looks like a hypothetical example or placeholder for some other functionality. I feel like you're hiding something from us in this question, and it's hard to advise you properly.
    – 200_success
    Jun 25 at 16:18






  • 2




    This question has been discussed on Meta.
    – 200_success
    Jun 26 at 6:03






  • 2




    I've updated my question with better context. Please have a look and let me know if I need to improve anything. I would like to have an answer on my question. Cheers.
    – Andy K
    Jun 26 at 14:42











  • With Rev 8, the question has once again veered into the hypothetical realm, asking for advice about code not yet written.
    – 200_success
    Jun 26 at 17:48







1




1




This is an awkward library. Why do these two pieces of functionality belong together in the same program or library? Also, the conn_wr() function looks like a hypothetical example or placeholder for some other functionality. I feel like you're hiding something from us in this question, and it's hard to advise you properly.
– 200_success
Jun 25 at 16:18




This is an awkward library. Why do these two pieces of functionality belong together in the same program or library? Also, the conn_wr() function looks like a hypothetical example or placeholder for some other functionality. I feel like you're hiding something from us in this question, and it's hard to advise you properly.
– 200_success
Jun 25 at 16:18




2




2




This question has been discussed on Meta.
– 200_success
Jun 26 at 6:03




This question has been discussed on Meta.
– 200_success
Jun 26 at 6:03




2




2




I've updated my question with better context. Please have a look and let me know if I need to improve anything. I would like to have an answer on my question. Cheers.
– Andy K
Jun 26 at 14:42





I've updated my question with better context. Please have a look and let me know if I need to improve anything. I would like to have an answer on my question. Cheers.
– Andy K
Jun 26 at 14:42













With Rev 8, the question has once again veered into the hypothetical realm, asking for advice about code not yet written.
– 200_success
Jun 26 at 17:48




With Rev 8, the question has once again veered into the hypothetical realm, asking for advice about code not yet written.
– 200_success
Jun 26 at 17:48










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










It sounds like the problem is not the code but how to package it up in a such a way that only functions that you want to call get called when you want to call them.



A part of the solution is as you have defined it ...



if __name__ == '__main__':
conn_up()


... this would ensure that conn_up() gets called, but you could just treat conn_up as a constructor and pull all the code from it and put it in another function which you then call on demand.



From further discussion with you on discord it seems that the issue is also in part about extending this in the future so I would advise packaging this up in to a class that you might add functions to, then can add to your "if __name__ == '__main__':" block additional functions as you add them, or through some other logic in a new "onStart()" function or something.



There's a great post on this here ...



Hope this helps.






share|improve this answer























    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    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: "196"
    ;
    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197211%2fsending-an-uptime-to-a-remote-server-through-ssh-with-the-method-pxssh%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    It sounds like the problem is not the code but how to package it up in a such a way that only functions that you want to call get called when you want to call them.



    A part of the solution is as you have defined it ...



    if __name__ == '__main__':
    conn_up()


    ... this would ensure that conn_up() gets called, but you could just treat conn_up as a constructor and pull all the code from it and put it in another function which you then call on demand.



    From further discussion with you on discord it seems that the issue is also in part about extending this in the future so I would advise packaging this up in to a class that you might add functions to, then can add to your "if __name__ == '__main__':" block additional functions as you add them, or through some other logic in a new "onStart()" function or something.



    There's a great post on this here ...



    Hope this helps.






    share|improve this answer



























      up vote
      2
      down vote



      accepted










      It sounds like the problem is not the code but how to package it up in a such a way that only functions that you want to call get called when you want to call them.



      A part of the solution is as you have defined it ...



      if __name__ == '__main__':
      conn_up()


      ... this would ensure that conn_up() gets called, but you could just treat conn_up as a constructor and pull all the code from it and put it in another function which you then call on demand.



      From further discussion with you on discord it seems that the issue is also in part about extending this in the future so I would advise packaging this up in to a class that you might add functions to, then can add to your "if __name__ == '__main__':" block additional functions as you add them, or through some other logic in a new "onStart()" function or something.



      There's a great post on this here ...



      Hope this helps.






      share|improve this answer

























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        It sounds like the problem is not the code but how to package it up in a such a way that only functions that you want to call get called when you want to call them.



        A part of the solution is as you have defined it ...



        if __name__ == '__main__':
        conn_up()


        ... this would ensure that conn_up() gets called, but you could just treat conn_up as a constructor and pull all the code from it and put it in another function which you then call on demand.



        From further discussion with you on discord it seems that the issue is also in part about extending this in the future so I would advise packaging this up in to a class that you might add functions to, then can add to your "if __name__ == '__main__':" block additional functions as you add them, or through some other logic in a new "onStart()" function or something.



        There's a great post on this here ...



        Hope this helps.






        share|improve this answer















        It sounds like the problem is not the code but how to package it up in a such a way that only functions that you want to call get called when you want to call them.



        A part of the solution is as you have defined it ...



        if __name__ == '__main__':
        conn_up()


        ... this would ensure that conn_up() gets called, but you could just treat conn_up as a constructor and pull all the code from it and put it in another function which you then call on demand.



        From further discussion with you on discord it seems that the issue is also in part about extending this in the future so I would advise packaging this up in to a class that you might add functions to, then can add to your "if __name__ == '__main__':" block additional functions as you add them, or through some other logic in a new "onStart()" function or something.



        There's a great post on this here ...



        Hope this helps.







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jun 26 at 17:40









        Billal BEGUERADJ

        1




        1











        answered Jun 26 at 15:42









        War

        1463




        1463






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197211%2fsending-an-uptime-to-a-remote-server-through-ssh-with-the-method-pxssh%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation