Last active
February 28, 2019 06:28
-
-
Save swordfeng/54ab3ed87a39bf45f41961df309a08ea to your computer and use it in GitHub Desktop.
load stored SSH passwords
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
#!/usr/bin/env python3 | |
import sys | |
import os | |
import re | |
from pathlib import Path | |
def main(): | |
args = list(sys.argv)[1:] | |
password = None | |
with open(os.path.join(Path.home(), '.ssh', 'stored_passwords')) as f: | |
for l in f: | |
l = l.rstrip('\n') | |
parts = l.split(None, 1) | |
if len(parts) == 2 and any(re.match(parts[0] + r'($|:)', arg) for arg in args): | |
password = parts[1] | |
break | |
if password is None: | |
os.execvp(args[0], args) | |
else: | |
os.environ['SSHPASS'] = password | |
os.execvpe('sshpass', ['sshpass', '-e'] + args, os.environ) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment