$7.49 .com domain with free InstantPage Website Builder$1.99 Web Hosting   捷飞网络官方淘宝店   Godaddy 优惠码
返回列表 发帖

[Tools] Wget Proxy: How to run Wget behind a Proxy server

Since, proxy servers are very common today - in companies, universities etc. almost all of the applications today have proxy support. As far as wget is concerned, there are two ways of doing so, either set the appropriate environment variables and wget will act according to them or configure the wget configuration file - wgetrc.* S# l% ~* {' y  l

3 P# m- T0 k/ D5 C. @Wget Proxy: Set the environment variable
' r; ?4 Z" ^) w# A* v( G( n# u8 k5 S- P9 o2 s5 n
Before setting the environment variables, lets first see if there are any preset proxy variables present. We can get that information using the following command.
, N5 H9 h1 e' O) m8 v; ]! Y( d5 V; S: X. S2 b- `- _
[shredder12]$ env | grep proxy
( e2 t. Z" o; r8 b& _8 t2 |2 }; l6 Z5 f6 T3 S( L* T% j
http_proxy=http://foo.bar:3128" A  Q" B0 t# \2 i/ q; n9 N
no_proxy=localhost, ]# b8 l9 u: Q, Z5 Q% Q

+ Q. X" w/ h) {If no proxy environment variable is set, it will go for direct connections. Lets take an example of my college. We use a HTTP proxy server. Running wget without setting the http_proxy environment variable just fails. So, lets set this env. variable first0 Q, U$ |) o- x: A; ]

5 b" f; \+ Y( c: `[shredder12]$ export http_proxy="http://foo.bar:8080"8 u; d0 D2 N1 {# I) S3 _

! I1 b. w* w7 I7 ]3 f1 f4 z  M4 eNow, run Wget and it should work like a charm. If your proxy also requires authentication i.e username & password then use this format. ~3 A  P& ~2 ?$ x. f
( e% P, K+ z+ W* e: n* W
[shredder12]$ export http_proxy="http://username:password@foo.bar:8080"$ ]; a; x7 |" ]7 U
, q. r, K; j+ ^9 B9 H  ~
Similarly, for other type of proxies - ftp_proxy, https_proxy etc. All we need to do is set the appropriate environment variable.
* H9 M; L1 @  B, b/ W+ [+ f% [6 D; W% l& O+ F/ y
Please note that, the above commands will be in effect only till the user session expires, i.e. by running the above commands we are just setting the proxy env. for a session, not permanently. And usually we wouldn't want to run the command everytime we login. So, to make it permanent, we can either set the value in wget's configuration - wgetrc file or set the environment in ~/.bashrc file.
7 C7 \; b5 g7 k( X/ g
; m5 _. s9 U, x: ?% j[shredder12]$ echo "export http_proxy=http://foo.bar:8080/" | tee -a ~/.bashrc
) V+ @. T" U7 z
4 Y8 I0 B: O' W, Q0 `/ ^It should be noted that, this way we might be affecting the user's proxy settings. A better way is to set the settings permanently in wget's configuration file, this way no other app. is affected.
- D; z( h6 e$ J6 C
7 V+ ~1 @& Q# H% @Wget Proxy: Configuring the wgetrc file" E8 J# {; R* M' G

2 L5 y2 {3 p9 `* U7 zLike most of the applications wget has a configuration file too - wgetrc:! s3 |$ u# b: ~0 |/ u
. a3 n( Y( K* V% W& c# u% }
2 g1 m* `$ z1 J, J# y3 V~/.wgetrc.
# V8 n+ f; d6 K1 l% Q" ]; KThe former is for global changes and the latter one is for local settings(user specific). We will go into the details later, lets just see how to apply the proxy settings. Its similar to setting the proxy environment variable, just exclude the export command. Open the file ~/.wgetrc file. If one doesn't exist, then create it.) M& J4 ]6 i' j: F

  J* v7 U4 t. G[shredder12]$ vim ~/.wgetrc
/ p' E6 h3 G& I( a9 ?# D% f& c' ?" G) S% O6 y* C; P
Now, add the following statement in the file& }) F7 e$ M3 N6 a
! `) d4 z- `' _1 ?' V8 y  r" ~, {
# e  b6 T: A1 P% e6 p' z. `' L! v+ F7 C5 j
Wget Proxy: How to disable/turn-off the proxy settings
, L" H! ]2 s: V$ q
& O* j: l/ C6 AOnce you have configured the proxy settings in the wgetrc file, everytime you run wget, it will automatically connect to the proxy server. Sometimes for local/LAN based downloads, you may not need the proxy server. In such cases we can turn off the proxy settings using --no-proxy argument in the command/ j: e# X+ K) G- ~7 P2 T0 U: I$ ~7 Q% `
  Y# D+ _, U- _, p( X( O) V; b+ o
[shredder12]$ wget --no-proxy
2 x3 u2 K+ z2 K' m2 y; f1 j0 v' W3 g6 e8 r$ Q  ]
Wget Proxy: If the proxy requries authentication - Username & Password5 G+ P' K" |+ A2 r7 \, R

3 Q* Z9 `8 K7 K3 `7 {' sWe already know the format for setting the environment variable with username and password. Wget provides you two methods to mention the username and password for proxy server.; c, b+ y; t- ]* b
. W3 I. ^0 ?' s! a, s" b6 j
Mention it in the command in the form of arguments.
! d' Z' I( f, X! YSet the values in the configuration file, /etc/wgetrc or ~/.wgetrc3 t& t7 M4 p& m: M
If the authentication you are using is static, then its better t set it in the configuration file. If its just one time thing or if you want to overwrite the settings in configuration file then you can use the --proxy-user & --proxy-password options.
; c$ \! J5 s7 W% t) k0 Q1 e
7 b1 K2 F; }- x  }9 }[shredder12]$ wget --proxy-user=username --proxy-password=password http://foo.bar/file.tgz( v$ i, k* ?: s% G

; t& g, ^& K: O, e( fFor permanent changes, its always advised to change the configuration file.
4 p/ S, P) K+ u; \! t! s4 K% U& X' c
Once again, for global changes modify the /etc/wgetrc file otherwise go for ~/.wgetrc file. Open any of the file and append/add the following lines in it.
) y1 ?% y; M3 U& p9 ?4 S- y% D) f
, R% A) W; V& T/ e! F! Vproxy_user=username8 X2 `9 {/ {  s3 w
4 B1 t) S6 Q& A" Q3 O6 |( a( B, |1 K8 s& H- p: [
In a similar way, you can set the ftp proxy along with the authencation credentials.
3 j4 ?: T' O! t% d  ?/ B! c& G* a- E4 P1 b
How to ovewrite a preset proxy in Wget0 r7 a% O8 ]3 m. A0 U
' |1 }4 c' j9 n; Z' w. r- @
If you see the first proxy example, we are actually overwriting the pre-defined environment variable(if any). But since, we are executing the export command, rather than editing any configuration file, the proxy setting will be removed as soon as we close the terminal ( in case of GUI, gnome-terminal etc.) or logout of the session.
2 v2 m4 K5 |1 B+ y$ Q; o# Z; \) L. i* G2 J3 n: R7 }
And, if for some reason we are interested in using separate proxy for wget, then instead of overwriting the variable we can just edit the ~/.wgetrc or /etc/wgetrc file. Please note that, a local wgetrc file (~/.wgetrc) will overwrite the settings on /etc/wgetrc. Once again, remember that ~/.wgetrc is a local/user-specific setting where as /etc/wgetrc is a global one.. J9 g" L& l: Z; u& t* l: j: I

5 e, I/ ]* @6 u6 R5 \Finally, the high command in this hierarchy are the variables provided at command execution time. (--proxy-password, --no-proxy etc.), i.e." ~# Q  M* W; l8 o

/ S) O. M$ p: y* U; l8 k0 l(Exec. time command options) > ~/.wgetrc > /etc/wgetrc > environment
5 J% v" u6 m1 N8 m  w8 F* B5 H' H
- ~  Y+ G- X$ \3 G0 l+ D2 ~1 t' lThis is just a big picture, only for proxy settings. It might not be the same in all the cases.* w, G& L$ ?5 a; ^6 `6 c
" |* O8 D' i% K( G
参考: http://linuxers.org/tutorial/wget-proxy-how-run-wget-behind-proxy-server

Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting