controlVMnetworkstate.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 import os
3 import sys
4 import time
5 import subprocess
6 import random
7 bindir='/usr/bin'
8 sys.path.append(bindir)
9 
10 def input_sel(prompt,max_,selectionoption):
11  # let the user choose the VM and verify selection
12  while True:
13  try:
14  print ('Please select from the list of running VMs\n\n'+'\n'.join(selectionoption))
15  userin = int(raw_input(prompt))
16  except ValueError:
17  print('\nThat was not a number\n\n')
18  continue
19  if userin > max_:
20  print('\nInput must be less than or equal to {0}.\n\n'.format(max_))
21  elif userin < 1:
22  print('\nInput must be greater than or equal to 1\n\n')
23  else:
24  return userin
25 
26 def statustext(result):
27  if result == 0:
28  status = 'OK'
29  else:
30  status = 'Failed'
31  return status
32 
34  try:
35  offtime = 600
36  ontime = 14
37  vmdict={}
38  vmlist=[]
39  executable = os.path.join(bindir, 'VBoxManage')
40 
41  #retrieve a list of all running VMs
42  runningvms= subprocess.check_output('%s list runningvms' %executable,shell=True).splitlines()
43  if len(runningvms) != 0:
44  for n in range(0, len(runningvms)):
45  vmlist.append('%s: %s' %(n+1,runningvms[n].rsplit(' ',1)[0].strip('"')))
46  vmdict[n+1]=runningvms[n].rsplit(' ',1)[-1]
47  usersel=input_sel('\nEnter the number of the VM: ',len(runningvms),vmlist)
48 
49  else:
50  print('Can not retrieve list of running VMs')
51  sys.exit()
52 
53  vmuuid=vmdict[usersel]
54  while True:
55  offtime = random.randint(60, 90)
56  ontime = random.randint(10, 90)
57  timenow = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
58  on = subprocess.call('%s controlvm %s setlinkstate1 on' %(executable,vmuuid),
59  shell=True)
60  status=statustext(on)
61  print ('%s: Plug Network cable into VM %s for %ds: %s' % (timenow, runningvms[usersel-1].rsplit(' ',1)[0].strip('"'),ontime, str(status)))
62  time.sleep(ontime)
63  timenow = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
64  off = subprocess.call('%s controlvm %s setlinkstate1 off' %(executable,vmuuid),
65  shell=True)
66  status = statustext(off)
67  print ('%s: Unplug Network cable from VM %s for %ds: %s' % (timenow, runningvms[usersel-1].rsplit(' ',1)[0].strip('"'),offtime, str(status)))
68  time.sleep(offtime)
69  except KeyboardInterrupt:
70  sys.exit('\nUser Interrupt')
71  except Exception as e:
72  print("Error in %s in function %s: %s" % (__name__, sys._getframe().f_code.co_name, e.message))
73 if __name__ == "__main__":
74  sys.exit(controlvmnetworkstate())
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
Definition: core.h:2081
def input_sel(prompt, max_, selectionoption)
void print(std::FILE *f, const S &format_str, Args &&...args)
Definition: core.h:2101
arg_join< It, Sentinel, char > join(It begin, Sentinel end, string_view sep)
Definition: format.h:3688
int len
Definition: utf-8.c:46


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:47:33