Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
666 views
in Technique[技术] by (71.8m points)

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

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 fx81r "Administrator" ein: x00 ', 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...