#!/usr/bin/env python

import argparse
import logging
import time
import paramiko

SSH_PORT = 22

logging.basicConfig(#level=logging.DEBUG,
                    format='[%(levelname)s] [%(asctime)s] %(message)s',
                    datefmt='%Y-%m-%d %H:%M')

def get_args():
    parser = argparse.ArgumentParser()

    parser.add_argument('host')
    parser.add_argument('password')
    parser.add_argument('command', nargs='+')

    args = parser.parse_args()
    
    if '@' in args.host:
        args.username, args.host = args.host.split('@')
    else:
        args.username = None
    
    if ':' in args.host:
        args.host, args.port = args.host.split(':')
    else:
        args.port = SSH_PORT
    
    return args

args = get_args()

host, port = args.host, args.port
username, password = args.username, args.password
command = ' '.join(args.command)

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, port=port, username=username, password=password)
shell = client.invoke_shell()

flush = shell.recv(1024)
logging.debug(flush)

shell.send(command.encode('ascii') + b'\r\n')
time.sleep(0.5) # give time for embedded processor to recv/send over shell

if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
    logging.debug(shell.recv(8192))

shell.shutdown_write()
shell.shutdown_read()
client.close()