Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run shell script from python with permissions

I have the most simple script called update.sh

#!/bin/sh
cd /home/pi/circulation_of_circuits
git pull

When I call this from the terminal with ./update.sh I get a Already up-to-date or it updates the files like expected.

I also have a python script, inside that scipt is:

subprocess.call(['./update.sh'])

When that calls the same script I get:

Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

(I use SSH).

----------------- update --------------------

Someone else had a look for me:

OK so some progress. When I boot your image I can't run git pull in your repo directory and the bash script also fails. It seems to be because the bitbucket repository is private and needs authentication for pull (the one I was using was public so that's why I had no issues). Presumably git remembers this after you type it in the first time, bash somehow tricks git into thinking it's you typing the command subsequently but running it from python isn't the same.

I'm not a git expert but there must be some way of setting this up so python can provide the authentication.

like image 560
clankill3r Avatar asked Mar 05 '17 22:03

clankill3r


People also ask

How do I run a shell script in Python?

If you need to execute a shell command with Python, there are two ways. You can either use the subprocess module or the command. run() function. The first option is easier to run one line of code and exit, but it isn't as flexible when using arguments or producing text output.

Does a Python script needs execution permissions to run?

Unlike a CGI script, Python scripts do not need to have execute permissions to run when you invoke them with the python command. Python is an interpreted language, which means that if you call the file with python [filename], Python itself handles the execution. The file only needs to be readable by Python.

Can I use Python as shell script?

Without much real work, it's easy to replace shell scripts with Python code. The revised code is easier to read and maintain, runs a little faster, and can have a proper unit test suite. Because shell code is so common, I'll provide some detailed examples of how to translate legacy shell scripts into Python.


4 Answers

sounds like you need to give your ssh command a public or private key it can access perhaps:

ssh -i /backup/home/user/.ssh/id_dsa [email protected]

-i tells it where to look for the key

like image 51
jimh Avatar answered Nov 09 '22 04:11

jimh


This problem is caused by the git repo authentication failing. You say you are using SSH, and git is complaining about publickey auth failing. Normally you can use git commands on a private repo without inputting a password. All this would imply that git is using ssh, but in the latter case it cannot find the correct private key.

Since the problem only manifests itself when run through another script, it is very likely caused by something messing with the environment variables. Subprocess.call should pass the environment as is, so there are a couple of usual suspects:

  1. sudo.
    • if you are using sudo, it will pass a mostly empty environment to the process
  2. the python script itself
    • if the python script changes its env, those changes will get propagated to the subprocess too.
  3. sh -lor su -
    • these commands set up a login shell, which means their environment gets reset to defaults.

Any of these reasons could hide the environment variables ssh-agent (or some other key management tool) might need to work.

Steps to diagnose and fix:

  1. Isolate the problem.

    • Create a minimal python script that does nothing else than runs subprocess.call(['./update.sh']). Run both update.sh and the new script.
  2. Diagnose the problem and fix accordingly:

    a) If update.sh works, and the new script doesn't, you are probably experiencing some weird corner case of system misconfiguration. Try upgrading your system and python; if the problem persists, it probably requires additional debugging on the affected system itself.

    b) If both update.sh and the new script work, then the problem lies within the outer python script calling the shell script. Look for occurrences of sudo, su -, sh -l, env and os.environ, one of those is the most likely culprit.

    c) If neither the update.sh nor the new script work, your problem is likely to be with ssh client configuration; a typical cause would be that you are using a non-default identity, did not configure it in ~/.ssh/config but used ssh-add instead, and after that, ssh-agent's cache expired. In this case, run ssh-add identityfile for the identity you used to authenticate to that git repo, and try again.

like image 32
Bass Avatar answered Nov 09 '22 06:11

Bass


I believe this answer will help you: https://serverfault.com/questions/497217/automate-git-pull-stuck-with-keychain?answertab=votes#tab-top

I didn't use ssh-agent and it worked: Change your script to the one that follows and try.

#!/bin/bash
cd /home/pi/circulation_of_circuits 
ssh-add /home/yourHomefolderName/.ssh/id_rsa
ssh-add -l
git pull

This assumes that you have configured correctly your ssh key.

like image 3
John Moutafis Avatar answered Nov 09 '22 06:11

John Moutafis


It seems like your version control system, need the authentication for the pull so can build the python with use of pexpect,

import pexpect
child = pexpect.spawn('./update.sh')
child.expect('Password:')
child.sendline('SuperSecretPassword')
like image 3
Rahul K P Avatar answered Nov 09 '22 04:11

Rahul K P