-
Notifications
You must be signed in to change notification settings - Fork 0
/
changeip.py
executable file
·86 lines (62 loc) · 2.79 KB
/
changeip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import re
import subprocess
import admin
import funciones
ruta = funciones.CreaDirectorioTrabajo()
#https://social.technet.microsoft.com/Forums/scriptcenter/en-US/e4424503-317c-44d1-8ecb-a40ad8796720/run-bat-file-in-admin-privilege
'''
Netsh interface set interface "Local Area Connection" enable
Netsh interface set interface "Local Area Connection" disable
netsh interface ip set address "Red local" static 192.168.0.3 255.255.255.0 192.168.0.1 1
netsh interface ip set dns name="Local Area Connection" static 208.67.222.222
netsh interface ip add dns name="Local Area Connection" 208.67.220.220 index=2
netsh interface ip set address "Description" dhcp
netsh interface ip set dnsservers name="Local Area Connection" source=dhcp
'''
def runRoot(funcion):
#funcion()
if not admin.isUserAdmin():
#admin.runAsAdmin(cmdLine=funcion())
admin.runAsAdmin(cmdLine=funcion())
funcion()
def creaBat(comando='ipconfig', fichero='ip.bat'):
f = open(fichero,'w')
f.write(comando)
f.close
def listaAdaptadores():
os.system('netsh interface show interface > %s\\interfaces'%ruta)
f = open('%s\\interfaces'%ruta, 'r')
lista = f.read()
f.close
return re.findall('Conex.*|Eth.*|WI.*', lista)
def batSTATIC(adaptadores, datos):
#adaptadores = listaAdaptadores() # coger esta funcion en main y ponerla con interfaz grafica y mandar la interfaz como parametro a la funcion
print(datos)
#netsh interface ipv4 set address name = "Conexión de área local 2" source=static address=172.0.0.181 mask=255.255.255.0 gateway=172.0.0.121
comando = '''netsh interface ipv4 set address name="%s" source=static %s %s %s 1
netsh interface ipv4 set dns name="%s" static %s
netsh interface ipv4 add dns name="%s" %s index=2'''%(adaptadores, datos[0], datos[1], datos[2], adaptadores, datos[3], adaptadores, datos[4])
creaBat(comando)
netshcmd=subprocess.Popen('ip.bat', shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE )
output, errors = netshcmd.communicate()
if errors:
print("WARNING: ", errors)
else:
print("SUCCESS ", output)
def batDHCP(adaptadores):
#adaptadores = listaAdaptadores() # coger esta funcion en main y ponerla con interfaz grafica y mandar la interfaz como parametro a la funcion
comando = '''netsh interface ipv4 set address "%s" dhcp
netsh interface ipv4 set dnsservers name="%s" source=dhcp'''%(adaptadores,adaptadores)
creaBat(comando)
netshcmd=subprocess.Popen('ip.bat', shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE )
output, errors = netshcmd.communicate()
if errors:
print("WARNING: ", errors)
else:
print("SUCCESS ", output)
#batDHCP()
#batSTATIC()