Sending an uptime to a remote server through SSH with the method pxssh
Clash 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?
python python-3.x ssh
add a comment |Â
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?
python python-3.x ssh
1
This is an awkward library. Why do these two pieces of functionality belong together in the same program or library? Also, theconn_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
add a comment |Â
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?
python python-3.x ssh
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?
python python-3.x ssh
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, theconn_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
add a comment |Â
1
This is an awkward library. Why do these two pieces of functionality belong together in the same program or library? Also, theconn_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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited Jun 26 at 17:40
Billal BEGUERADJ
1
1
answered Jun 26 at 15:42
War
1463
1463
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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