linux命令行模式下实现代理上网 专题
有些公司的局域网环境,例如我们公司的只允许使用代理上网,图形界面的很好解决就设置一下浏览器的代理就好了,但是linux纯命令行的界面就....下面简单几步就可以实现了!
一、命令行界面的一般代理设置方法
1、在profile文件中设置相关环境变量
# vi /etc/profile
http_proxy=192.168.10.91:3128 # 分别指定http、https、ftp协议使用的代理服务器地址
https_proxy=192.168.10.91:3128
ftp_proxy=192.168.10.91:3128
no_proxy=192.168.10.0. # 访问局域网地址(192.168.20.0/24网段)时不使用代理,可以用逗号分隔多个地址
export http_proxy https_proxy ftp_proxy no_proxy
保存退出,注销重新登陆系统即可生效。
现在代理上网时没有问题了,如果使用的是debian的系统要使用apt-get怎么办?
三种方法:
1.临时设置让 apt-get 使用代理
直接在命令行下输入 export http_proxy=http://yourproxyaddress:proxyport
2.永久设置
编辑/etc/apt/apt.conf文件,在文件末尾加入
Acquire::http::Proxy "http://yourproxyaddress:proxyport
有的系统安装时没有apt.conf文件需要手动建立,此方法仅是给apt-get设置代理
3.为apt和其他应用程序,如wget设置代理
编辑.bashrc文件,在文件末尾添加
http_proxy=http://yourproxyaddress:proxyport
export http_proxy保存文件,退出重新登录设置生效。
http://www.cnblogs.com/AloneSword/p/3394806.html
Command Line JVM Settings
The proxy settings are given to the JVM via command line arguments:
java -Dhttp.proxyHost=proxyhostURL
-Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=someUserName
-Dhttp.proxyPassword=somePassword HelloWorldClass
Setting System Properties in Code
Add the following lines in your Java code so that JVM uses the proxy to make HTTP calls. This would, of course, require you to recompile your Java source. (The other methods do not require any recompilation):
System.getProperties().put("http.proxyPort", "someProxyPort");
System.getProperties().put("http.proxyUser", "someUserName");
System.getProperties().put("http.proxyPassword", "somePassword");
System.getProperties().put("http.proxyHost", "someProxyURL");
One more configuraiton for Microsoft ISA NTLM Authentication
You need add below in addition is the proxy using NTLM authentication.
-Dhttp.auth.ntlm.domain=DOMAIN
Configuring Authentication Order
There are three authentication mechanisms provided - ntlm, digest & basic in that order by default. It is possible to change the order and also not exclude one or more methods altogether. This can be done with the following property.
-Dhttp.proxyAuth=basic,ntlm
In this example the authentication order will be basic and then ntlm only - digest will not be used.
Since Java 1.5 you can also pass a java.net.Proxy instance to the openConnection() method
//Proxy instance, proxy ip = 123.0.0.1 with port 8080
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("123.0.0.1", 8080));
URL url = new URL("http://www.yahoo.com");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect(); String page;
StringBuffer tmp = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while ((line = in.readLine()) != null){
page.append(line + "\n");
}
System.out.println(page);
so you don't need to set system properties. You can use the default PROXY as defined by your networking settings.
System.setProperty("java.net.useSystemProxies", "true");
List l = null;
try {
l = ProxySelector.getDefault().select(new URI("http://www.yahoo.com"));
}
catch (URISyntaxException e) {
e.printStackTrace();
}
if (l != null) {
for (Iterator iter = l.iterator(); iter.hasNext() {
java.net.Proxy proxy = (java.net.Proxy) iter.next();
System.out.println("proxy hostname : " + proxy.type());
InetSocketAddress addr = (InetSocketAddress) proxy.address();
if (addr == null) {
System.out.println("No Proxy");
}
else {
System.out.println("proxy hostname : " + addr.getHostName());
System.out.println("proxy port : " + addr.getPort());
}
}
}
To bypass the PROXY,
URL url = new URL("http://internal.server.local/");
URLConnection conn = url.openConnection(Proxy.NO_PROXY);
Proxy and Username/Password
You might need to identify yourself to the proxy server.
One way is to use the HTTP property "Proxy-Authorization" with a username:password base64 encoded.
System.setProperty("http.proxyHost", "myProxyServer.com");
System.setProperty("http.proxyPort", "80");
URL url=new URL("http://someserver/somepage");
URLConnection uc = url.openConnection ();
String encoded = new String
(Base64.base64Encode(new String("username:password").getBytes()));
uc.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
uc.connect();
The following example dumps the content of a URL but before we identify ourself to the proxy.
import java.net.*;
import java.io.*; public class URLUtils {
public static void main(String s[]) {
URLUtils.dump("http://www.yahoo.com");
System.out.println("**************");
URLUtils.dump("https://www.paypal.com");
System.out.println("**************");
} public static void dump(String URLName){
try {
DataInputStream di = null;
FileOutputStream fo = null;
byte [] b = new byte[1]; // PROXY
System.setProperty("http.proxyHost","proxy.mydomain.local") ;
System.setProperty("http.proxyPort", "80") ; URL u = new URL(URLName);
HttpURLConnection con = (HttpURLConnection) u.openConnection();
//
// it's not the greatest idea to use a sun.misc.* class
// Sun strongly advises not to use them since they can
// change or go away in a future release so beware.
//
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
String encodedUserPwd =
encoder.encode("mydomain\\MYUSER:MYPASSWORD".getBytes());
con.setRequestProperty
("Proxy-Authorization", "Basic " + encodedUserPwd);
// PROXY ---------- di = new DataInputStream(con.getInputStream());
while(-1 != di.read(b,0,1)) {
System.out.print(new String(b));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
With JDK1.2, the java.net.Authenticator can be used to send the credentials when needed.
public static void dump(String URLName){
try {
DataInputStream di = null;
FileOutputStream fo = null;
byte [] b = new byte[1];
// PROXY
System.setProperty("http.proxyHost","proxy.mydomain.local") ;
System.setProperty("http.proxyPort", "80") ;
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new
PasswordAuthentication("mydomain\\username","password".toCharArray());
}});
URL u = new URL(URLName);
HttpURLConnection con = (HttpURLConnection) u.openConnection();
di = new DataInputStream(con.getInputStream());
while(-1 != di.read(b,0,1)) {
System.out.print(new String(b));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
Bypass a Proxy
In intranet environment, you may need to bypass the proxy server and go directly to the http server.
The http.nonProxyHosts property indicates the hosts which should be connected too directly and not through the proxy server. The value can be a list of hosts, each seperated by a |, and in addition a wildcard character (*) can be used for matching.
java.exe -Dhttp.nonProxyHosts="*.mycompany.com|*.mycompany.local|localhost" MyClass
Below are the Networking Properties from http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html
There are a few standard system properties used to alter the mechanisms and behavior of the various classes of the java.net package.
Some are checked only once at startup of the VM, and therefore are best set using the -D option of the java command,
while others have a more dynamic nature and can also be changed using the System.setProperty() API.
The purpose of this document is to list and detail all of these properties.
If there is no special note, a property value is checked every time it is used.
IPv4 / IPv6
java.net.preferIPv4Stack (default: false)
If IPv6 is available on the operating system the underlying native socket will be, by default,
an IPv6 socket which lets applications connect to, and accept connections from, both IPv4 and IPv6 hosts.
However, in the case an application would rather use IPv4 only sockets, then this property can be set to true.
The implication is that it will not be possible for the application to communicate with IPv6 only hosts.
java.net.preferIPv6Addresses (default: false)
When dealing with a host which has both IPv4 and IPv6 addresses, and if IPv6 is available on the operating system,
the default behavior is to prefer using IPv4 addresses over IPv6 ones. This is to ensure backward compatibility,
for example applications that depend on the representation of an IPv4 address (e.g. 192.168.1.1).
This property can be set to true to change that preference and use IPv6 addresses over IPv4 ones where possible.
Both of these properties are checked only once, at startup.
Proxies
A proxy server allows indirect connection to network services and is used mainly for security (to get through firewalls)
and performance reasons (proxies often do provide caching mechanisms).
The following properties allow for configuration of the various type of proxies.
- HTTP
The following proxy settings are used by the HTTP protocol handler.
http.proxyHost (default: <none>) The hostname, or address, of the proxy server
http.proxyPort (default: 80) The port number of the proxy server.
http.nonProxyHosts (default: localhost|127.*|[::1])
Indicates the hosts that should be accessed without going through the proxy. Typically this defines internal hosts.
The value of this property is a list of hosts, separated by the '|' character.
In addition the wildcard character '*' can be used for pattern matching.
For example -Dhttp.nonProxyHosts=”*.foo.com|localhost” will indicate that every hosts in the foo.com domain
and the localhost should be accessed directly even if a proxy server is specified.
The default value excludes all common variations of the loopback address.
- HTTPS
This is HTTP over SSL, a secure version of HTTP mainly used when confidentiality (like on payment sites) is needed.
The following proxy settings are used by the HTTPS protocol handler.
https.proxyHost(default: <none>)
The hostname, or address, of the proxy server
https.proxyPort (default: 443)
The port number of the proxy server.
The HTTPS protocol handler will use the same nonProxyHosts property as the HTTP protocol.
- FTP
The following proxy settings are used by the FTP protocol handler.
ftp.proxyHost(default: <none>)
The hostname, or address, of the proxy server
ftp.proxyPort (default: 80)
The port number of the proxy server.
ftp.nonProxyHosts (default: localhost|127.*|[::1])
Indicates the hosts that should be accessed without going through the proxy. Typically this defines internal hosts.
The value of this property is a list of hosts, separated by the '|' character. In addition the wildcard character '*' can be
used for pattern matching.
For example -Dftp.nonProxyHosts=”*.foo.com|localhost” will indicate that every hosts in the foo.com
domain and the localhost should be accessed directly even if a proxy server is specified.
The default value excludes all common variations of the loopback address.
- SOCKS
This is another type of proxy. It allows for lower level type of tunneling since it works at the TCP level.
In effect, in the Java(tm) platform setting a SOCKS proxy server will result in all TCP connections to go through that proxy,
unless other proxies are specified. If SOCKS is supported by a Java SE implementation, the following properties will be used:
socksProxyHost (default: <none>)
The hostname, or address, of the proxy server.
socksProxyPort (default: 1080)
The port number of the proxy server.
socksProxyVersion (default: 5)
The version of the SOCKS protocol supported by the server. The default is 5 indicating SOCKS V5, alternatively 4 can be specified for SOCKS V4.
Setting the property to values other than these leads to unspecified behavior.
java.net.socks.username (default: <none>)
Username to use if the SOCKSv5 server asks for authentication and no java.net.Authenticator instance was found.
java.net.socks.password (default: <none>)
Password to use if the SOCKSv5 server asks for authentication and no java.net.Authenticator instance was found.
Note that if no authentication is provided with either the above properties or an Authenticator, and the proxy requires one,
then the user.name property will be used with no password.
java.net.useSystemProxies (default: false)
On recent Windows systems and on Gnome 2.x systems it is possible to tell the java.net stack, setting this property to true,
to use the system proxy settings (both these systems let you set proxies globally through their user interface).
Note that this property is checked only once at startup.
- Misc HTTP properties
http.agent (default: “Java/<version>”)
Defines the string sent in the User-Agent request header in http requests. Note that the string “Java/<version>” will be appended
to the one provided in the property (e.g. if -Dhttp.agent=”foobar” is used, the User-Agent header will contain “foobar Java/1.5.0”
if the version of the VM is 1.5.0). This property is checked only once at startup.
http.keepalive (default: true)
Indicates if persistent connections should be supported. They improve performance by allowing the underlying socket
connection to be reused for multiple http requests. If this is set to true then persistent connections will be requested with HTTP 1.1 servers.
http.maxConnections (default: 5)
If HTTP keepalive is enabled (see above) this value determines the maximum number of idle connections that will be simultaneously kept alive,
per destination.
http.maxRedirects (default: 20)
This integer value determines the maximum number, for a given request, of HTTP redirects that will be automatically followed by the protocol handler.
http.auth.digest.validateServer (default: false)
http.auth.digest.validateProxy (default: false)
http.auth.digest.cnonceRepeat (default: 5)
These 3 properties modify the behavior of the HTTP digest authentication mechanism. Digest authentication
provides a limited ability for the server to authenticate itself to the client (i.e. By proving it knows the user's password).
However not all HTTP servers support this capability and by default it is turned off.
The first two properties can be set to true to enforce this check for authentication with either an origin or proxy server, respectively.
It is usually not necessary to change the third property. It determines how many times a cnonce value is re-used.
This can be useful when the MD5-sess algorithm is being used. Increasing this value reduces the computational overhead
on both client and server by reducing the amount of material that has to be hashed for each HTTP request.
http.auth.ntlm.domain (default: <none>)
NTLM is another authentication scheme. It uses the java.net.Authenticator class to acquire usernames and passwords
when they are needed. However NTLM also needs the NT domain name. There are 3 options for specifying that domain:
Do not specify it. In some environments the domain is actually not required and the application does not have to specify it.
The domain name can be encoded within the username by prefixing the domain name, followed by a back-slash '\' before the username.
With this method existing applications that use the authenticator class do not need to be modified,
as long as users are made aware that this notation must be used.
If a domain name is not specified as in method 2) and these property is defined, then its value will be used a the domain name.
All these properties are checked only once at startup.
- Address Cache
The java.net package, when doing name resolution, uses an address cache for both security and performance reasons.
Any address resolution attempt, be it forward (name to IP address) or reverse (IP address to name),
will have its result cached, whether it was successful or not, so that subsequent identical requests will not have to access the naming service.
These properties allow for some tuning on how the cache is operating.
networkaddress.cache.ttl (default: see below)
Value is an integer corresponding to the number of seconds successful name lookups will be kept in the cache. A value of -1,
or any other negative value for that matter, indicates a “cache forever” policy, while a value of 0 (zero) means no caching.
The default value is -1 (forever) if a security manager is installed, and implementation specific when no security manager is installed.
networkaddress.cache.negative.ttl (default: 10)
Value is an integer corresponding to the number of seconds an unsuccessful name lookup will be kept in the cache. A value of -1,
or any negative value, means “cache forever”, while a value of 0 (zero) means no caching.
Since these 2 properties are part of the security policy, they are not set by either the -D option or the System.setProperty() API,
instead they are set in the JRE security policy file lib/security/java.security.
http://docs.oracle.com/javase/6/docs/technotes/guides/net/properties.html
http://www.cnblogs.com/princessd8251/p/3819327.html
demo:
HTTP_PROXY="baidu.com:1234" http_proxy=$HTTP_PROXY
https_proxy=$HTTP_PROXY
ftp_proxy=$HTTP_PROXY
no_proxy=$HTTP_PROXY export http_proxy https_proxy ftp_proxy no_proxy export JAVA_OPTS="-Dhttp.proxySet=true -Dhttp.proxyHost=10.163.214.196 -Dhttp.proxyPort=3128 -Dhttp.nonProxyHosts=<domain_one>|<domain_two> $JAVA_OPTS"
Getting HTTP Connections to Use Proxy
In android, programmatic access to HTTP resources typically uses the HttpURLConnection class. You can tell it to use a proxy by setting some system properties:
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", port);
System.setProperty("https.proxyHost", host);
System.setProperty("https.proxyPort", port);
You can disable proxying by clearing those properties:
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
System.clearProperty("https.proxyHost");
System.clearProperty("https.proxyPort");
However, there is one big caveat - HttpURLConnection uses keep-alives to reuse existing TCP connections. These TCP connections will still be using the old proxy settings. This has several implications:
Set the proxy settings as early in the application's lifecycle as possible, ideally before any HttpURLConnections have been opened.
Don't expect the settings to take effect immediately if some HttpURLConnections have already been opened.
Disable keep-alives if you need to, which you can do like this:
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// Need to force closing so that old connections (with old proxy settings) don't get reused.
urlConnection.setRequestProperty("Connection", "close");
https://github.com/getlantern/lantern
linux命令行模式下实现代理上网 专题的更多相关文章
- linux命令行模式下实现代理上网
有些公司的局域网环境,例如我们公司的只允许使用代理上网,图形界面的很好解决就设置一下浏览器的代理就好了,但是linux纯命令行的界面就....下面简单几步就可以实现了! 一.命令行界面的一般代理设置方 ...
- linux命令行模式下实现代理上网(转)
有些公司的局域网环境,例如我们公司的只允许使用代理上网,图形界面的很好解决就设置一下浏览器的代理就好了,但是linux纯命令行的界面就....下面简单几步就可以实现了! 一.命令行界面的一般代理设置方 ...
- Linux命令行模式下安装VMware Tools详细步骤
在Linux命令行模式安装VMware Tools 方法/步骤1: 首先启动CentOS 7,在VMware中点击上方"VM",点击"Install VMware Too ...
- linux命令行模式下对FTP服务器进行文件上传下载
参考源:点击这里查看 1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码 ...
- linux 命令行模式下,浏览网页
Ubuntu自带最新版的Gnome桌面,拥有大量的服务和桌面应用程序,让您仅通过一张安装光盘就可以体验到无比舒适的操作环境.下文介绍的在ubuntu下使用终端命令行上网的方法. 第一步,需要安装一个名 ...
- linux 命令行模式下,浏览网页方法
Ubuntu自带最新版的Gnome桌面,拥有大量的服务和桌面应用程序,让您仅通过一张安装光盘就可以体验到无比舒适的操作环境.下文介绍的在ubuntu下使用终端命令行上网的方法. 第一步,需要安装一个名 ...
- Linux在终端命令行模式下智能补全功能以及组合键
linux命令行下也有很多热键(快捷键).先来看看tab键 1.如果想看看linux下以c开头的命令可直接在命令行下敲入c然后连续敲两次tab,再选择y,会显示所有以c开头的命令. 2.涉及到文件时, ...
- wpa_supplicant 移植及 linux 命令行模式配置无线上网
本文涉及内容为linux 命令行模式配置无线上网 及 wpa_supplicant 移植到开发板的过程,仅供参考. 1.源码下载 wpa_supplicant 源码下载地址 :http://hosta ...
- 在Linux命令行模式安装VMware Tools
在Linux命令行模式安装VMware Tools 方法/步骤1: 首先启动CentOS 7,在VMware中点击上方“VM”,点击“Install VMware Tools...”(如已安装则显示“ ...
随机推荐
- STS 3.6.4 SpringMVC 4.1.6 Hibernate 4.3.8 MySQL
开发环境: Java 1.8 Spring Tool Suite 3.6.4 Spring faramework 4.1.6 Hibernate 4.3.8 Maven 2.9 数据库是MySQL 5 ...
- ReentrantLock Condition await signal 专题
Condition的执行方式,是当在线程T1中调用await方法后,线程T1将释放锁,并且将自己阻塞,等待唤醒, 线程T2获取到锁后,开始做事,完毕后,调用Condition的signal方法,唤醒线 ...
- 通过rinetd实现port转发来訪问内网的服务
一. 问题描写叙述 通过外网来訪问内网的服务 二. 环境要求 须要有一台能够外网訪问的机器做port映射.通过数据包转发来实现外部訪问阿里云的内网服务 三. 操作方法 做port映射的方案 ...
- 数组[0]和[firstobject]的区别
数组[0]和[firstobject]的区别 [0]:数组为空时回报错 [firstobject]:数组为空时回返回nil
- 【网络】无法解析服务器的DNS地址?;能登陆QQ,无法打开网页
1. 无法解析服务器的DNS地址 手动设置 DNS(域名解析服务器) 8.8.8.8 114.114.114.114 清除浏览器缓存: 重启主机: 无法解析服务器的DNS地址?DNS解析错误怎么办? ...
- 通过引入SiteMesh的JSP标签库,解决Freemarker与SiteMesh整合时,自定义SiteMesh标签的问题
不少web项目,都用到了SiteMesh.SiteMesh可以和JSP.Freemarker等模版进行整合,有一定的好处,当然也有其不好的地方.我个人觉得,如果没有必要,不要在项目中引入太多的工具和技 ...
- Oracle数据库零散知识02
15,函数的创建,要求必须有返回值,必须在语句中调用,需要多个返回值时,使用out参数类型,在user_procedures表中查询属性,在user_source表中查询源代码,创建示例: CREAT ...
- Java冒泡排序与直接选择排序代码随笔
冒泡排序:延申的有很多种,有的是先确定最大值放到后面,有的是先确定最小值放到前边,还有就是反过来,先确定最小值的位置,但是本质都是:不断两两比较,交换位置...第一趟确定一个最大(最小)值放到前边(后 ...
- 瀑布流的一些CSS实现方式
一个选择是用CSS3的多列columns,可以参考这篇文章.但这篇文章给的例子并不怎么好理解,我做了一些更改,在每个元素上加了序号.可以看到,多列布局是在每一列上依次排列元素的,第一列排完才开始排第二 ...
- 【BZOJ 1024】 [SCOI2009]生日快乐
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1024 [题意] [题解] 要求恰好分成n个部分;每个部分的面积都一样; 则dfs的时候 ...