Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python script to ssh to server and run command

#!/usr/bin/python

import os
import sys
import select
import paramiko
import time


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

    def run_cmd(self, host_ip, cmd_list):
        i = 0
        while True:
        # print("Trying to connect to %s (%i/%i)" % (self.host, i, self.retry_time))
        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 we could not connect within time limit
        if i >= self.retry_time:
            print("Could not connect to %s. Giving up" % host_ip)
            sys.exit(1)
        # After connection is successful
        # Send the command
        for command in cmd_list:
            # print command
            print "> " + command
            # execute commands
            stdin, stdout, stderr = ssh.exec_command(command)
            # TODO() : if an error is thrown, stop further rules and revert back changes
            # Wait for the command to terminate
            while not stdout.channel.exit_status_ready():
                # Only print data if there is data to read in the channel
                if stdout.channel.recv_ready():
                    rl, wl, xl = select.select([ stdout.channel ], [ ], [ ], 0.0)
                    if len(rl) > 0:
                        tmp = stdout.channel.recv(1024)
                        output = tmp.decode()
                        print output

        # Close SSH connection
        ssh.close()
        return

def main(args=None):
    if args is None:
        print "arguments expected"
    else:
        # args = {'<ip_address>', <list_of_commands>}
        mytest = Commands()
        mytest.run_cmd(host_ip=args[0], cmd_list=args[1])
    return


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

python how to run code in ssh

stdout = client.exec_command('python -c "exec("' + open('hello.py','r').read().encode('base64').strip('
') + '".decode("base64"))"' )[1]
Comment

PREVIOUS NEXT
Code Example
Python :: database with python connection 
Python :: python using secrets 
Python :: Delete All Rows In Table Django 
Python :: Generate bar plot python 
Python :: convert rgb image to binary in pillow 
Python :: how to create a subset of two columns in a dataframe 
Python :: loop in python 
Python :: code example of sum of the first n odd numbers using for loop 
Python :: use a library in python 
Python :: os.filename 
Python :: python string replace by index 
Python :: python hash 
Python :: amazon redshift 
Python :: tkinter radio button default selection 
Python :: phyton datetime comparison 
Python :: random list generator 
Python :: python code for internet radio stream 
Python :: what is the size of cover in facebook 
Python :: How to perform topological sort of a group of jobs, in Python? 
Python :: python TypeError: function takes positional arguments but were given 
Python :: python test coverage 
Python :: re.sub 
Python :: __str__ returned non-string (type User) 
Python :: push button raspberry pi 
Python :: How to check the number of occurence of each character of a given string in python 
Python :: purpose of migration folder in django 
Python :: euclidean distance 
Python :: python add list 
Python :: datetime convert python 
Python :: summing all Odd Numbers from 1 to N 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =