Files
dev/script/test/TestRSync.py
2021-04-28 09:29:19 +02:00

72 lines
2.4 KiB
Python

import ch.psi.utils.Sys
RSYNC_GENERATE_USER_KEY = True
XTERM = "LANG=C /opt/X11/bin/xterm" if ch.psi.utils.Sys.getOSFamily().name()=="Mac" else "xterm"
user = "gobbo_a"
aux_file = os.path.expanduser("~/.rsync.tmp")
fix_permissions=True
do_print=True
def remove_user_key(do_print=True):
cmd = "rm ~/.ssh/ke;"
cmd = cmd + "rm ~/.ssh/ke.pub"
ret = exec_cmd(cmd, False)
if do_print:
if not ret.strip():
ret = "Success removing ssh keys"
print ret
def reset_user_key(do_print=True):
remove_user_key(do_print)
cmd = "ssh-keygen -N '' -f ~/.ssh/ke -t rsa;"
ret = exec_cmd(cmd)
if do_print:
print ret
if (os.path.isfile(aux_file)):
os.remove(aux_file)
with open(aux_file, "w") as fh:
fh.write("Cannot access file: " + aux_file)
os.chmod(aux_file, 0o777)
success_msg = 'Success transfering authorization key for: ' + user
cmd = 'echo Authorizing: ' + user + ";"
cmd = cmd + 'echo Invalid user or password > ' + aux_file + ";"
cmd = cmd + "export PK_SUCCESS=FAILURE;"
if RSYNC_GENERATE_USER_KEY:
reset_user_key(do_print)
cmd = cmd + "export PK=`cat ~/.ssh/ke.pub`;"
else:
cmd = cmd + "export PK=`cat ~/.ssh/id_rsa.pub`;"
cmd = cmd + 'echo Invalid user or password > ' + aux_file + ";"
cmd = cmd + 'su - ' + user + ' bash -c "'
cmd = cmd + 'echo $PK >> .ssh/authorized_keys;'
#cmd = cmd + 'sort .ssh/authorized_keys | uniq > .ssh/authorized_keys.uniq;'
#cmd = cmd + 'mv .ssh/authorized_keys.uniq .ssh/authorized_keys;'
if fix_permissions:
cmd = cmd + 'chmod g-w ~' + ";"
cmd = cmd + 'echo ' + success_msg + ";"
cmd = cmd + 'echo ' + success_msg + " > " + aux_file + ";"
cmd = cmd + '"'
#xterm_options = '-hold -T "Authentication" -into 44040199' #Get Winfow ID with 'wmctrl -lp'
xterm_options = '-T "Authentication" -fa monaco -fs 14 -bg black -fg green -geometry 80x15+400+100'
try:
cmd = "su - gobbo_a;ls"
ret = exec_cmd(XTERM + " " + xterm_options + " -e '" + cmd + "'")
with open (aux_file, "r") as myfile:
ret=myfile.read()
#;if [ "$depth" -eq "1" ]; then echo ' + success_msg + '; fi')
if not success_msg in ret:
raise Exception (ret)
except:
if RSYNC_GENERATE_USER_KEY:
remove_user_key(do_print)
raise Exception ("Error authenticating user: " + str(sys.exc_info()[1]))
finally:
if (os.path.isfile(aux_file)):
os.remove(aux_file)