Created
June 4, 2015 06:14
-
-
Save therealmik/f09d427bc062e0b6d329 to your computer and use it in GitHub Desktop.
For when you just want to dump some data on a remote sftp server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
BUFSIZE=4096 | |
import sys | |
import paramiko | |
import re | |
def parse_url(urlstr): | |
urlre = re.compile("^(([^@:]+)@)?([^:]+):(.*)$") | |
match = urlre.match(urlstr) | |
if match is None: | |
print >>sys.stderr, "URL must be in form [username@]host:path" | |
sys.exit(1) | |
username = match.group(2) | |
hostname = match.group(3) | |
path = match.group(4) | |
return (hostname, username, path) | |
def copy(fromfd, tofd): | |
while True: | |
buf = fromfd.read(BUFSIZE) | |
if buf == "": | |
return | |
tofd.write(buf) | |
def main(): | |
if len(sys.argv) != 2: | |
print >>sys.stderr, "Usage: %s <url>" % sys.argv[0] | |
sys.exit(1) | |
hostname, username, path = parse_url(sys.argv[1]) | |
client = paramiko.client.SSHClient() | |
client.load_system_host_keys() | |
client.set_missing_host_key_policy(paramiko.client.RejectPolicy()) | |
client.connect(hostname, username=username) | |
sftp = client.open_sftp() | |
with sftp.open(path, "wx", bufsize=BUFSIZE) as remotefd: | |
copy(sys.stdin, remotefd) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment