Created
February 9, 2021 23:53
-
-
Save frgomes/08e9a5af439d8c98f21824ab160d415f to your computer and use it in GitHub Desktop.
Python - Find public keys matching a given list of patterns
This file contains hidden or 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
def contents(ssh, files): | |
for file in files: | |
path = '{}/{}'.format(ssh, file) | |
yield open(path).read().replace("\n", "") | |
def keysFor(args): | |
ssh = '{}/.ssh'.format(os.environ['HOME']) | |
extensions = [ '.pub' ] | |
files = [f for f in os.listdir(ssh) if os.path.splitext(f)[1] in extensions] | |
keys = [k for k in contents(ssh, files) if k.split(' ')[2] in args] | |
for key in keys: | |
yield key | |
def main(args): | |
keys = keysFor(args) | |
for key in keys: | |
print(key) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Call like this: