如何通過自己的ADSL使家里的電腦成為服務(wù)器呢?像花生殼這樣的應(yīng)用可以幫助你動(dòng)態(tài)解析ip,不過這個(gè)程序太龐大了,根本沒有必要。
下面介紹我的做法
條件
? 上網(wǎng)方式:ADSL
? 臺(tái)式機(jī):linux
? 中繼網(wǎng)頁:在某虛擬主機(jī)申請(qǐng)一個(gè)免費(fèi)空間,需要支持動(dòng)態(tài)腳本(php、jsp等)
方案
??在本機(jī)運(yùn)行一個(gè)Java程序,定時(shí)讀取本機(jī)的外網(wǎng)IP,自動(dòng)向中繼網(wǎng)頁用GET方式提交該數(shù)據(jù)。中繼網(wǎng)頁保存IP記錄在內(nèi)存中,其他訪問者可以通過該頁面查看當(dāng)前的IP。

package ?tedeyang;

import ?java.io.BufferedReader;
import ?java.io.IOException;
import ?java.io.InputStreamReader;
import ?java.net.InetAddress;
import ?java.net.URL;
import ?java.net.URLConnection;
import ?java.net.UnknownHostException;
import ?java.util.Date;
import ?java.util.Timer;
import ?java.util.TimerTask;

public ? class ?UpdateIp? {
????
static ?String?page? = ? " http://xxx.xxx.com/name/link.jsp?ip= " ; // ?need?'ip='
???? static ?String?strategy? = ? " ifconfig " ; // ?or?jdk
???? static ? long ?refreshTime? = ? 5L ? * ? 60 ? * ? 1000 ;? // ?5?minutes

????
/**
?????*?
@param ?args
?????*?
@throws ?IOException
?????
*/

????
public ? static ? void ?main( final ?String[]?args)? throws ?IOException? {
????????
if ?(args.length? <= ? 1 )? {
????????????System.out.println(
" Parameter?error?! " );
????????????System.out
????????????????????.println(
" should?be?followed?by?three?parameters?:?updatePage?ipStrategy?refreshMinutes,?at?least?updatePage " );
????????????System.out.println(
" ????updatePage?likes?'http://host/xxx.jsp?ip=' " );
????????????System.out.println(
" ????ipStrategy?maybe?is?jdk?or?ifconfig,default?is?ifconfig " );
????????????System.out.println(
" ????refreshMinutes?'s?unit?is?minute,?default?is?5?minutes " );
????????????
return ;
????????}

????????page?
= ?args[ 0 ];
????????
if ?( ! page.endsWith( " ip= " ))
????????????page?
+= ? " ?ip= " ;? // ??
????????strategy? = ?args[ 1 ];
????????
try ? {
????????????refreshTime?
= ?Integer.parseInt(args[ 2 ]);
????????????refreshTime?
*= ? 60 ? * ? 1000 ;
????????}
? catch ?(Exception?e)? {
????????}

????????
// cycle
???????? final ?Timer?t? = ? new ?Timer();
????????t.schedule(
new ?TimerTask()? {
????????????
public ? void ?run()? {
????????????????
try ? {
????????????????????UpdateIp.run(page,?strategy);
????????????????}
? catch ?(IOException?e)? {
????????????????????e.printStackTrace();
????????????????????
// ?t.cancel();
????????????????}

????????????}
;
????????}
,? 0 ,?refreshTime);
????}


????
private ? static ? void ?run(String?page,?String?ipStrategy)? throws ?IOException? {

????????String?ip?
= ? " no " ;
????????
if ?(ipStrategy.equals( " jvm " ))
????????????ip?
= ?getIpByJavaApi();
????????
else
????????????ip?
= ?getIpByLinuxIfconfig();
????????System.out.print(
new ?Date()? + ? " ,?this?ip?is?: " ? + ?ip);
????????touchIp(page,?ip);
????????System.out.println(
" ?,?touch?successed. " );
????}


????
private ? static ? void ?touchIp(String?page,?String?ip)? throws ?IOException? {
????????URL?url?
= ? null ;
????????url?
= ? new ?URL(page? + ?ip);
????????URLConnection?con?
= ? null ;
????????BufferedReader?reader?
= ? null ;
????????
try ? {
????????????con?
= ?url.openConnection();
????????????con.connect();
????????????reader?
= ? new ?BufferedReader( new ?InputStreamReader(con.getInputStream()));
????????????reader.readLine();
????????}
? finally ? {
????????????
try ? {
????????????????reader.close();
????????????????con?
= ? null ;
????????????}
? catch ?(IOException?e)? {
????????????????e.printStackTrace();
????????????}

????????}

????}


????
private ? static ?String?getIpByJavaApi()? throws ?UnknownHostException? {
????????String?ip?
= ? " none " ;
????????InetAddress[]?all?
= ?InetAddress.getAllByName(InetAddress.getLocalHost()
????????????????.getHostName());
????????
// ?通過本機(jī)主機(jī)名,遍歷多個(gè)ip
???????? for ?( int ?i? = ? 0 ;?i? < ?all.length;?i ++ )? {
????????????String?ip2?
= ?all[i].getHostAddress();
????????????
if ?( ! ip2.startsWith( " 127. " )? && ? ! ip2.startsWith( " 192. " )
????????????????????
&& ? ! ip2.startsWith( " 10. " )? && ? ! ip2.startsWith( " 172. " ))? {
????????????????ip?
= ?ip2;
????????????}

????????}

????????
return ?ip;
????}


????
private ? static ?String?getIpByLinuxIfconfig()? throws ?IOException? {
????????String?ip?
= ? " none " ;
????????Process?pre?
= ?Runtime.getRuntime().exec(
????????????????
new ?String[]? {? " sh " ,? " -c " ,? " ifconfig?ppp0?|?grep?addr " ?} );
????????BufferedReader?reader?
= ? null ;
????????
try ? {
????????????reader?
= ? new ?BufferedReader( new ?InputStreamReader(pre
????????????????????.getInputStream()));
????????????String?line?
= ?reader.readLine();
????????????
if ?(line? != ? null ? && ?line.matches( " .*addr.* " ))? {
????????????????
int ?s? = ?line.indexOf( " addr: " )? + ? 5 ;
????????????????
int ?e? = ?line.indexOf( " ? " ,?s);
????????????????ip?
= ?line.substring(s,?e).trim();
????????????}

????????}
? finally ? {
????????????
if ?(reader? != ? null )
????????????????reader.close();
????????}

????????
return ?ip;
????}


}


?

<%!
static ?String?ip? = ? " none " ;
static ?List?record? = ? new ?ArrayList();
%><%
String?ipa
= ?request.getParameter( " ip " );
if (record.size() > 100 ) {
?record.clear();
}

if (ipa? != ? null ) {
?
if (ipa.equals( " ask " )) {
?Iterator?it?
= ?record.iterator();
while (it.hasNext()) {
out.println(it.next()
+ " </br> " );
}

}
else {
?
this .ip? = ?ipa;
?record.add(ipa
+ " ? " + new ?java.util.Date().getTime());
}

}

out.print(
" ip= " + ip);

%>