#!/usr/bin/env python # Non-Interactive ssh Connection # $Id: tsshbatch.py,v 1.102 2010/12/14 18:47:50 tundra Exp $ import paramiko import sys ##### # Constants And Literals ##### SEPARATOR = " ---> " USAGE = "Usage: sshlogin.py username password hostname command" ##### # Print Message(s) To stdout ##### # Expects an iterable object as input def PrintStdout(messages, TERMINATOR="\n"): for msg in messages: sys.stdout.write(msg + TERMINATOR) # End of 'PrintStdout()' ##### # Display An Error Message And Exit ##### def ErrorExit(msg): PrintStdout([msg]) sys.exit() # End Of 'ErrorExit()' ##### # Program Entry Point ##### # Unpack command line arguments if len(sys.argv[1:]) == 4: user, pw, host, cmd = sys.argv[1:] # Otherwise let the user know else: ErrorExit(USAGE) ssh = paramiko.SSHClient() PrintStdout(host + SEPARATOR, TERMINATOR="") ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=user, password=pw) stdin, stdout, stderr = ssh.exec_command(cmd) PrintStdout(stdout)