Pages

Friday, February 9, 2018

Setting http_proxy based on IP address


In office http_proxy is required . This script will help to set the http_proxy

1. Create python script to get IP

2. Add in alias for every bash login


Python Script :

 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
#!/bin/python





import socket



officeProxy='Myoffice:80'



def get_ip():

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    try:

        # doesn't even have to be reachable

        s.connect(('10.255.255.255', 1))

        IP = s.getsockname()[0]

    except:

        IP = '127.0.0.1'

    finally:

        s.close()

    return IP









def setProxyINBashShell():

    if get_ip().startswith("10"):

      print officeProxy



    else :

      print ''



if __name__ == '__main__':

    setProxyINBashShell()


2. Alias

#### Smart Proxy
 http_proxy=`/home/sterin/Templates/shell/smartProxy.py` 
 export http_proxy=$http_proxy



Code is highlighted using : http://hilite.me/