TechplexEngineer
12/16/2015 - 1:57 AM

RunRemoteCommands.py

import paramiko, time

def run_commands(ip_address, user, password, commandList, platform='', buffer=5000):
    """ this function runs the specified commands on the node and returns a
    list with unfiltered results.
    """
    print "Configuring " + ip_address
    remote_conn_pre = paramiko.SSHClient()
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    remote_conn_pre.connect(ip_address, username=user, password=password)
    remote_conn = remote_conn_pre.invoke_shell()
    if platform == "cisco":
        remote_conn.send("enable\n")
        time.sleep(1)
        remote_conn.send(password+'\n')
        time.sleep(1)
    commands = commandList.split('\n')
    for com in commands:
        remote_conn.send(com+'\n')
        time.sleep(1)
        output = remote_conn.recv(buffer)
        #print output

if __name__ == '__main__':
    run_commands('50.1.1.1', 'vyos', 'vyos', 'configure\nexit')