Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (20.3k points)

I'm writing a script to automate some command line commands in Python. At the moment I'm doing calls thus:

cmd = "some unix command"

retcode = subprocess.call(cmd,shell=True)

However, I need to run some commands on a remote machine. Manually, I would log in using ssh and then run the commands. How would I automate this in Python? I need to log in with a (known) password to the remote machine, so I can't just use cmd = ssh user@remotehost, I'm wondering if there's a module I should be using?

2 Answers

0 votes
by (40.7k points)

Try using the code given below: 

ssh = paramiko.SSHClient()

ssh.connect(server, username=username, password=password)

ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute)

If you wish to learn more about Python, visit the Python tutorial and Python course by Intellipaat. 

0 votes
by (106k points)

Use the below code to perform over ssh in Python:-

import spur 

shell = spur.SshShell(hostname="localhost", username="bob", password="password1") 

result = shell.run(["echo", "-n", "hello"]) 

print result.output 

Related questions

0 votes
1 answer
asked Dec 22, 2020 in Python by laddulakshana (16.4k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...