-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a utility to generate the encrypted data
- Loading branch information
Showing
5 changed files
with
63 additions
and
43 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python3 | ||
import os | ||
import argparse | ||
from getpass import getpass | ||
|
||
from netmiko.utilities import load_netmiko_yml | ||
from netmiko.encryption_handling import encrypt_value | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description="Encrypt data using Netmiko's encryption." | ||
) | ||
parser.add_argument("data", help="The data to encrypt", nargs='?') | ||
parser.add_argument( | ||
"--key", | ||
help="The encryption key (if not provided, will use NETMIKO_TOOLS_KEY env variable)", | ||
) | ||
parser.add_argument( | ||
"--type", | ||
choices=["fernet", "aes128"], | ||
help="Encryption type (if not provided, will read from .netmiko.yml)", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
if args.data: | ||
data = args.data | ||
else: | ||
data = getpass("Enter the data to encrypt: ") | ||
|
||
# Get encryption key | ||
if args.key: | ||
key = args.key.encode() | ||
else: | ||
key = os.environ.get("NETMIKO_TOOLS_KEY") | ||
if not key: | ||
msg = """Encryption key not provided. | ||
Use --key or set NETMIKO_TOOLS_KEY environment variable.""" | ||
raise ValueError(msg) | ||
key = key.encode() | ||
|
||
# Get encryption type | ||
if args.type: | ||
encryption_type = args.type | ||
else: | ||
config_params, my_devices = load_netmiko_yml() | ||
encryption_type = config_params.get("encryption_type", "fernet") | ||
|
||
if not encryption_type: | ||
msg = """Encryption type not provided. | ||
Use --type or set 'encryption_type' in .netmiko.yml in the '__meta__' section.""" | ||
raise ValueError(msg) | ||
|
||
# Encrypt the password | ||
encrypted_data = encrypt_value(data, key, encryption_type) | ||
print(f"\nEncrypted data: {encrypted_data}\n") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.