xfreerdpctl 5.8 KB

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