different behavior in Python shell and program - python

I'm using subprocess.Popen to instantiate an ssh-agent, add a key and push a git repository to a remote. To do this I string them together with &&. The code I'm using is
subprocess.Popen("eval $(ssh-agent) && ssh-add /root/.ssh/test_rsa && git push target HEAD", shell=True)
When I run this as a .py file I am prompted for the key's password. This seems to work as I get.
Identity added: /root/.ssh/test_rsa (/root/.ssh/test_rsa).
But when it tries to push the repository to the remote, an error occurs.
ssh: connect to host ***.***.***.*** port 22: Connection refused
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
However, if I simply run the same command in the interactive shell, it works. What causes this difference in behaviour, and what can I do to fix this?

The git server was on an aws instance that was being started earlier in the script. There was a check to make sure it was running, but aws seems to report an instance as running once boot has begun. This means that there is a brief time in which the instance is running, but an ssh daemon doesn't exist. Because the script moved very quickly into trying to push, it was falling within this time period and the server was refusing its connection attempt. By the time I would try anything in the interactive shell the instance was running long enough that it was working.
In short, aws says instances are running before the OS has started services.

Related

Using the ssh agent inside a python script

I'm pulling and pushing to a github repository with a python script. For the github repository, I need to use a ssh key.
If I do this manually before running the script:
eval $(ssh-agent -s)
ssh-add ~/.ssh/myprivkey
everything works fine, the script works. But, after a while, apparently the key expires, and I have to run those 2 cmds again
The thing is, if I do that inside the python script, with os.system(cmd), it doesn't work, it only works if I do that manually
I know this must be a messy way to use the ssh agent, but I honestly don't know how it works, and I just want the script to work, that's all
The script runs once an hour, just in case
While the normal approach would be to run your Python script in a shell where the ssh agent is already running, you can also consider an alternative approach with sshify.py
# This utility will execute the given command (by default, your shell)
# in a subshell, with an ssh-agent process running and your
# private key added to it. When the subshell exits, the ssh-agent
# process is killed.
Consider defining the ssh key path against a host of github.com in your ssh config file as outlined here: https://stackoverflow.com/a/65791491/14648336
If Linux then at this path ~/.ssh/ create a file called config and input something similar to in the above answer:
Host github.com
HostName github.com
User your_user_name
IdentityFile ~/.ssh/your_ssh_priv_key_file_name
This would save the need for starting an agent each time and also prevent the need for custom environment variables if using GitPython (you mention using Python) as referenced in some other SO answers.

Fabric 2 automating deployment error when git pulling on remote server. Repository not found

I'm trying to automate my deployment with Fabric 2.
When I manually do a git pull through the command line on the remote server everything works fine.
When I try to do the same with my Fabric/Invoke script it does not allow me to pull.
It does though allow me to do git status and other commands.
The code:
# Imports
from fabric import Connection
from fabric.tasks import task
import os
# Here i pass my local passphrase:
kwargs = {'passphrase': os.environ["SSH_PASSPHRASE"]}
#task
def serverdeploy(c, branch="Staging"):
con = Connection('myuser#myhost', connect_kwargs=kwargs)
with con.cd("/home/user/repository/"):
# Activate the virtual environment:
with con.prefix("source ENV/bin/activate"):
con.run("git pull origin {}".format(branch))
The results are:
git#bitbucket.org: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Notes:
I don't even get asked for a passphrase while doing the pull.
I have tried doing the pull without activating the environment but that didn't work either.
What could possibly be the problem?
Please place con.run("git pull origin {}".format(branch)) outside the with con.prefix("source ENV/bin/activate"):.
Your code has nothing to do with the interpreter or the virtual env! Try that and it should works!
The most likely issue is that the user that you log in as has the proper ssh key setup for bitbucket.org, but the fabric connection user is different. You can test whether the setup is correct by using these two commands as the user that fabric connects as:
ssh -T git#bitbucket.org
ssh -T -i /path/to/private_key git#bitbucket.org
In order to fix this issue, copy the private key to the /home/myuser/.ssh directory and add an ssh config entry for bitbucket.org to /home/myuser/.ssh/config:
Host bitbucket.org
IdentityFile /path/to/private_key

Modules appear to not be installed when running python through ssh

I am connecting to a remote server through
ssh user#server.com
and run
python script.py
in the appropriate directory. However, I get the error
ImportError: No module named numpy
even though I know the module is installed and the script runs with no problems when I am physically logged in to that server.
None of the answers I was able to find worked (for example this, and this). Do have any ideas as to how I can run the script using ssh?
The remote server has Python 2.6.6 installed, and
which python
returns
/usr/bin/python
The remote serves runs CentOS.
See similar problem describe here: Why does an SSH remote command get fewer environment variables then when run manually?.
Compare your environment variables in the local (physical) mode to the remote mode by running env in both cases. Move missing variables from your local profile to /etc/profile. Then log out from ssh session and connect again.
Another approach: If you don't want to change anything, then after ssh switch to your user via su - <your user>. This may look weird because you already logged it with this user. The difference is, that after su all your env. variables will set like in a local (physical) mode. Advantage: it is quick. Disadvantage: You will have to do it each time you want to run your Python script. So the first approach with configuring /etc/profile may be better on the long run.

WLST disconnect command issue

I had ran wlst.cmd in my local system after I ran my Weblogic Admin Instance. But as WLST is stateful, I am getting connected to my IT env which is my Integration Testing environment (some UNIX machine for my project). I tried disconnect() to goto offline mode, however it is failed.
wls:/beaProjDir/serverConfig> disconnect()
You will need to be connected to a running server to execute this command
Please help to go offline mode in WLST. As I need to get some work done in my local system.
You can try to getting out of WLST Shell ctrl+D. Which is similar to exiting from any other shell.

How to Use Python to execute initial commands over the terminal and then continue use

I have looked into using pxssh ,subprocess and paramiko but have found no success. What I am ultimately trying to do is figure out a way to not only use SSH to access a server and execute commands using a python script and finish there, but also have it open an instance of the terminal after executing all the commands for continued use.
Currently the server has modules that clients have to manually activate using commands after they have established an SSH connection.
For example:
module python
This command would give the user access to python.
Following this the user would then be able to use python and all its commands through the ssh connection in the terminal.
The issue I have with the methods listed earlier for executing these commands is that it does not display an instance of the terminal. It successfully executes the commands but since these commands have to be executed every time a new SSH connection is established it's worthless unless I can get essentially a copy of the terminal that the Python script executed and loaded up all the modules with.
Does any one have a solution to this? I've scoured the web for hours to no success.
This is a very difficult issue to explain so if anything is unclear please ask me and I will try my best to rephrase things. I am very new to all this.

Categories