python running batch file with administartor rights by subprocess to change ip

北城以北 提交于 2019-12-11 19:38:58

问题


I want to run a batch file which changes my ip address by static

// sth.bat

netsh int ip set address "Wifi" static xxxx.xxxx.xxxx.xxxx 255.255.255.0 xxxx.xxxx.xxxx.x 1

changing the ip address requires administrator rights so i want to use the subprocess module to run the file as admin

by the following python module

// ip_changer.py

import subprocess,time,socket
from subprocess import PIPE,STDOUT

p = subprocess.Popen("runas /user:Administrator \"C:/Users/userName/sth.bat\"", shell = True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
print(p.communicate(input = bytes("myPassword","utf-8")))

the communicate tuple returns me the following:

(b'Geben Sie das Kennwort f\x81r "Administrator" ein: \x00\r\n', None)

where the first entry is the question for the password from the shell, but my ip doesn't change at all. Running the .bat file manually works fine.

Do i send the pasword before it is requested? Or do you see what's wrong there ?

would be great to get some help here.

来源:https://stackoverflow.com/questions/20803595/python-running-batch-file-with-administartor-rights-by-subprocess-to-change-ip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!