栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python已管理员身份执行cmd

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python已管理员身份执行cmd

创建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"scriptcmd.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"scriptshell.vbs"
            # shell=False的时候cmd_list是一个列表,shell=True的时候cmd_list是一个字符串
            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'))
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/357066.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号