Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to read password from stdin #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions VmBackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
# Usage w/ config file for multiple vm backups, where you can specify either vm-export or vdi-export:
# ./VmBackup.py <password> <config-file-path>

import sys, time, os, datetime, subprocess, re, shutil, XenAPI, smtplib, re, base64, socket
import sys, time, os, datetime, subprocess, re, shutil, XenAPI, smtplib, re, base64, socket, signal
from email.MIMEText import MIMEText
from subprocess import PIPE
from subprocess import STDOUT
Expand Down Expand Up @@ -1369,6 +1369,46 @@ def usage_examples():
print ' ./VmBackup.py /root/VmBackup.pass monthly.cfg'
print

def read_char_non_block(file_object,Block=True):
timeout=1
signal.signal(signal.SIGALRM, input)
signal.alarm(timeout)
try:
if Block or select.select([file_object], [], [], 0) == ([file_object], [], []):
return file_object.read(1)
except:
return

def read_password_from_stdin():
password=""
while True:
char = read_char_non_block(sys.stdin)
if char:
password += char
else:
if len(password) > 0:
return password.rstrip()
else:
return

def get_password(password_arg):

# read password from stdin if password_arg is a dash("-")
if password_arg == "-":
password = read_password_from_stdin()
if password:
return password
else:
raise "FATAL: Could not get password from stdin"

# return base64-decoded file content if password_arg is an existing file
if (os.path.exists(password)):
password = base64.b64decode(open(password, 'r').read())
return password

# else take the given password_arg as clear text password
return pasword_arg

if __name__ == '__main__':
if 'help' in sys.argv or 'config' in sys.argv or 'example' in sys.argv:
if 'help' in sys.argv: usage_help()
Expand All @@ -1378,11 +1418,9 @@ def usage_examples():
if len(sys.argv) < 3:
usage()
sys.exit(1)
password = sys.argv[1]
password = get_password(sys.argv[1])
cfg_file = sys.argv[2]
# obscure password support
if (os.path.exists(password)):
password = base64.b64decode(open(password, 'r').read())
if cfg_file.lower().startswith('create-password-file'):
array = sys.argv[2].strip().split('=')
open(array[1], 'w').write(base64.b64encode(password))
Expand Down