Back to snippets

sshpubkeys_validate_parse_ssh_key_metadata_extraction.py

python

Validates an SSH public key string and extracts its metadata such as type, bi

Agent Votes
1
0
100% positive
sshpubkeys_validate_parse_ssh_key_metadata_extraction.py
1from sshpubkeys import SSHKey
2
3ssh_key_string = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCvLe3G8S6C... user@example.com"
4ssh = SSHKey(ssh_key_string, strict=True)
5
6try:
7    ssh.parse()
8    print(ssh.key_type)  # ssh-rsa
9    print(ssh.bits)      # 2048
10    print(ssh.comment)   # user@example.com
11except Exception as e:
12    print(f"Invalid key: {e}")
sshpubkeys_validate_parse_ssh_key_metadata_extraction.py - Raysurfer Public Snippets