See How to answer to prompts automatically with python fabric?
from ilogue.fexpect import expect, expecting, run
def sample():
private_key = "password"
hostname = "ubuntu"
output_dir = '/etc/ssl/' + hostname
prompts = []
prompts += expect('Enter pass phrase for private.key:',private_key)
prompts += expect('Verifying - Enter pass phrase for private.key:private_key',private_key)
prompts += expect('Enter pass phrase for %s/server.key:' % output_dir, private_key)
prompts += expect('Country Name \(2 letter code\) \[AU\]:','AU')
prompts += expect('State or Province Name \(full name\) \[Some-State\]:','State')
prompts += expect('Locality Name \(eg, city\) \[\]:','City')
prompts += expect('Organization Name \(eg, company\) \[Internet Widgits Pty Ltd\]:','Company')
prompts += expect('Organizational Unit Name \(eg, section\) \[\]:','Section')
prompts += expect('Common Name \(e.g. server FQDN or YOUR name\) \[\]:','FQDN')
prompts += expect('Email Address \[\]:','email@foo.com')
prompts += expect('A challenge password \[\]:','challenge_password')
prompts += expect('An optional company name \[\]:','optional_company')
with expecting(prompts):
run('openssl genrsa -des3 -out %s/server.key 2048' % output_dir)
run('openssl req -new -key %s/server.key -out %s/server.csr' % (output_dir, output_dir))
# fab sample -H localhost
the regular expression is applied to expect(), you need to escape [, ], (, ) ...