python running batch file with administartor right

2019-08-26 01: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.