python

Python 3 Script to Perform Remote Commands Over SSH Client Protocol on Command Line Automatically Full Project For Beginners


 

 

I will refer you to paramiko

see this question

ssh = paramiko.SSHClient()
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute)

If you are using ssh keys, do:

k = paramiko.RSAKey.from_private_key_file(keyfilename)


ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, username=user, pkey=k)

 

 

Keep it simple. No libraries required.

import subprocess

subprocess.Popen("ssh {user}@{host} {cmd}".format(user=user, host=host, cmd='ls -l'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

 

 

I found paramiko to be a bit too low-level, and Fabric not especially well-suited to being used as a library, so I put together my own library called spur that uses paramiko to implement a slightly nicer interface:

import spur

shell = spur.SshShell(hostname="localhost", username="bob", password="password1")
result = shell.run(["echo", "-n", "hello"])
print result.output 

If you need to run inside a shell:

shell.run(["sh", "-c", "echo -n hello"])

 

 



import os
import sys
import select
import paramiko
import time


class Commands:
    def __init__(self, retry_time=):
        self.retry_time = retry_time
        pass

    def run_cmd(self, host_ip, cmd_list):
        i = 
        while True:
        
        try:
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(host_ip)
            break
        except paramiko.AuthenticationException:
            print("Authentication failed when connecting to %s" % host_ip)
            sys.exit(1)
        except:
            print("Could not SSH to %s, waiting for it to start" % host_ip)
            i += 1
            time.sleep(2)

        
        if i >= self.retry_time:
            print("Could not connect to %s. Giving up" % host_ip)
            sys.exit(1)
        
        
        for command in cmd_list:
            
            print "> " + command
            
            stdin, stdout, stderr = ssh.exec_command(command)
            
            
            while not stdout.channel.exit_status_ready():
                
                if stdout.channel.recv_ready():
                    rl, wl, xl = select.select([ stdout.channel ], [ ], [ ], 0.0)
                    if len(rl) > :
                        tmp = stdout.channel.recv(1024)
                        output = tmp.decode()
                        print output

        
        ssh.close()
        return

def main(args=None):
    if args is None:
        print "arguments expected"
    else:
        
        mytest = Commands()
        mytest.run_cmd(host_ip=args[], cmd_list=args[1])
    return


if __name__ == "__main__":
    main(sys.argv[1:])

Similar Posts:

    None Found

Leave a Reply

Your email address will not be published. Required fields are marked *