proxy.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. function proxy_on() {
  2. export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
  3. if (( $# > 0 )); then
  4. valid=$(echo $@ | sed -n 's/\([0-9]\{1,3\}.\)\{4\}:\([0-9]\+\)/&/p')
  5. if [[ $valid != $@ ]]; then
  6. >&2 echo "Invalid address"
  7. return 1
  8. fi
  9. export
  10. http_proxy="http://$1/" \
  11. https_proxy=$http_proxy \
  12. ftp_proxy=$http_proxy \
  13. rsync_proxy=$http_proxy
  14. echo "Proxy environment variable set."
  15. return 0
  16. fi
  17. echo -n "username: "; read username
  18. if [[ $username != "" ]]; then
  19. echo -n "password: "
  20. read -es password
  21. local pre="$username:$password@"
  22. fi
  23. echo -n "server: "; read server
  24. echo -n "port: "; read port
  25. export http_proxy="http://$pre$server:$port/" \
  26. https_proxy=$http_proxy \
  27. ftp_proxy=$http_proxy \
  28. rsync_proxy=$http_proxy \
  29. HTTP_PROXY=$http_proxy \
  30. HTTPS_PROXY=$http_proxy \
  31. FTP_PROXY=$http_proxy \
  32. RSYNC_PROXY=$http_proxy
  33. }
  34. function proxy_off(){
  35. unset http_proxy https_proxy ftp_proxy rsync_proxy \
  36. HTTP_PROXY HTTPS_PROXY FTP_PROXY RSYNC_PROXY
  37. echo -e "Proxy environment variable removed."
  38. }