读取本机动态ip到远程网页

如何通过自己的ADSL使家里的电脑成为服务器呢?像花生壳这样的应用可以帮助你动态解析ip,不过这个程序太庞大了,根本没有必要。
下面介绍我的做法
条件
  上网方式:ADSL
  台式机:linux
  中继网页:在某虚拟主机申请一个免费空间,需要支持动态脚本(php、jsp等)
方案
  在本机运行一个Java程序,定时读取本机的外网IP,自动向中继网页用GET方式提交该数据。中继网页保存IP记录在内存中,其他访问者可以通过该页面查看当前的IP。

None.gif package  tedeyang;
None.gif
None.gif
import  java.io.BufferedReader;
None.gif
import  java.io.IOException;
None.gif
import  java.io.InputStreamReader;
None.gif
import  java.net.InetAddress;
None.gif
import  java.net.URL;
None.gif
import  java.net.URLConnection;
None.gif
import  java.net.UnknownHostException;
None.gif
import  java.util.Date;
None.gif
import  java.util.Timer;
None.gif
import  java.util.TimerTask;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  UpdateIp  dot.gif {
InBlock.gif    
static  String page  =   " http://xxx.xxx.com/name/link.jsp?ip= " ; //  need 'ip='
InBlock.gif
     static  String strategy  =   " ifconfig " ; //  or jdk
InBlock.gif
     static   long  refreshTime  =   5L   *   60   *   1000 //  5 minutes
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** */ /**
InBlock.gif     * 
@param  args
InBlock.gif     * 
@throws  IOException
ExpandedSubBlockEnd.gif     
*/

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

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

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

ExpandedSubBlockEnd.gif            }
;
ExpandedSubBlockEnd.gif        }
0 , refreshTime);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private   static   void  run(String page, String ipStrategy)  throws  IOException  dot.gif {
InBlock.gif
InBlock.gif        String ip 
=   " no " ;
InBlock.gif        
if  (ipStrategy.equals( " jvm " ))
InBlock.gif            ip 
=  getIpByJavaApi();
InBlock.gif        
else
InBlock.gif            ip 
=  getIpByLinuxIfconfig();
InBlock.gif        System.out.print(
new  Date()  +   " , this ip is : "   +  ip);
InBlock.gif        touchIp(page, ip);
InBlock.gif        System.out.println(
"  , touch successed. " );
ExpandedSubBlockEnd.gif    }

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

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private   static  String getIpByJavaApi()  throws  UnknownHostException  dot.gif {
InBlock.gif        String ip 
=   " none " ;
InBlock.gif        InetAddress[] all 
=  InetAddress.getAllByName(InetAddress.getLocalHost()
InBlock.gif                .getHostName());
InBlock.gif        
//  通过本机主机名,遍历多个ip
ExpandedSubBlockStart.gifContractedSubBlock.gif
         for  ( int  i  =   0 ; i  <  all.length; i ++ dot.gif {
InBlock.gif            String ip2 
=  all[i].getHostAddress();
InBlock.gif            
if  ( ! ip2.startsWith( " 127. " &&   ! ip2.startsWith( " 192. " )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
&&   ! ip2.startsWith( " 10. " &&   ! ip2.startsWith( " 172. " ))  dot.gif {
InBlock.gif                ip 
=  ip2;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
return  ip;
ExpandedSubBlockEnd.gif    }

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

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
  finally   dot.gif {
InBlock.gif            
if  (reader  !=   null )
InBlock.gif                reader.close();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return  ip;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif


 

None.gif <%!
None.gif
static  String ip  =   " none " ;
None.gif
static  List record  =   new  ArrayList();
None.gif
%><%
None.gifString ipa
=  request.getParameter( " ip " );
ExpandedBlockStart.gifContractedBlock.gif
if (record.size() > 100 ) dot.gif {
InBlock.gif record.clear();
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
if (ipa  !=   null ) dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
if (ipa.equals( " ask " )) dot.gif {
InBlock.gif Iterator it 
=  record.iterator();
ExpandedSubBlockStart.gifContractedSubBlock.gif
while (it.hasNext()) dot.gif {
InBlock.gifout.println(it.next()
+ " </br> " );
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif}
else dot.gif {
InBlock.gif 
this .ip  =  ipa;
InBlock.gif record.add(ipa
+ "   " + new  java.util.Date().getTime());
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gifout.print(
" ip= " + ip);
None.gif
None.gif
%>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值