Skip to main content
added 2 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Send Commands Overcommands over SSH to Server - Pythonserver

Here's a very small and basic script that sends commands over sshSSH to another computer on my network:

#!/bin/python
import sys, os
commands = ""
for i in sys.argv[1:]:
        commands += " " + i;

if len(sys.argv) <= 1:
        os.system("sshpass -p VerySecrectPassword ssh [email protected]")
else:
        os.system("sshpass -p VerySecrectPassword ssh [email protected]" + commands)

Am I being very "Pythonic"? Is it better to use a for loop to join all the commands or the join function?

commands = " ".join(sys.argv[1:])

Personally, I think the for loop reads better, but that's just my opinion and I'm a newbie. Which is considered the better way to do things?

Furthermore:

  • Are there any other areas in this script that could be improved?
  • Is it a bad idea to store the password in the script (located at /usr/bin)?

Note: This script is stored on a computer where I am the only user. (Except root)Note: This script is stored on a computer where I am the only user (except root).

Send Commands Over SSH to Server - Python

Here's a very small and basic script that sends commands over ssh to another computer on my network:

#!/bin/python
import sys, os
commands = ""
for i in sys.argv[1:]:
        commands += " " + i;

if len(sys.argv) <= 1:
        os.system("sshpass -p VerySecrectPassword ssh [email protected]")
else:
        os.system("sshpass -p VerySecrectPassword ssh [email protected]" + commands)

Am I being very "Pythonic"? Is it better to use a for loop to join all the commands or the join function?

commands = " ".join(sys.argv[1:])

Personally, I think the for loop reads better, but that's just my opinion and I'm a newbie. Which is considered the better way to do things?

Furthermore:

  • Are there any other areas in this script that could be improved?
  • Is it a bad idea to store the password in the script (located at /usr/bin)

Note: This script is stored on a computer where I am the only user. (Except root)

Send commands over SSH to server

Here's a very small and basic script that sends commands over SSH to another computer on my network:

#!/bin/python
import sys, os
commands = ""
for i in sys.argv[1:]:
        commands += " " + i;

if len(sys.argv) <= 1:
        os.system("sshpass -p VerySecrectPassword ssh [email protected]")
else:
        os.system("sshpass -p VerySecrectPassword ssh [email protected]" + commands)

Am I being very "Pythonic"? Is it better to use a for loop to join all the commands or the join function?

commands = " ".join(sys.argv[1:])

Personally, I think the for loop reads better, but that's just my opinion and I'm a newbie. Which is considered the better way to do things?

Furthermore:

  • Are there any other areas in this script that could be improved?
  • Is it a bad idea to store the password in the script (located at /usr/bin)?

Note: This script is stored on a computer where I am the only user (except root).

Source Link
user2635139
  • 337
  • 1
  • 3
  • 14

Send Commands Over SSH to Server - Python

Here's a very small and basic script that sends commands over ssh to another computer on my network:

#!/bin/python
import sys, os
commands = ""
for i in sys.argv[1:]:
        commands += " " + i;

if len(sys.argv) <= 1:
        os.system("sshpass -p VerySecrectPassword ssh [email protected]")
else:
        os.system("sshpass -p VerySecrectPassword ssh [email protected]" + commands)

Am I being very "Pythonic"? Is it better to use a for loop to join all the commands or the join function?

commands = " ".join(sys.argv[1:])

Personally, I think the for loop reads better, but that's just my opinion and I'm a newbie. Which is considered the better way to do things?

Furthermore:

  • Are there any other areas in this script that could be improved?
  • Is it a bad idea to store the password in the script (located at /usr/bin)

Note: This script is stored on a computer where I am the only user. (Except root)