主頁 > 知識(shí)庫 > 局域網(wǎng)設(shè)置自動(dòng)配置腳本文件的寫法與用途

局域網(wǎng)設(shè)置自動(dòng)配置腳本文件的寫法與用途

熱門標(biāo)簽:服務(wù)外包 百度競(jìng)價(jià)排名 地方門戶網(wǎng)站 AI電銷 呼叫中心市場(chǎng)需求 鐵路電話系統(tǒng) Linux服務(wù)器 網(wǎng)站排名優(yōu)化
因?yàn)橛信笥言谏钲诖髮W(xué),他們學(xué)校的網(wǎng)絡(luò)比較變態(tài)。如果訪問了教育網(wǎng)指定的免費(fèi)IP之外的IP,每M 6元錢。 
我本來想教她用二級(jí)代理之類的,不過無奈這個(gè)對(duì)她來講太難了。所以權(quán)宜之下,我只好讓她使用IE的自動(dòng)配置腳本文件來限制以防萬一,至少能保證她在使用瀏覽器上網(wǎng)的時(shí)候不會(huì)因?yàn)椴恍⌒脑L問了收費(fèi)的IP而挨宰。 
雖然說這么變態(tài)的學(xué)校不多,但是終究還是有的,所以把自動(dòng)配置腳本文件的寫法寫出來,如果有需要的朋友可以參考著來。 
首先我們先來介紹一下自動(dòng)配置腳本文件: 
打開IE,點(diǎn)擊"工具"--"Internet選項(xiàng)"--"局域網(wǎng)設(shè)置",你就可以看到"使用自動(dòng)配置腳本" 
自動(dòng)配置腳本起的作用就是,當(dāng)IE訪問網(wǎng)頁的時(shí)候會(huì)根據(jù)腳本文件里面界定的內(nèi)容來訪問。比方說,你在腳本文件里面限定了訪問某些IP使用某個(gè)代理的時(shí)候,訪問另外一些IP使用另外的代理,這就很方便通過腳本文件來完成。 
一個(gè)PAC文件其實(shí)就是一個(gè)文本文件,最簡(jiǎn)單的格式就是包含一個(gè)叫FindProxyForURL的JScript函數(shù),IE通過傳入兩個(gè)變量來調(diào)用這個(gè)函數(shù),一個(gè)是用戶瀏覽的地址URL全路經(jīng),一個(gè)是這個(gè)URL中的主機(jī)名部分(host)。 
這個(gè)FindProxyForURL函數(shù)有三種可能的字符串返回值,一是"DIRECT",就是直接連接,不通過代理;二是"PROXY proxyaddr:port",其中proxyaddr和port分別是代理的地址和代理的端口;三是"SOCKS socksaddr:port",其中socksaddr和port分別是socks代理的地址和端口,一個(gè)自動(dòng)代理文件可以是多個(gè)選擇的組合,其中用分號(hào)(;)隔開,如: 
function FindProxyForURL(url,host) 

if (host == "www.mydomain.com") 
return "DIRECT"; 
return "PROXY myproxy:80; 
PROXY myotherproxy:8080; 
DIRECT"; 

下面是代理腳本可能用到的函數(shù)和說明(英文不好的朋友可以直接跳過去看應(yīng)用): 
PAC Helper Functions 
dnsDomainIs(host, domain) Returns true if the host is part of the specified domain, false otherwise. 
isInNet(hostname, Resolves the hostname and subnet IP, subnet mask) returns true if the hostname is within the subnet 
specified by the IP address and the subnet mask, false otherwise. 
isPlainHostName(host) Returns true if there are no dots in the hostname, false otherwise. 
isResolvable(host) Internet Explorer tries to resolve the hostname through DNS and returns true if successful, false otherwise. 
localHostOrDomainIs Returns true if the host matches (host, domain) the host portion of the domain, or if the host matches the host and domain portions of the domain, false otherwise. (Executed only for URLs in the local domain.) 
dnsDomainLevels(host) Returns the number of dots in the hostname. 
dnsResolve(host) Returns a string containing the IP address of the specified host. 
myIPAddress( ) Returns a string containing the local machine's IP address. 
shExpMatch(url, shexp) Returns true if the supplied URL matches the specified shell expression, false otherwise. 
dateRange(parmList) Returns true if the current date falls within the dates specified in parmList, false otherwise. 
timeRange(parmList) Returns true if the current time falls within the times specified in parmList, false otherwise. 
weekdayRange(parmList) Returns true if today is within the days of the week specified in parmList, false otherwise. 
下面是各個(gè)函數(shù)應(yīng)用的例子: 
作者: xmudahai 2006-10-17 00:33   回復(fù)此發(fā)言 
--------------------------------------------------------------------------------
2 自動(dòng)配置腳本文件的寫法與用途(原創(chuàng)) 
a、isPlainHostName(host),本例演示判斷是否為本地主機(jī),如 http://myservername/ 
的方式訪問,如果是直接連接,否則使用代理 
function FindProxyForURL(url, host) 

if (isPlainHostName(host)) 
return "DIRECT"; 
else 
return "PROXY proxy:80"; 

b、dnsDomainIs(host, "")、localHostOrDomainIs(host, ""),本例演示判斷訪問主機(jī)是否屬于某個(gè)域和某個(gè)域名,如果屬于.company.com域的主機(jī)名,而域名不是company.com和home.company.com的直接連接,否則使用代理訪問。 
function FindProxyForURL(url, host) 

if ((isPlainHostName(host) ││ 
dnsDomainIs(host, ".company.com"))  
!localHostOrDomainIs(host, "www.company.com")  
!localHostOrDomainIs(host, "home.company.com")) 
return "DIRECT"; 
else 
return "PROXY proxy:80"; 

c、isResolvable(host),本例演示主機(jī)名能否被dns服務(wù)器解析,如果能直接訪問,否則就通過代理訪問。 
function FindProxyForURL(url, host) 

if (isResolvable(host)) 
return "DIRECT"; 
else 
return "PROXY proxy:80"; 

d、isInNet(host, "", ""),本例演示訪問IP是否在某個(gè)子網(wǎng)內(nèi),如果是就直接訪問,否則就通過代理,例子演示訪問清華IP段的主頁不用代理。 
function FindProxyForURL(url, host) 

if (isInNet(host, "166.111.0.0", "255.255.0.0")) 
return "DIRECT"; 
else 
return "PROXY proxy:80"; 

e、shExpMatch(host, ""),本例演示根據(jù)主機(jī)域名來改變連接類型,本地主機(jī)、*.edu 、*.com分別用不同的連接方式。 
function FindProxyForURL(url, host) 

if (isPlainHostName(host)) 
return "DIRECT"; 
else if (shExpMatch(host, "*.com")) 
return "PROXY comproxy:80"; 
else if (shExpMatch(host, "*.edu")) 
return "PROXY eduproxy:80"; 
else 
return "PROXY proxy:80"; 

f、url.substring(),本例演示根據(jù)不同的協(xié)議來選擇不同的代理,http、https、ftp、gopher分別使用不同的代理。 
function FindProxyForURL(url, host) 

if (url.substring(0, 5) == "http:") { 
return "PROXY proxy:80"; 

else if (url.substring(0, 4) == "ftp:") { 
return "PROXY fproxy:80"; 

else if (url.substring(0, 7) == "gopher:") { 
return "PROXY gproxy"; 

else if (url.substring(0, 6) == "https:") { 
return "PROXY secproxy:8080"; 

else { 
return "DIRECT"; 


g、dnsResolve(host),本例演示判斷訪問主機(jī)是否某個(gè)IP,如果是就使用代理,否則直接連接。 
unction FindProxyForURL(url, host) 

if (dnsResolve(host) == "166.111.8.237") { 
return "PROXY secproxy:8080"; 

else { 
return "PROXY proxy:80"; 


h、myIpAddress(),本例演示判斷本地IP是否某個(gè)IP,如果是就使用代理,否則直接使用連接。 
function FindProxyForURL(url, host) 

if (myIpAddress() == "166.111.8.238") { 
return "PROXY proxy:80"; 

else { 
return "DIRECT"; 


i、dnsDomainLevels(host),本例演示訪問主機(jī)的域名級(jí)數(shù)是幾級(jí),就是域名有幾個(gè)點(diǎn)如果域名中有點(diǎn),就通過代理訪問,否則直接連接。 
function FindProxyForURL(url, host) 

if (dnsDomainLevels(host) > 0) { // if number of dots in host > 0 
return "PROXY proxy:80"; 

return "DIRECT"; 

j、weekdayRange(),本例演示當(dāng)前日期的范圍來改變使用代理,如果是GMT時(shí)間周三到周六,使用代理連接,否則直接連接。 
function FindProxyForURL(url, host) 

if(weekdayRange("WED", "SAT", "GMT")) 
return "PROXY proxy:80"; 
else 
return "DIRECT"; 

k、最后一個(gè)例子是演示隨機(jī)使用代理,這樣可以好好利用代理服務(wù)器。 
function FindProxyForURL(url,host) 

return randomProxy(); 

function randomProxy() 

switch( Math.floor( Math.random() * 5 ) ) 

case 0: 
return "PROXY proxy1:80"; 
break; 
case 1: 
return "PROXY proxy2:80"; 
break; 
case 2: 
return "PROXY proxy3:80"; 
break; 
case 3: 
return "PROXY proxy4:80"; 
break; 
case 4: 
return "PROXY proxy5:80"; 
break; 


利用上面的函數(shù)和例子說明,大家就可以寫出比較復(fù)雜有效的自動(dòng)代理腳本。

標(biāo)簽:崇左 湖南 衡水 仙桃 黃山 銅川 蘭州 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《局域網(wǎng)設(shè)置自動(dòng)配置腳本文件的寫法與用途》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266