Execute commands on local machine and on remote machine via ssh, and a wrapper for paramikos scp.
Wrapper around subprocess.call with shell=True
shell(cmd)
Runs a blocking unix command and returns the result
cmd_run(cmd, pr=False, streamoutput=True, returnoutput=True, cwd=None, prefix=None)
# example
cmd_run('date "+%Y-%m-%d% %H:%M"', pr=False, streamoutput=False, returnoutput=True)
# more params
call_command(command, cmdfolder=os.getcwd(), verbose=False, streamoutput=True,\
returnoutput=False, prefix=None, ret_and_code=False)
###Parameters:
command
: unix commandcmdfolder
=os.getcwd() -> working folder commandverbose
=False -> prints the commandstreamoutput
=True -> prints output to stdout (keeps buffering)returnoutput
=False -> return the buffered outputprefix
=None -> string to place before streaming outputret_and_code
=False -> return exit code also (code, val)
Common usecase scenario, run a command and get the result, possibly print to the console using an optional filter.
# example filter:
def onlyerrors(data):
if "ERROR" in data:
return data
cmd_exec(cmd, cmdtoprint=None, display=True, myfilter=onlyerrors)
cmdtoprint
: unix commanddisplay
: print to consolemyfilter
: function used to print
Uses ssh and key authentication to logon to a remote ssh server and execute a command there.
def remote_cmd(server, cmd, username=None, timeout=60, keypath=None):
#example
remote_cmd("localhost", "rm -Rf ~/Desktop/foobar")
###Parameters:
server
: ip or domain name of servercmd
: unix command to executeusername
: username used to logintimeout
: try time to connect to serverkeypath
: path to the public key of username
remote_cmd_map(servercmd)
servercmd
: tuple with ( server, cmd, username, keypath )
scp_get(server, fp1, fp2, username=None, keypath=None)
scp_put(server, fp1, fp2, username=None, keypath=None)
###Parameters:
server
: ip or domain name of serverfp1
: source filepathfp2
: target filepathusername
: username used to loginkeypath
: path to the public key of username
Invoke a shell on a machine
invoke_shell(server, username, keypath)
###Parameters:
server
: ip or domain name of serverusername
: username used to loginkeypath
: path to the public key of username
Wrapper around the requests library. Downloads a file with a progress bar.
download(url, mypath):
url
: url to downloadmypath
: filepath where to create the downloaded file
download: http://download.thinkbroadband.com/5MB.zip
00:00:02|███████████████████████ | 3.7Mi/5.0Mi