views:

163

answers:

3

Hi, I would like to write a script that will tell another server to SVN export a SVN repository.

This is my python script:

import os

# svn export to crawlers
for s in ['work1.main','work2.main']:
    cmd = 'ssh %s "cd /home/zes/ ; svn --force export svn+ssh://174.113.224.177/home/svn/dragon-repos"' % s
    print cmd
    os.system(cmd)

Very simple. It will ssh into work1.main, then cd to a correct directory. Then call SVN export command.

However, when I run this script...

$ python export_to_crawlers.py
ssh work1.main "cd /home/zes/ ; svn --force export svn+ssh://174.113.224.177/home/svn/dragon-repos"
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-with-mic,password).
svn: Connection closed unexpectedly
ssh work2.main "cd /home/zes/ ; svn --force export svn+ssh://174.113.224.177/home/svn/dragon-repos"
Host key verification failed.
svn: Connection closed unexpectedly

Why do I get this error and cannot export the directory? I can manually type the commands in the command line and it will work. Why can't it work in the script?

If I change to this...it will not work. and instead, nothing will happen.

cmd = 'ssh %s "cd /home/zes/ ;"' % s
A: 

I guess that it is related to ssh. Are you using a public key to automatically connect. I think that your shell knows this key but it is not the case of python.

I am not sure but it's just an idea. I hope it helps

luc
+1  A: 

This is a problem with SSH.

Permission denied, please try again.

This means that ssh can't login. Either your ssh agent doesn't have the correct key loaded, you're running the script as a different user or the environment isn't passed on correctly. Check that the variables SSH_AUTH_SOCK and SSH_AGENT_PID are passed to the subprocess of your python script.

Host key verification failed.

This error means that the remote host isn't known to ssh. This means that the host key is not found in the file $HOME/.ssh/known_hosts. Again, make sure that you're checking the home directory of the effective user of the script.

[EDIT] When you run the script, then python will become the "input" of ssh: ssh is no longer connected to a console and will ask python for the password to login. Since python has no idea what ssh wants, it ignores the request. ssh tries three times and dies.

To solve it, run these commands before you run the Python script:

eval $(ssh-agent)
ssh-add path-to-your-private-key

Replace path-to-your-private-key with the path to your private key (the one which you use to login). ssh-add will ask for your password and the ssh-agent will save it in a secure place. It will also modify your environment. So when SSH runs the next time, it will notice that an ssh agent is running and ask it first. Since the ssh-agent knows the password, ssh will login without bothering Python.

To solve the second issue, run the second ssh command manually once. ssh will then add the second host to its files and won't ask again.

[EDIT2] See this howto for a detailed explanation how to login on a remote server via ssh with your private key.

Aaron Digulla
How do I solve the "Permission denied, please try again" problem? What do you mean "passed to the subprocess of your python script"? Thanks
TIMEX
The remote side expects a password. How do you pass it to ssh when you run the command manually?
Aaron Digulla
First, I ssh into work1.main. Then, I cd into the directory. then I paste "svn --force export svn+ssh://174.113.224.177/home/svn/dragon-repos" Once that happens, it will ask me for a password to the SVN server. When that happens, I enter the password, and it will export. THis is when I do it manually.
TIMEX
Okay, see my edits how to solve it.
Aaron Digulla
Hi Aaron. I did those steps. and it seems like it was added."Identity added: /home/zes/.ssh/id_dsa (/home/zes/.ssh/id_dsa)" that's the output after ssh-add. Now, when I run the script...the same thing happens?
TIMEX
Do you login with password or with private key? Is ssh asking for your passphrase or for the password of the remote user?
Aaron Digulla
I've added a link to a howto which explains how to setup ssh for remote login with the passphrase.
Aaron Digulla
A: 

Check out the pxssh module that is part of the pyexpect project:

http://pexpect.sourceforge.net/pxssh.html

It simplifies dealing with automating ssh-ing into machines.

bismark