subprocess Popen

Posted on 2011-10-20
Last Modified: 2012-05-12
This is driving me crazy as I cant figure out why it wont work. I have this very simple logic that once I can get working, I will start building more complex pieces:

process = Popen(['scp -i keyfile' ,'localpath', 'user@device:/remotepath'],  stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()

no matter how many ways I try to use the -i flag for the indentity to use for scp I get this:
Traceback (most recent call last):
  File "./test.cgi", line 11, in <module>
  File "/usr/lib/python2.7/", line 672, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/", line 1213, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

If I run it without the -i flag, it works but I am prompted for password. If I run the exact same command (the scp portion for the Linux command line, it works fine... Is there some special way to deal with passing flags to the system binary being used with Popen?

I had used this same method way back and had working, but just cant seem to get it right here. Anyone have any pointers.

Question by:andrew_89
    LVL 16

    Expert Comment

    Few questions.

    Shouldn't you write:

    process = Popen(['scp', '-i',  'keyfile' ,'localpath', 'user@device:/remotepath'],  stdout=PIPE, stderr=PIPE)

    instead of:

    process = Popen(['scp -i keyfile' ,'localpath', 'user@device:/remotepath'],  stdout=PIPE, stderr=PIPE)

    LVL 16

    Accepted Solution

    Some more coments:

    Make sure, (one of the below options is true)
    - keyfile is an absolute paht name
    - the python script is started in the directory, where keyfile is located
    - the python script performs an os.chdir() to the right directory before running scp

    Do not use '~/.ssh/id_xxx' as file name. I think this would not be expanded.
    use better os.path.join(os.environ['HOME'], '.ssh/id_xxx')

    You might considerusing the option '-q' in order to get rid of the prgress bar

    process = Popen(['scp', '-i',  'keyfile' , '-q', 'localpath', 'user@device:/remotepath'],  stdout=PIPE, stderr=PIPE)

    LVL 1

    Author Comment

    thanks that gets rid of the error but now of course I still get password prompt. That will  end up being another issue that I can debug separately.

    LVL 16

    Expert Comment

    Two ways of getting rid of the password:
    - ssh-agent can store the ssh passwords for you, so you don't have to enter them multiple times.

    just run ssh-agent
    and ssh-add prior to running your script.

    Alternatively your python script could prompt for the password.

    and you could use Pexpect (under linux) to send the password to scp

    For more details it's probably best to open a new question.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
    Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now