file transfer question
Robin Munn
rmunn at pobox.com
Tue Apr 29 13:03:30 EDT 2003
Ali Dada <afd00 at aub.edu.lb> wrote:
> hi all:
>
> i want to read a file from a network pc and use it in a local python script. i
> thought of an easy way: i log on from within the python script to the host pc
> using ssh then copy the file to my working directory using scp. below is the code:
>
> import os
> os.popen('ssh username at hostname')
> os.popen('scp username at hostname:~/requiredFile ./copiedFile')
>
> but what's happening is that when i execute the code, i get prompted for the
> password by ssh, so i enter it, and then the program freezes and i can do
> nothing. how do you think i should alter the code? thanks in advance..
Why are you using os.popen() for this -- and why are you throwing its
return code away? os.popen() creates a pipe to the program you're
running so that you can read its output. It returns the opened pipe as a
file-like object, so that you can call read() and close() on it. But you
don't need to read the output from ssh or from scp; you should use
os.system() instead.
Furthermore, you don't need to call ssh here at all; scp will do the
logging-in required. Try:
import os
os.system('scp username at hostname:~/requiredFile ./copiedFile')
You should still get prompted for a password.
BTW, if you want to automate this script, you might need to get rid of
the password prompting; look into SSH's features for public/private SSH
keys. The manpage for ssh-keygen might be a good place to start.
--
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838 50FF 2478 CFFB 081A 8338 54F7 845D ACFD 6AFB 6838
More information about the Python-list
mailing list