|
|
|
@ -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() |