Files
x11ma/script/test/TestRsync.py
gac-x11ma 99586393b5
2020-09-22 10:39:02 +02:00

94 lines
3.2 KiB
Python

import subprocess
import sys
import os.path
RSYNC_GENERATE_USER_KEY = True
def rsync(src, dest, key):
#cmd = 'rsync -e "ssh -i ' + key + ' -o LogLevel=quiet" --chmod=ug=rwx --verbose --modify-window=1 --times --recursive ' + src + ' ' + dest
#ret = exec_cmd(cmd)
cmd = 'rsync -e "ssh -i ' + key + '" --chmod=ug=rwx --verbose --modify-window=1 --times --recursive ' + src + ' ' + dest
ret = exec_cmd(cmd, False)
lines = ret.split("\n")
lines = filter(lambda x: x != "", lines)
if len(lines)<3:
raise Exception ("Invalid format")
files = lines[1:-2]
stats = lines[-2]
stats = [int(s) for s in stats.split() if s.isdigit()]
bytes_sent, bytes_received = stats[0], stats[1]
return files, bytes_sent, bytes_received
def sync_user_data(user, src, dest):
try:
key = os.path.expanduser("~/.ssh/" + ("ke" if RSYNC_GENERATE_USER_KEY else "id_rsa"))
if not os.path.isfile(key):
raise Exception ("Invalid key file")
dest = user + "@localhost:" + dest
files, bytes_sent, bytes_received = rsync(src,dest,key)
msg = "Transferred " + str(bytes_sent) + " byte to " + user + ": "
for f in files:
msg = msg + "\n" + f
log(msg, False)
print msg
except:
msg = "Error transferring user data to " + user + ": " + str(sys.exc_info()[1])
log(msg, False)
print >> sys.stderr, msg
#sync_user_data("e18720", "/sls/X11MA/data/X11MA/pshell/home/log", "~/Data1")
#export PK=`cat ~/.ssh/id_rsa.pub`; su - e18720 bash -c "echo $PK >> .ssh/authorized_keys; sort .ssh/authorized_keys | uniq > .ssh/authorized_keys.uniq ; mv .ssh/authorized_keys.uniq .ssh/authorized_keys"
user = "e18720"
def remove_user_key():
cmd = "rm ~/.ssh/ke;"
cmd = cmd + "rm ~/.ssh/ke.pub"
print exec_cmd(cmd, False)
def reset_user_key():
remove_user_key()
cmd = "ssh-keygen -N '' -f ~/.ssh/ke -t rsa;"
print exec_cmd(cmd)
def authorize_user(user):
success_msg = 'Success transfering authorization key for: ' + user
cmd = 'echo Authorizing: ' + user + ";"
if RSYNC_GENERATE_USER_KEY:
reset_user_key()
cmd = cmd + "export PK=`cat ~/.ssh/ke.pub`;"
else:
cmd = cmd + "export PK=`cat ~/.ssh/id_rsa.pub`;"
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;'
cmd = cmd + 'echo ' + success_msg
cmd = cmd + '"'
#xterm_options = '-hold -T "Authentication" -into 44040199' #Get Winfow ID with 'wmctrl -lp'
xterm_options = '-hold -T "Authentication" -fa monaco -fs 14 -bg black -fg green -geometry 80x15+400+100'
try:
ret = exec_cmd("xterm " + xterm_options + " -e '" + cmd + "'")
if not success_msg in ret:
raise Exception ("Error authenticating user")
except:
if RSYNC_GENERATE_USER_KEY:
remove_user_key()
raise
return ret
#reset_user_key()
authorize_user(user)
#sync_user_data(user, "/sls/X11MA/data/X11MA/pshell/home/log", "~/Data1")
#print exec_cmd("ls $HOME", user, "6p!ZP_sQ9")