xfreerdpctl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/env python3
  2. ## Autore: Nicolo' Palazzini
  3. ## Liberamente ispirato a labcalcoloctl di Elisa Aliverti (all rights reserved)
  4. ## Ultima modifica: Gennaio 2020
  5. from time import time
  6. start = time()
  7. import argparse
  8. import os
  9. import subprocess, sys
  10. from threading import Thread
  11. choices = ('status','start','stop')
  12. parser = argparse.ArgumentParser(description='Simple tool to handle xfreerdp on LCM nodes.', usage='%(prog)s {'+','.join(choices)+'} [--options]')
  13. ## positional arguments
  14. parser.add_argument( 'cmd', nargs="?", choices=choices, default='status', help='Specify command to execute, default is \'status\'' )
  15. ## optional arguments
  16. parser.add_argument( '-a', '--all', action='store_true', dest='lcm', help='All LCM nodes are considered' )
  17. parser.add_argument( '-n', nargs='+', dest='node', help='Select one or more nodes (at least one)' )
  18. parser.add_argument( '-1', '--lcm1', action='store_true', dest='lcm1', help='LCM1 nodes are considered' )
  19. parser.add_argument( '-2', '--lcm2', action='store_true', dest='lcm2', help='LCM2 nodes are considered' )
  20. parser.add_argument( '-v', '--version', action='version', version='%(prog)s 2.0', help='Print program version' )
  21. args = parser.parse_args()
  22. def print_progressbar(index, num) :
  23. sys.stdout.write('\r ['
  24. + '='*index
  25. + '>'*(1-int(index/num))
  26. + ' '*(num-index-1) + ']')
  27. sys.stdout.flush()
  28. class Host(Thread):
  29. # Constructor
  30. def __init__(self, name, location):
  31. # Fork the thread first thing
  32. Thread.__init__(self)
  33. # Variables initialization
  34. self.hostname = name
  35. self.location = location
  36. self.running = False
  37. self.up = False
  38. # Run method called on Thread start. Check if host is up and if is running a VM
  39. def run(self) :
  40. if self.isup() :
  41. self.up = True
  42. self.running=self.status()
  43. # Ping the host to see if it's up
  44. def isup(self):
  45. # Is the host up?
  46. ping = os.popen("ping -w1 -c1 " + self.hostname, "r")
  47. # print("pinging " self.hostname)
  48. if "0 received" in ping.read():
  49. return False
  50. else:
  51. return True
  52. def sshcommand(self, command):
  53. if self.up:
  54. ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, command], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE, universal_newlines=True)
  55. return ssh
  56. else:
  57. print(self.hostname + ' is not up.')
  58. return False
  59. def status(self):
  60. statuscmd = "ps aux | grep 'xfreerdp' | grep -v grep"
  61. ssh = self.sshcommand(statuscmd)
  62. result = [ l for l in ssh.stdout.readlines() if '/usr/bin/xfreerdp /cert-ignore /f /v:tolab.fisica.unimi.it' in l ]
  63. if result == []:
  64. return False
  65. else:
  66. return True
  67. def xstart(self):
  68. if self.up:
  69. if not self.running:
  70. startcmd = "startx /usr/bin/xfreerdp /cert-ignore /f /v:tolab.fisica.unimi.it &> /dev/null &"
  71. self.sshcommand(startcmd)
  72. print('xfreerdp started on ' + self.hostname)
  73. else:
  74. print('xfreerdp is already running on ' + self.hostname)
  75. else:
  76. print(self.hostname + ' is not up.')
  77. def xstop(self):
  78. if self.up:
  79. if self.running:
  80. # Try to kill sending SIGTERM, wait, check if running and if so issue SIGKILL.
  81. # (Do this asynchronously, on the remote node)
  82. stopcmd = '''PCMDL="/usr/bin/xfreerdp /cert-ignore /f /v:tolab.fisica.unimi.it"
  83. pkill -xf "$PCMDL"
  84. sleep 5
  85. pgrep -xf "$PCMDL" && \
  86. pkill -9 -xf "$PCMDL"'''
  87. self.sshcommand(stopcmd)
  88. print('xfreerdp stopped on ' + self.hostname)
  89. else:
  90. print('xfreerdp is not running on ' + self.hostname)
  91. else:
  92. print(self.hostname + ' is not up.')
  93. Hosts = [
  94. Host('abe', 'LCM1'),
  95. Host('crash', 'LCM1'),
  96. Host('duke', 'LCM1'),
  97. Host('glados', 'LCM1'),
  98. Host('lara', 'LCM1'),
  99. Host('link', 'LCM1'),
  100. Host('king', 'LCM1'),
  101. Host('pang', 'LCM1'),
  102. Host('pong', 'LCM1'),
  103. Host('snake', 'LCM1'),
  104. Host('sonic', 'LCM1'),
  105. Host('spyro', 'LCM1'),
  106. Host('yoshi', 'LCM1'),
  107. Host('actarus', 'LCM2'),
  108. Host('elwood', 'LCM2'),
  109. Host('gex', 'LCM2'),
  110. Host('gin', 'LCM2'),
  111. Host('jake', 'LCM2'),
  112. Host('kirk', 'LCM2'),
  113. Host('martini', 'LCM2'),
  114. Host('picard', 'LCM2'),
  115. Host('q', 'LCM2'),
  116. Host('raziel', 'LCM2'),
  117. Host('sarek', 'LCM2'),
  118. Host('spock', 'LCM2'),
  119. Host('tron', 'LCM2'),
  120. Host('worf', 'LCM2'),
  121. Host('zombie', 'LCM2')
  122. ]
  123. nodes = []
  124. # Show usage if no arguments
  125. if len(sys.argv) < 2:
  126. parser.print_usage()
  127. print("\nSimple tool to handle xfreerdp on LCM nodes.")
  128. print('\nTry: "xfreerdpctl --help" to display help message.')
  129. sys.exit(1)
  130. # Filter hostlist according to arguments
  131. if args.lcm:
  132. nodes = Hosts
  133. elif args.lcm1:
  134. nodes = [ host for host in Hosts if host.location == 'LCM1' ]
  135. elif args.lcm2:
  136. nodes = [ host for host in Hosts if host.location == 'LCM2' ]
  137. elif args.node:
  138. nodes = [ j for j in Hosts if j.hostname in args.node ]
  139. for i in nodes :
  140. i.start()
  141. running=[]
  142. down=[]
  143. num=len(nodes)
  144. index=0
  145. print(' Querying ' + str(num) + ' hosts...')
  146. for i in nodes:
  147. # Rejoin them when their work is done
  148. i.join()
  149. if i.running:
  150. running.append(i.hostname)
  151. if not i.up :
  152. down.append(i.hostname)
  153. index += 1
  154. print_progressbar(index, num)
  155. # New line after progress bar
  156. print('\n Done... (%(t).3f s)' % {'t': (time() - start)})
  157. if args.cmd == 'status':
  158. if len(running):
  159. print("xfreerdp is running on:")
  160. for i in running : print('\t',i)
  161. else:
  162. print("xfreerdp is not running")
  163. if len(down):
  164. print("Down nodes:")
  165. for i in down : print('\t',i)
  166. elif args.cmd == 'start':
  167. for i in nodes: i.xstart()
  168. elif args.cmd == 'stop':
  169. for i in nodes: i.xstop()