Skip to content

Instantly share code, notes, and snippets.

@olavmrk
Created August 27, 2015 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olavmrk/dac2f92864e25d9914c0 to your computer and use it in GitHub Desktop.
Save olavmrk/dac2f92864e25d9914c0 to your computer and use it in GitHub Desktop.
SSH with key from environment variable
#!/usr/bin/env python
from __future__ import print_function
from __future__ import unicode_literals
import os
import tempfile
import subprocess
import sys
if not 'SSH_KEY' in os.environ:
cmd = [
'ssh',
'-o', 'StrictHostKeyChecking=no',
]
cmd += sys.argv[1:]
retcode = subprocess.call(cmd)
sys.exit(retcode)
with tempfile.NamedTemporaryFile() as ssh_key_file:
ssh_key_file.write(os.environ['SSH_KEY'])
ssh_key_file.flush()
cmd = [
'ssh',
'-o', 'StrictHostKeyChecking=no',
'-i', ssh_key_file.name,
]
cmd += sys.argv[1:]
retcode = subprocess.call(cmd)
sys.exit(retcode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment