From cf873454ff563b3481a0940dd4c1edf4664063f2 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Tue, 9 Apr 2019 16:49:31 -0500 Subject: [PATCH] Added the script to put the certificate where it needs to go --- copy_cert.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/copy_cert.py b/copy_cert.py index e4eb3e5..b414329 100644 --- a/copy_cert.py +++ b/copy_cert.py @@ -2,6 +2,7 @@ import paramiko import argparse import os +import posixpath from subprocess import list2cmdline @@ -45,3 +46,39 @@ if __name__ == "__main__": help="directory to store the private key and certificate") parser.add_argument('-d', '--dry', action='store_true') args = parser.parse_args() + # Connect to host + router = connection_from_config('router') + stdout, stderr = exec_remote(router, ['ls', args.store_path]) + # Check to see if remote directory exists + if stdout.channel.recv_exit_status() == 0: + files = list(filter(bool, stdout.read().decode().split('\n'))) + # Delete old certificate and key + for file in files: + if file in ('cert.pem', 'key.pem'): + if args.dry: + print("Removing {}".format( + posixpath.join(args.store_path, file))) + else: + exec_remote( + router, ['rm', posixpath.join( + args.store_path, file)] + ) + else: + # Create missing directory + if args.dry: + print("Creating missing dir {}".format(args.store_path)) + else: + exec_remote(router, ['mkdir', args.store_path]) + sftp = router.open_sftp() + # Copy certificate and key to store_path + for src, dst in zip( + [args.cert_path, args.key_path], + [posixpath.join(args.store_path, "cert.pem"), + posixpath.join(args.store_path, "key.pem")] + ): + if args.dry: + print("{} -> {}".format(src, dst)) + else: + scp(sftp, src, dst) + sftp.close() + router.close()