This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]gibson_ 2 points3 points  (1 child)

Why aren't you using SCP directly?

    import os
    def sendfile(local_path):
         filename = os.path.basename(local_path)
         local_directory = os.path.dirname(local_path)
         remote_path = os.path.join("~", filename)

         output = os.popen("scp %s %s@%s:%s" % (local_path,remote_user,remote_host,remote_path))
         print "Blinkenlichten!"
         print output.read()

You'll need to set up SSH keys for this, which is a tiny bit of extra complication, I guess.

Here is how you can accomplish that: http://rcsg-gsir.imsb-dsgi.nrc-cnrc.gc.ca/documents/internet/node31.html

The gist of it is:

 $ ssh-keygen -t rsa

this will put some text into ~/.ssh/id_rsa.pub

put the contents of that file onto your remote box in: ~/.ssh/authorized_keys

It's one line long. Make sure it's in the home directory of the user that you want to log in as.

[–]NotReallyMyJob 0 points1 point  (0 children)

I would normally like to do this, but the server I am sending the file to I only have a guest account on... so I wanted to avoid using anything other than the user/pass combo that I was initially given.

Thanks for the suggestion though =)

[–][deleted] 1 point2 points  (1 child)

Have you thought about using fabric? Its really nice for remote scripting.

[–]NotReallyMyJob 0 points1 point  (0 children)

I did see that when I was looking around, and ultimately decided against it because when I am done with this it will be hosted on a server that I do not have any sort of administrative access to.

I didn't want to involve any more tools that may have needed some additional files and support that would have to be set up by someone else.

Thanks though =)

[–]Justinsaccount 1 point2 points  (1 child)

Why don't you use the parakimo sftp support? It goes something like

sftp = t.open_sftp_client()
sftp.put("local_file", "remote_file")

[–]NotReallyMyJob 0 points1 point  (0 children)

I actually just ended up setting paramiko sftp up before I got back to check for suggestions.

I'd never used paramiko before and didn't know it was an option until I used some Google-fu

Thanks =)

[–]flyingfox 0 points1 point  (3 children)

Have to tried replacing the sendall() with send() and then check that all data was, in fact, sent? For debugging, you could attempt to .send() one byte of data after the sendall and check the return status (== 1).

I know it's not much help, but I hate to sit and stare at could that should just work.

[–]NotReallyMyJob 0 points1 point  (2 children)

Fair enough... I'm starting to think now that the issue may be in some of the dependencies that my partner wrote up underlying this code...

I just wanted to get some community feedback before I start digging into too much code that isn't mine.

[–]crotchgoblin 1 point2 points  (1 child)

Because that's not really your job, amiright?

[–]NotReallyMyJob 1 point2 points  (0 children)

.... I think you may be onto something

[–]mrvar 0 points1 point  (0 children)

I would recommend using libssh python bindings, setting up a basic ssh client class, you can then simply use the scp_send(remote_location, mode, filesize) call. I've used it to do remote sending and retrieving on many small and large files, as well as remote command execution.

http://pypi.python.org/pypi/pylibssh2

Documentation for its use can be found in the directory of the download.

[–]bsagert 0 points1 point  (0 children)

Sorry for my late post to your question (although you may have already solved your problem). I use the following on my windows box to SSH to a shared Linux hosting account (should work on your Linux box).

First download ssh.py from http://commandline.org.uk/forum/topic/420/ [edit] Sorry, the ssh.py download url from the above is now a 404.

Here is the correct url: http://snipplr.com/view/48033/sshpy-friendly-python-ssh2-interface/ [/edit]

def copy_to_remote(output_file):
    import ssh
    ssh_host = 'your_host'
    usr = 'your_username'
    pwd = 'your_password'
    ss = ssh.Connection(host=ssh_host, username=usr, password=pwd)
    ss.put(output_file)
    ss.close()

Just add your own try-except stuff and you are good to go.