Skip to content

Instantly share code, notes, and snippets.

@justAnotherDev
Last active August 29, 2015 14:25
Show Gist options
  • Save justAnotherDev/aee9b759f932bc89df59 to your computer and use it in GitHub Desktop.
Save justAnotherDev/aee9b759f932bc89df59 to your computer and use it in GitHub Desktop.
Python SCP/SSH Commands
#!/usr/bin/python2.4
import string
from string import rstrip
import subprocess
from subprocess import Popen, PIPE, call
from time import time
import os
# copy a folder to a remote folder
def scpFolderFromHostToFolderPathOnHost(fromFolderPath, fromHost, toFolderPath, toHost, asRoot):
# add hostname before the fromFolderPath if need be
if len(fromHost) and not '127.0.0.1' in fromHost:
fromFolderPath = fromHost + ':' + fromFolderPath
# add hostname before the toFolderPath if need be
if len(toHost) and not '127.0.0.1' in toHost:
toFolderPath = toHost + ':' + toFolderPath
if asRoot:
toFolderPath = 'root@' + toFolderPath
command = "scp -qr " + fromFolderPath + " " + toFolderPath + '/'
os.system(command)
# open a ssh tunnel
# returns the process
def sshIntoMachineAsUser(host, username):
command = "ssh -T " + username + "@" + host;
# open tunnel
process = Popen(command, shell=True, stderr=PIPE, stdin=PIPE, stdout=PIPE)
# load bash profile
process.stdin.write("source ~/.bash_profile\n")
return process
# close the ssh tunnel
def closeSSH(process):
process.stdin.close()
process.wait()
def removeFolderFromHost(folderPath, hostname):
# open ssh tunnel
process = sshIntoMachineAsUser(hostname, 'root')
# navigate to depot
process.stdin.write("rm -r " + folderPath + "\n")
closeSSH(process)
def respringHost(hostname):
process = sshIntoMachineAsUser(hostname, 'root')
process.stdin.write("respring\n")
closeSSH(process)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment