Reputation: 605
I see that there are a few questions regarding fabric and passwords. I know that if I pass -I to fabric then the password I enter is passed to the environmental variable "password." The problem is that I'm prompted for a password when running an ssh command on my remote server to another remote server.
But, I don't want to be prompted for a password entry. I am prompted no matter what I try to do. So here's a little snippet of the code:
elif "test" in run('hostname -d'):
print(blue("Gathering Knife info"))
run("ssh mychefserver knife node show `hostname`.test.dmz")
It works just fine when I enter my password. The thing is, I don't want to have to enter my password. Maybe this is because another ssh connection is initiated on the remote host and fabric can't do anything about that.
I could have the script disconnect from the remote host, run the ssh command locally, then reconnect to the remote host to finish the script... but that seems silly. Suggestions?
Getpass info:
Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from getpass import getpass
>>> getpass('test: ')
test:
'This is a test'
Upvotes: 4
Views: 11381
Reputation: 1830
Here's how I did it. In short: If you set fabric.api.env.password
, fabric will use it to connect to the servers listed in env.hosts
:
from getpass import getpass
from fabric.api import env
# Setting env.hosts
env.hosts = ['[email protected]']
def deploy():
"""A deployment script"""
# Setting env.password with `getpass`
# See here for more: http://fabric.readthedocs.org/en/1.8/usage/env.html#password
env.password = getpass('Enter the password for %s: ' % env.hosts[0])
# The rest of the script follows...
Upvotes: 0
Reputation: 23480
from subprocess import Popen, PIPE
from getpass import getpass
x = Popen('ssh root@host', stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
print x.stdout.readline()
_pass = getpass('Enter your superduper password:')
x.stdin.write(_pass)
print x.stdout.readline()
Once connected, you can still input things as if you were on the other machine via x.stdin.write(...)
so yea, that should work?
DEBUG (just start a cmd promt, navigate to your python directory and write Python):
C:\Users>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from getpass import getpass
>>> getpass('test: ')
test:
'This is a test'
>>>
Upvotes: 3
Reputation: 4392
If you don't want to enter your password, you can set up a keypair for authentication. You use ssh-keygen to generate a public and private key, put the public key onto the remote machine and use the private key as authentication mechanism (https://en.wikipedia.org/wiki/Ssh-keygen).
Upvotes: -1