Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 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 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...