mayankjoin1

setproxy.py

Sep 26th, 2017
1,403
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Credits: Vivek Bhargav, Source https://gomchikbhokablog.wordpress.com/2013/08/13/proxy-settings-in-ubuntu/
  2. #Slight Modifications
  3.  
  4. import sys
  5. import re
  6. import fileinput
  7. import os
  8.  
  9. if len(sys.argv) != 5 and len(sys.argv) != 3:
  10.     print("Wrong command, sample commands: \n python setproxy.py 202.141.80.19 3128 username password \n OR \n python setproxy.py 202.141.80.19 3128")
  11.  
  12. else:
  13.     proxy = sys.argv[1]
  14.     port = sys.argv[2]
  15.     os.system('sudo touch /etc/apt/apt.conf')
  16.     if len(sys.argv) == 5:
  17.         username = sys.argv[3]
  18.         password = sys.argv[4]
  19.         mark = 0
  20.         for line in fileinput.input("/etc/environment", inplace=1):
  21.             if "proxy" in line:
  22.                 mark = 1
  23.                 line = re.sub(r'(.*)_proxy=(.*)', r'\1_proxy="\1://'+username+':'+password+'@'+proxy+':'+port+"/\"\n", line.rstrip())
  24.             sys.stdout.write(line)
  25.  
  26.         if mark == 0:
  27.             file1 = open("/etc/environment", "w")
  28.             file1.write("PATH=\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games\"\n")
  29.             file1.write("http_proxy=\"http://"+username+":"+password+"@"+proxy+":"+port+"/\"\n")
  30.             file1.write("https_proxy=\"https://"+username+":"+password+"@"+proxy+":"+port+"/\"\n")
  31.             file1.write("ftp_proxy=\"ftp://"+username+":"+password+"@"+proxy+":"+port+"/\"\n")
  32.             file1.write("socks_proxy=\"socks://"+username+":"+password+"@"+proxy+":"+port+"/\"\n")
  33.             file1.close()
  34.  
  35.         mark = 0
  36.         for line in fileinput.input("/etc/apt/apt.conf", inplace=1):
  37.             if "Acquire::" in line and "Cache" not in line:
  38.                 mark = 1
  39.                 line = re.sub(r'Acquire::(.*)::proxy (.*)', r'Acquire::\1::proxy "\1://'+username+":"+password+"@"+proxy+":"+port+"/\";\n", line.rstrip())
  40.             sys.stdout.write(line)
  41.  
  42.         if mark == 0:
  43.             file1 = open("/etc/apt/apt.conf", "w")
  44.             file1.write("Acquire::http::proxy \"http://"+username+":"+password+"@"+proxy+":"+port+"/\";\n")
  45.             file1.write("Acquire::https::proxy \"https://"+username+":"+password+"@"+proxy+":"+port+"/\";\n")
  46.             file1.write("Acquire::ftp::proxy \"ftp://"+username+":"+password+"@"+proxy+":"+port+"/\";\n")
  47.             file1.write("Acquire::http::No-Cache \"True\";\n")
  48.             file1.write("Acquire::socks::proxy \"socks://"+username+":"+password+"@"+proxy+":"+port+"/\";\n")
  49.             file1.close()
  50.  
  51.         mark = 0
  52.         for line in fileinput.input("/etc/bash.bashrc", inplace=1):
  53.             if "export" in line:
  54.                 mark = 1
  55.                 line = re.sub(r'export (.*)_proxy=(.*)', r'export \1_proxy=\1://'+username+':'+password+'@'+proxy+':'+port+'\n', line.rstrip())
  56.             sys.stdout.write(line)
  57.  
  58.         if mark == 0:
  59.             file1 = open("/etc/bash.bashrc", "a")
  60.             file1.write("\n\nexport http_proxy=http://"+username+":"+password+"@"+proxy+":"+port+"\n")
  61.             file1.write("export https_proxy=https://"+username+":"+password+"@"+proxy+":"+port+"\n")
  62.             file1.write("export ftp_proxy=ftp://"+username+":"+password+"@"+proxy+":"+port+"\n")
  63.             file1.close()
  64.  
  65.  
  66.         mark = 0
  67.         for line in fileinput.input("/etc/wgetrc", inplace=1):
  68.             if not line.startswith("#") and "proxy" in line:
  69.                 mark = 1
  70.                 line = re.sub(r'(.*)_proxy=(.*)//(.*)', r'\1_proxy=\1://'+username+':'+password+'@'+proxy+':'+port+'\n', line.rstrip())
  71.             sys.stdout.write(line)
  72.  
  73.         if mark == 0:
  74.             file1 = open("/etc/wgetrc", "a")
  75.             file1.write("\n\nhttp_proxy=http://"+username+":"+password+"@"+proxy+":"+port+"\n")
  76.             file1.write("https_proxy=https://"+username+":"+password+"@"+proxy+":"+port+"\n")
  77.             file1.write("ftp_proxy=ftp://"+username+":"+password+"@"+proxy+":"+port+"\n")
  78.             file1.close()
  79.  
  80.     else:
  81.         mark = 0
  82.         for line in fileinput.input("/etc/environment", inplace=1):
  83.             if "proxy" in line:
  84.                 mark = 1
  85.                 line = re.sub(r'(.*)_proxy=(.*)', r'\1_proxy="\1://'+proxy+':'+port+"/\"\n", line.rstrip())
  86.             sys.stdout.write(line)
  87.  
  88.         if mark == 0:
  89.             file1 = open("/etc/environment", "w")
  90.             file1.write("PATH=\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games\"\n")
  91.             file1.write("http_proxy=\"http://"+proxy+":"+port+"/\"\n")
  92.             file1.write("https_proxy=\"https://"+proxy+":"+port+"/\"\n")
  93.             file1.write("ftp_proxy=\"ftp://"+proxy+":"+port+"/\"\n")
  94.             file1.write("socks_proxy=\"socks://"+proxy+":"+port+"/\"\n")
  95.             file1.close()
  96.  
  97.         mark = 0
  98.         for line in fileinput.input("/etc/apt/apt.conf", inplace=1):
  99.             if "Acquire::" in line and "Cache" not in line:
  100.                 mark = 1
  101.                 line = re.sub(r'Acquire::(.*)::proxy (.*)', r'Acquire::\1::proxy "\1://'+proxy+":"+port+"/\";\n", line.rstrip())
  102.             sys.stdout.write(line)
  103.  
  104.         if mark == 0:
  105.             file1 = open("/etc/apt/apt.conf", "w")
  106.             file1.write("Acquire::http::proxy \"http://"+proxy+":"+port+"/\";\n")
  107.             file1.write("Acquire::https::proxy \"https://"+proxy+":"+port+"/\";\n")
  108.             file1.write("Acquire::ftp::proxy \"ftp://"+proxy+":"+port+"/\";\n")
  109.             file1.write("Acquire::http::No-Cache \"True\";\n")
  110.             file1.write("Acquire::socks::proxy \"socks://"+proxy+":"+port+"/\";\n")
  111.             file1.close()
  112.  
  113.         mark = 0
  114.         for line in fileinput.input("/etc/bash.bashrc", inplace=1):
  115.             if "export" in line:
  116.                 mark = 1
  117.                 line = re.sub(r'export (.*)_proxy=(.*)', r'export \1_proxy=\1://'+proxy+':'+port+'\n', line.rstrip())
  118.             sys.stdout.write(line)
  119.  
  120.         if mark == 0:
  121.             file1 = open("/etc/bash.bashrc", "a")
  122.             file1.write("\n\nexport http_proxy=http://"+proxy+":"+port+"\n")
  123.             file1.write("export https_proxy=https://"+proxy+":"+port+"\n")
  124.             file1.write("export ftp_proxy=ftp://"+proxy+":"+port+"\n")
  125.             file1.close()
  126.  
  127.  
  128.         mark = 0
  129.         for line in fileinput.input("/etc/wgetrc", inplace=1):
  130.             if not line.startswith("#") and "proxy" in line:
  131.                 mark = 1
  132.                 line = re.sub(r'(.*)_proxy=(.*)//(.*)', r'\1_proxy=\1://'+proxy+':'+port+'\n', line.rstrip())
  133.             sys.stdout.write(line)
  134.  
  135.         if mark == 0:
  136.             file1 = open("/etc/wgetrc", "a")
  137.             file1.write("\n\nhttp_proxy=http://"+proxy+":"+port+"\n")
  138.             file1.write("https_proxy=https://"+proxy+":"+port+"\n")
  139.             file1.write("ftp_proxy=ftp://"+proxy+":"+port+"\n")
RAW Paste Data