Skip to content

Instantly share code, notes, and snippets.

@besstiolle
Created February 11, 2019 12:06
Show Gist options
  • Save besstiolle/efd83a6d2b22b54b445a5b88e5037039 to your computer and use it in GitHub Desktop.
Save besstiolle/efd83a6d2b22b54b445a5b88e5037039 to your computer and use it in GitHub Desktop.
Script Python pour détecter le réseau et switcher les paramètres proxy du PC
import socket
import subprocess
import os
# Exemple de pattern ip
patternA = "10.99.99."
patternB = "10.0.0."
# Information Proxy utilisé dans le pattern ip B
user = "aaa"
password = "xxx"
host = "99.99.99.99"
port = "8080"
whitelist = "localhost; 127.0.0.1"
currentIp = socket.gethostbyname(socket.gethostname())
if patternA in currentIp :
print ("pattern A")
proxy = ''
os.system("setx.exe http_proxy '" + proxy + "'")
os.system("setx.exe https_proxy '" + proxy + "'")
os.system("setx.exe ftp_proxy '" + proxy + "'")
os.system("git config --global --unset http.proxy")
os.system("git config --global --unset https.proxy")
os.system("npm config delete proxy")
os.system("npm config delete http.proxy")
os.system("npm config delete https.proxy")
os.system("REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\" /v \"MigrateProxy\" /t REG_DWORD /d 0 /f")
os.system("REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\" /v \"ProxyEnable\" /t REG_DWORD /d 0 /f")
os.system("REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\" /v \"AutoDetect\" /t REG_DWORD /d 0 /f")
os.system("REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\" /v \"ProxyServer\" /t REG_SZ /d \"\" /f")
os.system("REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\" /v \"ProxyOverride\" /t REG_SZ /d \"\" /f")
elif patternB in currentIp :
print ("pattern B")
proxy = 'http://'+user+':'+password+'@'+host+':'+port
os.system("setx.exe http_proxy '" + proxy + "'")
os.system("setx.exe https_proxy '" + proxy + "'")
os.system("setx.exe ftp_proxy '" + proxy + "'")
os.system("git config --global --add http.proxy " + proxy)
os.system("git config --global --add https.proxy " + proxy)
os.system("npm config set proxy " + proxy)
os.system("npm config set http.proxy " + proxy)
os.system("npm config set https.proxy " + proxy)
os.system("REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\" /v \"MigrateProxy\" /t REG_DWORD /d 1 /f")
os.system("REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\" /v \"ProxyEnable\" /t REG_DWORD /d 1 /f")
os.system("REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\" /v \"AutoDetect\" /t REG_DWORD /d 1 /f")
os.system("REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\" /v \"ProxyServer\" /t REG_SZ /d \"" + proxy + "\" /f")
os.system("REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\" /v \"ProxyOverride\" /t REG_SZ /d \"" + whitelist + "\" /f")
else:
print ("erreur ip was : " + currentIp)
print ("That All Folks")
#Unix version
#export http_proxy='http://username:password@host:port'
#export https_proxy='http://username:password@host:port'
#export ftp_proxy='http://username:password@host:port'
#unset http_proxy
#unset https_proxy
#unset ftp_proxy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment