我正在使用python paramiko编写用于自动生成SSL证书的工具
我的步骤:
1。使用paramiko自动登录AWS Linux服务器(EC2),此步骤成功
2.Exec certbot命令,我无法获得响应内容,但是我可以读取 如果我在aws终端上执行命令,则响应消息
PS:其他命令可以获取内容,例如:“ ls -al”,“ ls”,“ cd”,“ mkdir”,“ touch” .............. >
这是我的代码结果的屏幕截图:
所以我不知道为什么我无法获得响应内容,哪个链接有问题吗?
谢谢大家!
import argparse
import paramiko
import time
class GenerateSsl:
def __init__(self, host, key):
self.generate_ssl_host = host
self.sandbox_host_key = key
def connect_to_sandbox(self):
try:
folder_name = self.generate_ssl_host.replace('.', '')
k = paramiko.RSAKey.from_private_key_file(self.sandbox_host_key)
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print('[-] connecting')
c.connect(hostname="x.x.x.x", username="ec2-user", pkey=k)
print('[+] connected')
commands = [
# "ls -al",
# "ls /var/www/%s" % (folder_name),
# "sudo touch /var/www/%s/index.html" % (folder_name),
"sudo ./certbot-auto certonly --webroot -w /var/www/%s -d %s" % (folder_name, self.generate_ssl_host)
]
for index, command in enumerate(commands):
print(index)
print('[+] ' + "Executing %s" % (format(command)))
stdin, stdout, stderr = c.exec_command(command)
print(stdout.read())
print(stdout.readline())
print(stdout.readlines())
# if index == 0:
# print('2222222222')
# response = stdout.readlines()
# print(response)
# else:
# response = stdout.read().decode()
# print(response)
# if index == 2 is True:
# print('step 2')
# print(response)
# else:
# print('0000000')
# stdin, stdout, stderr = c.exec_command(command)
# response = stdout.read().decode()
# if response == '':
# command = "sudo mkdir /var/www/%s" % (folder_name)
# stdin, stdout, stderr = c.exec_command(command)
# response = stdout.read().decode()
# if response == '':
# print('[+] ' + "Executing %s successfully" % (format(command)))
# commands = ["sudo ./certbot-auto certonly --webroot -w /var/www/www2 -d %s" % (self.generate_ssl_host)]
# for command in commands:
# print('[+] ' + "Executing %s" % (format(command)))
# stdin, stdout, stderr = c.exec_command(command)
# print(stdout.read().decode())
c.close()
except Exception:
print(Exception)
finally:
c.close()
def main():
parser = argparse.ArgumentParser(description='Automatic Generation of Domain Name SSL Certificate.')
parser.add_argument('-host', help='Domain name that need to generate SSL certificates')
parser.add_argument('-key', help='Sandbox server aws key')
args = parser.parse_args()
instance = GenerateSsl(args.host, args.key)
instance.connect_to_sandbox()
if __name__ == '__main__':
main()