Fabric: How to edit file across machines? - python

Is there a way to use Fabric to open editor and set text?
I have to set new keys on 50+ servers and not willing to do it manually.

okay create .aws directory in your local machine and put whatever contents you want to copy.
install sshpass
run this command over a loop changing hostname and password
sshpass -p abcpass rsync -arvce "ssh -o StrictHostKeyChecking=no" ./* user#ip:/home/user/.aws

Related

Remote environment variable from file with Fabric

I am trying to just do a simple echo $MYVAR on a remote server with Fabric.
The environment variable is in my ~/.bashrc file on the remote host.
I have tried:
run("source /home/<myusername>/.bashrc && echo $MYVAR")
This just prints an empty string. Running it when logged in on the remote machine prints "5", the value in the bashrc file. Does anyone know why this would be?
I need the environment variable to be set from a file on the remote host, and not decided by Fabric.
I am running the ssh commands as <myusername>, so the username seems correct. This seems like a duplicate of this question, except that maybe they were running things as the wrong user. I also tried the shell argument to run, with no luck.
I haven't tried the various context manager-ish things, as they just seem to be a shorthand for command1 && command2.
First, as explained in the answer you link to, fabric uses a login shell, not and interactive shell, meaning your ~/.bahsrc is not going to be source'd on the remote. You should export your variable in your ~/.bash_profile or ~/.profile.
Without the relevant content of your remote ~/.bashrc it's impossible to tell why your command fails.

How to run git fetch over ssh in a windows subprocess

I've got some code which needs to grab code from github periodically (on a Windows machine).
When I do pulls manually, I use GitBash, and I've got ssh keys running for the repos I check so everything is fine. However when I try to run the same actions in a python subprocess I don't have the ssh services which GitBash provides and I'm unable to authenticate to the repo.
How should I proceed from here. I can think of a couple of different options:
I could revert to using https:// fetches. This is problematic because the repos I'm fetching use 2-factor authentication and are going to be running unattended. Is there a way to access an https repo that has 2fa from a command line?
I've tried calling sh.exe with arguments that will fire off ssh-agent and then issuing my commands so that everything is running more or less the way it does in gitBash, but that doesn't seem to work:
"C:\Program Files (x86)\Git\bin\sh.exe" -c "C:/Program\ Files\ \(x86\)/Git/bin/ssh-agent.exe; C:/Program\ Files\ \(x86\)/Git/bin/ssh.exe -t git#github.com"
produces
SSH_AUTH_SOCK=/tmp/ssh-SiVYsy3660/agent.3660; export SSH_AUTH_SOCK;
SSH_AGENT_PID=8292; export SSH_AGENT_PID;
echo Agent pid 8292;
Could not create directory '/.ssh'.
The authenticity of host 'github.com (192.30.252.129)' can't be established.
RSA key fingerprint is XXXXXXXXXXX
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/.ssh/known_hosts).
Permission denied (publickey).
Could I use an ssh module in python like paramiko to establish a connection? It looks to me like that's only for ssh'ing into a remote terminal. Is there a way to make it provide an ssh connection that git.exe can use?
So, I'd be grateful if anybody has done this before or has a better alternative
The git bash set the HOME environment variable, which allows git to find the ssh keys (in %HOME%/.ssh)
You need to make sure the python process has or define HOME to the same PATH.
As explained in "Python os.environ[“HOME”] works on idle but not in a script", you need to set HOME to %USERPROFILE% (or, in python, to os.path.expanduser("~") ).

How do i give ssh keys for multiple hosts from terminal command line in fabric

I want to test various package installation on multiple hosts. Different hosts have different password/ssh-key.
I dont want to hard code host name and their ssh-key in my fab file. How can i pass multiple host and their ssh-key through terminal command line.
Code in my fab file looks like -
from fabric.api import settings, run, env
def test_installation(cmd):
run("dpkg -s %s" %cmd)
And i am calling it like -
fab test_installation:tomcat7 --hosts "user1#host1:port","vuser2#host2:port" -i "ssh-file-path for host1","ssh-file-path for host2"
Please suggest me the proper way. any help is most welcomed.
You don't provide ssh keys of hosts, but only your ssh key, that is used to register you in authorized_keys on host. And you provide only path to it (usually it is ~/.ssh/id_rsa).
Moreover, you can configure fabric to use your ssh config, so you don't need to hardcode any path at all. It can use the same keys, as it would use if you typed ssh my_host in shell.
How to do that you can find in fabric tutorial:
http://docs.fabfile.org/en/1.8/usage/execution.html#leveraging-native-ssh-config-files
http://docs.fabfile.org/en/1.8/usage/env.html#full-list-of-env-vars
You can also set your ~/.ssh/config to use different key for different host.
If you are not familiar with ssh and configuring it, please see:
http://linux.die.net/man/5/ssh_config

Execute a command on remote machine using telnet in a single line

I would like to execute a command on a remote machine using telnet as,
telnet x.x.x.x command or similar
I want to include this as part of a script in Python ( subprocess ) and hence need it in one line. Or, are there any other ways to do the same?
Any help is appreciated.
Thanks.
Rather than relying on subprocess, you could try Python's built-in telnetlib instead.
A complete example that does almost exactly what you want is available as an example in the documentation: http://docs.python.org/library/telnetlib.html#telnet-example
I would personally also see if SSH is available on the target system. Not only will you be using a secure connection, but you can also set up SSH keys and use SSH's built-in support for executing a single command (e.g. ssh user#example.com ls -l).
#!/bin/sh
empty -f -i in -o out telnet foo.bar.com
empty -w -i out -o in "ogin:" "luser\n"
empty -w -i out -o in "assword:" "TopSecret\n"
empty -s -o in "who am i\n"
empty -s -o in "exit\n"
http://empty.sourceforge.net/
Maybe this solution will be useful, but as stated in there, it won't work in case you need user/password authentication.
In order to skip manual user/password authentication, setting up ssh keys is the way to go. There is a short explanation about this here: SSH to a server without password for Admin Ease

Cannot write a script to "svn export" in Python

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
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.
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
Check out the pxssh module that is part of the pyexpect project:
https://pexpect.readthedocs.org/en/latest/api/pxssh.html
It simplifies dealing with automating ssh-ing into machines.

Categories

Resources