创建shell.vbs和cmd.bat文件
shell.vbs的内容如下
cwd = CreateObject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.Path
path = cwd & "\cmd.bat"
Set shell = CreateObject("Shell.Application")
shell.ShellExecute path,"","","runas",1
WScript.Quit
cmd = "net user " + user + " " + pwd + " /add1"
f = None
try:
bat = os.getcwd() + r"\script\cmd.bat"
f = open(bat, 'w')
f.write(cmd)
except Exception as e:
traceback.print_exc()
raise e
finally:
if f:
f.close()
try:
shell = os.getcwd() + r"\script\shell.vbs"
msg = subprocess.Popen(shell, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("[PID] %s: %s" % (msg.pid, cmd))
msg.wait(timeout=3)
stderr = str(msg.stderr.read().decode("gbk")).strip()
stdout = str(msg.stdout.read().decode("gbk")).strip()
if "" != stderr:
print(stderr)
conn.send("失败".encode('utf-8'))
if stdout.find("失败") > -1:
print(stdout)
conn.send("失败".encode('utf-8'))
if stdout.find("成功") > -1:
print(stdout)
conn.send("成功".encode('utf-8'))
except Exception as e:
conn.send("失败".encode('utf-8'))
|