text: #! /usr/bin/env python
  #  -*- coding: utf-8 -*-
  #
  # Support module generated by PAGE version 4.18
  #  in conjunction with Tcl version 8.6
  #    Nov 11, 2018 03:57:06 PM CET  platform: Windows NT
  
  import sys, gui1, subprocess, threading, time, psutil, base64, re
  
  
  MEMORY_LIMIT_PERCENT=60
  CPU_LIMIT_PERCENT=30
  thread_limit=10
  
  block=False
  run_block=False
  first_run=True
  No_Cred=False
  
  try:
      import Tkinter as tk
  except ImportError:
      import tkinter as tk
  
  try:
      import ttk
      py3 = False
  except ImportError:
      import tkinter.ttk as ttk
      py3 = True
  
  def set_Tk_var():
      global wrn
      wrn = tk.StringVar()
      global combobox
      combobox = 'tk.StringVar()'
  
  def main():
      global CREATE_NO_WINDOW,block,hosts_parsed,run_block,querry,user,passw,CHECK_PER,sqlcmd_mode,first_run,selection,No_Cred,opts
      CREATE_NO_WINDOW = 0x08000000
      CHECK_PER=10
      Checked=False
      if not run_block:
          selection=w.TCombobox1.get()
          if selection=="sqlcmd":
              querry=w.Scrolledtext2.get(0.0,"end").replace('\n', ' ')
          elif selection=="Invoke-Command" or selection=="Invoke-WmiMethod":
              querry=w.Scrolledtext2.get(0.0,"end").replace('\n', ';')
          elif selection=="PSEXEC":
              querry=w.Scrolledtext2.get(0.0,"end").replace('\n', ';')
              querry=w.Scrolledtext2.get(0.0,"end").replace('powershell', 'powershell -inputformat none')
          querry=querry[:-1]
          user=w.Entry1.get().replace(' ', '').replace('\n', '')
          passw=w.Entry2.get().replace(' ', '').replace('\n', '').encode('base64')
          hosts=w.Scrolledtext1.get(0.0,"end").encode("ascii")
          opts=w.Entry3.get()
          sqlcmd_mode=False
          if len(hosts)>0 and len(querry)>0 and "-Querry-" not in querry  and "-Hosts-" not in hosts:
              if selection=="sqlcmd" and len(passw)>0 and len(user)>0 and "Username" not in user and "Username" not in passw.decode('base64'):
                  Checked=True
              elif selection=="Invoke-Command" or selection=="Invoke-WmiMethod" or selection=="PSEXEC":
                  Checked=True
                  No_Cred=False
                  if len(passw)==0 or len(user)==0 or "Username" in user or "Username" in passw.decode('base64'):
                      No_Cred=True
          if Checked:
              first_run=False
              run_block=True
              w.TProgressbar1['value']=0
              w.Scrolledtext3.delete(1.0,"end")
              w.Button1.configure(text="Running")
              hosts_parsed=hosts.splitlines()
              if selection=="sqlcmd" and re.search("xp_cmdshell",querry,re.IGNORECASE):
                  querry=querry.replace('xp_cmdshell', 'exec xp_cmdshell')
                  querry=querry.replace('"', '""')
                  sqlcmd_mode=True
              threading.Thread(target=check_system).start()
              threading.Thread(target=launcher).start()
  
          
  
  def launcher():
      global hosts_parsed,iterations,CHECK_PER,block,thread_limit
      iterations=0
      for line in hosts_parsed:
          if len(line)>0:
              if block or threading.activeCount()>thread_limit:
                  while threading.activeCount()>6:
                      time.sleep(1)
              thread = threading.Thread(target=procedure,args=(line.splitlines()))
              thread.start()
          else:
              pass
          if iterations%20==0 and iterations!=0 and len(hosts_parsed)!=(iterations+1):
              time.sleep(0.2)
          iterations+=1
          w.TProgressbar1['value']=(float(float(iterations)/float(len(hosts_parsed))))*100
  
  
  
  def procedure(dest):
      global iterations,hosts_parsed,run_block,querry,user,passw,sqlcmd_mode,selection,No_Cred,opts
      try:
          if selection=="sqlcmd":
              if sqlcmd_mode:
                  if "Opts.(Default)" in opts or len(opts)<1:
                      opts="-l 10 -t 30"
                      w.Entry3.delete(first=0,last=100)
                      w.Entry3.insert('insert',"Opts.(Default)")
                  out = subprocess.check_output("chcp 65001>NUL && @echo ON && sqlcmd -S "+dest+" -U "+user+" -P "+passw.decode('base64')+" -Q "+'"'+querry+'"'+" -s "+'"'+'|'+'"'+
                  " "+opts+" && exit",shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
              else:
                  if "Opts.(Default)" in opts or len(opts)<1:
                      opts="-y 32 -Y 32 -l 10 -t 60"
                      w.Entry3.delete(first=0,last=100)
                      w.Entry3.insert('insert',"Opts.(Default)")
                  out = subprocess.check_output("chcp 65001>NUL && @echo ON && sqlcmd -S "+dest+" -U "+user+" -P "+passw.decode('base64')+" -Q "+'"'+"SET NOCOUNT ON;"+querry+'"'+" -s "+'"'+'|'+'"'+
                  " "+opts+" && exit",shell=True, bufsize=-1 ,stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
          elif selection=="Invoke-Command":
              if "Opts.(Default)" in opts or len(opts)<1:
                  opts=""
                  w.Entry3.delete(first=0,last=100)
                  w.Entry3.insert('insert',"Opts.(Default)")
              if No_Cred:
                  out = subprocess.check_output("chcp 65001>NUL && powershell -ExecutionPolicy RemoteSigned -Command "+'"'+
                  "Invoke-Command -ComputerName "+dest+" -ScriptBlock {"+querry+"}"+" "+opts+
                  '"',shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
              else:
                  out = subprocess.check_output("chcp 65001>NUL && powershell -ExecutionPolicy RemoteSigned -Command "+'"'+"$Password = '"+passw.decode('base64')+
                  "';$pass = ConvertTo-SecureString -AsPlainText $Password -Force;$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList "+user+",$pass;"+
                  "Invoke-Command -ComputerName "+dest+" -Credential $Cred -ScriptBlock {"+querry+"}"+" "+opts+
                  '"',shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
          elif selection=="Invoke-WmiMethod":
              if "Opts.(Default)" in opts or len(opts)<1:
                  opts=""
                  w.Entry3.delete(first=0,last=100)
                  w.Entry3.insert('insert',"Opts.(Default)")
              if No_Cred:
                  out = subprocess.check_output("chcp 65001>NUL && powershell -ExecutionPolicy RemoteSigned -Command "+'"'+
                  r"$proc = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList 'CMD.EXE /c "+querry+r" > C:\temp\result.txt && exit' -ComputerName '"+dest+
                  "'"+" "+opts+r";$Process = Get-Process -ID $proc.processid;$Process.WaitForExit();Get-Content \\"+dest+r"\C$\temp\result.txt"+
                  '"',shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
              else:
                  out = subprocess.check_output("chcp 65001>NUL && powershell -ExecutionPolicy RemoteSigned -Command "+'"'+"$Password ='"+passw.decode('base64')+
                  r"';$pass = ConvertTo-SecureString -AsPlainText $Password -Force;$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList "+user+",$pass;"+
                  r"$proc = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList 'CMD.EXE /c "+querry+r" > C:\temp\result.txt' -ComputerName '"+dest+
                  "'"+" "+opts+r"' -Credential $Cred;$Process = Get-Process -ID $proc.processid;$Process.WaitForExit();Get-Content \\"+dest+r"\C$\temp\result.txt -Credential $Cred;"+
                  '"',shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
          elif selection=="PSEXEC":
              if "Opts.(Default)" in opts or len(opts)<1:
                  opts="-s"
                  w.Entry3.delete(first=0,last=100)
                  w.Entry3.insert('insert',"Opts.(Default)")
              if No_Cred:
                  out = subprocess.check_output(r"chcp 65001>NUL && .\PSEXEC /accepteula \\"+dest+" "+opts+" cmd /c "+'"'+querry+'"'+" && exit",shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
              else:
                  out = subprocess.check_output(r"chcp 65001>NUL && .\PSEXEC /accepteula \\"+dest+" -u "+user+" -p "+passw.decode('base64')+" "+opts+" cmd /c "+'"'+querry+'"'+" && exit",shell=True, bufsize=-1 , stderr=subprocess.STDOUT, stdin=subprocess.PIPE, close_fds=False, creationflags=CREATE_NO_WINDOW).decode("utf-8")
          else:
              destroy_window()
          w.Scrolledtext3.insert("end",'\n'+"++++++++++++++++++++++++++++++++++++++++"+'\n'+"--------------------"+"Output from: "+dest+'\n'+out)
      except subprocess.CalledProcessError as e:
          w.Scrolledtext3.insert("end",'\n'+"++++++++++++++++++++++++++++++++++++++++"+'\n'+"--------------------"+"Output from: "+dest+ " (ERROR!)"+'\n'+e.output)
      except Exception as e:
          if hasattr(e, 'message'):
              w.Scrolledtext3.insert("end",'\n'+"++++++++++++++++++++++++++++++++++++++++"+'\n'+"--------------------"+"Output from: "+dest+ " (ERROR!)"+'\n'+getattr(e, 'message', repr(e)))
          else:
              w.Scrolledtext3.insert("end",'\n'+"++++++++++++++++++++++++++++++++++++++++"+'\n'+"--------------------"+"Output from: "+dest+ " (ERROR!)"+'\n'+"Unknown error !!!")
      if iterations>=len(hosts_parsed):
          w.Button1.configure(text='''GO''')
          w.Scrolledtext3.see("end")
          w.TProgressbar1['value']=0
          run_block=False
      else:
          run_block=True
  
  
  
  def check_system():
      global run_block,block,thread_limit
      while run_block:
          current_cpu=psutil.cpu_percent(interval=0.2, percpu=False)
          if psutil.virtual_memory().percent<MEMORY_LIMIT_PERCENT and current_cpu<CPU_LIMIT_PERCENT:
              block=False
              thread_limit+=2
          else:
              block=True
              thread_limit-=2
  
  def init(top, gui, *args, **kwargs):
      global w, top_level, root
      w = gui
      top_level = top
      root = top
  
  def destroy_window():
      # Function which closes the window.
      global top_level,run_block,hosts_parsed
      hosts_parsed = []
      run_block=False
      top_level.destroy()
      top_level = None
  
  
  if __name__ == '__main__':
      import gui1.py
      gui1.py.vp_start_gui()
  
  
  
  
  