Skip to content

Instantly share code, notes, and snippets.

@xiaowl
xiaowl / pyssh.py
Created July 9, 2012 10:22
ssh login using paramiko.
def ssh(hostname, keyfile, username='ubuntu'):
host_key_file = os.path.expanduser('~/.ssh/known_hosts')
pkey = paramiko.RSAKey.from_private_key_file(os.path.expanduser(keyfile))
client = paramiko.SSHClient()
client.load_system_host_keys()
client.load_host_keys(host_key_file)
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username=username, pkey=pkey)
return client
@xiaowl
xiaowl / pyscp.py
Created July 9, 2012 10:20
Pure python implementation of `scp`, using SFTP protocol. Depends on paramiko.
def scp(sshclient, src, dest=''):
path = os.path.abspath(os.path.expanduser(src)).rstrip('/')
basename = os.path.basename(path)
ftp = sshclient.open_sftp()
if os.path.isdir(path):
for root, dirs, files in os.walk(path):
root = root.rstrip('/')
dirname = os.path.basename(root)
if root != path:
dirname = os.path.join(basename, root.split(path)[1].strip('/'))