java用正則表達(dá)式統(tǒng)計(jì)ip個(gè)數(shù):
成都創(chuàng)新互聯(lián)公司從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元長子做網(wǎng)站,已為上家服務(wù),為長子各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
display ip count rule [ | { begin | exclude | include } regular-expression ]
參數(shù)說明:
|:使用正則表達(dá)式對顯示信息進(jìn)行過濾。有關(guān)正則表達(dá)式的詳細(xì)介紹,請參見“入門配置指導(dǎo)”中的“CLI”。
begin:從包含指定正則表達(dá)式的行開始顯示。
exclude:只顯示不包含指定正則表達(dá)式的行。
include:只顯示包含指定正則表達(dá)式的行。
regular-expression:表示正則表達(dá)式,為1~256個(gè)字符的字符串,區(qū)分大小寫。
【舉例】
# 顯示用戶配置的IP Accounting統(tǒng)計(jì)規(guī)則。
Sysname display ip count rule
IP Count rule list:
IP address address mask
1.1.1.0 255.255.255.0
2.0.0.0 255.0.0.0
------------------------------------
Total: 2 rules
步驟一、建一個(gè)表,表名任意,這里取名為:visitorcounter,表的結(jié)構(gòu)如下所示:
+-------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+------------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| vdate | date | NO | | 2000-01-01 | |
| vnum | int(11) | NO | | 0 | |
+-------+------------------+------+-----+------------+----------------+
步驟二、建立一個(gè)java類,名字也為:visitorcounter,類的內(nèi)容如下:
package com.hdzx.pub;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
public class VisitorCounter {
private final static String TABLE_NAME = "visitorcounter";
private static String today = null;
private static long today_num = 0;
private static long total_num = 0;
//加載訪問量
public static void loadNum(){
if(total_num1)
loadTotalNum();
if(today_num1)
loadToadyNum();
}
//加載今日訪問量
private static void loadToadyNum() {
// TODO Auto-generated method stub
DBConnect db = null;
ResultSet rs = null;
if(today==null)
today = getTodayDate();
String sql = "select vnum from "+TABLE_NAME+" where vdate='"+today+"'";
try {
db = new DBConnect();
rs = db.executeQuery(sql);
if(rs.next()){
today_num = rs.getLong("vnum");
}
else
{
sql = "insert into "+TABLE_NAME+"(vdate,vnum) values('"+today+"',0)";
db.executeUpdate(sql);
today_num = 0;
}
} catch (Exception e) {
// TODO: handle exception
today_num = 0;
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:獲得訪問人數(shù)");
}
}
//加載總訪問量
private static void loadTotalNum() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
DBConnect db = null;
ResultSet rs = null;
if(today==null)
today = getTodayDate();
String sql = "select vnum from "+TABLE_NAME+" where id=1";
try {
db = new DBConnect();
rs = db.executeQuery(sql);
if(rs.next()){
total_num = rs.getLong("vnum");
}
else
{
total_num = 0;
}
} catch (Exception e) {
// TODO: handle exception
total_num = 0;
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:獲得訪問人數(shù)");
}
}
//增加總的訪問量
private static int incTotalCounter(){
int k = 0;
DBConnect db = null;
loadNum();
total_num = total_num+1;
String sql = "update "+TABLE_NAME+" set vnum="+total_num+" where id=1";
try {
db = new DBConnect();
k = db.executeUpdate(sql);
} catch (Exception e) {
// TODO: handle exception
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:增加訪問人數(shù)");
}
return k;
}
//增加今日的訪問量
public static int incTodayCounter(){
int k = 0;
DBConnect db = null;
String sql = null;
loadNum();
today_num += 1;
sql = "update "+TABLE_NAME+" set vnum="+today_num+" where vdate='"+today+"'";
try {
db = new DBConnect();
k = db.executeUpdate(sql);
if(k 0)
incTotalCounter();
} catch (Exception e) {
// TODO: handle exception
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:增加訪問人數(shù)");
}
return k;
}
//獲得今天的日期
private static String getTodayDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}
///獲得今日訪問量
public static long getTodayNum(){
loadNum();
return today_num;
}
//獲得總的訪問量
public static long getTotalNum(){
loadNum();
return total_num;
}
}
步驟三、經(jīng)過以上的步驟后,在頁面中加入以下的代碼,就可以實(shí)現(xiàn)網(wǎng)站訪問量的統(tǒng)計(jì)工作:
if(session.isNew())
{
VisitorCounter.incTodayCounter();
}
%
今日訪問量:%=VisitorCounter.getTodayNum() %br/
總的訪問量: %=VisitorCounter.getTotalNum() %
1.得到局域網(wǎng)網(wǎng)段,可由自己機(jī)器的IP來確定 (也可以手動(dòng)獲取主機(jī)IP-CMD-ipconfig /all)
2.根據(jù)IP類型,一次遍歷局域網(wǎng)內(nèi)IP地址
JAVA類,編譯之后直接運(yùn)行便可以得到局域網(wǎng)內(nèi)所有IP,具體怎樣使用你自己編寫相應(yīng)代碼調(diào)用便可
代碼如下::
package bean;
import java.io.*;
import java.util.*;
public class Ip{
static public HashMap ping; //ping 后的結(jié)果集
public HashMap getPing(){ //用來得到ping后的結(jié)果集
return ping;
}
//當(dāng)前線程的數(shù)量, 防止過多線程摧毀電腦
static int threadCount = 0;
public Ip() {
ping = new HashMap();
}
public void Ping(String ip) throws Exception{
//最多30個(gè)線程
while(threadCount30)
Thread.sleep(50);
threadCount +=1;
PingIp p = new PingIp(ip);
p.start();
}
public void PingAll() throws Exception{
//首先得到本機(jī)的IP,得到網(wǎng)段
InetAddress host = InetAddress.getLocalHost();
String hostAddress = host.getHostAddress();
int k=0;
k=hostAddress.lastIndexOf(".");
String ss = hostAddress.substring(0,k+1);
for(int i=1;i =255;i++){ //對所有局域網(wǎng)Ip
String iip=ss+i;
Ping(iip);
}
//等著所有Ping結(jié)束
while(threadCount0)
Thread.sleep(50);
}
public static void main(String[] args) throws Exception{
Ip ip= new Ip();
ip.PingAll();
java.util.Set entries = ping.entrySet();
Iterator iter=entries.iterator();
String k;
while(iter.hasNext()){
Map.Entry entry=(Map.Entry)iter.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
if(value.equals("true"))
System.out.println(key+"--"+value);
}
}
class PingIp extends Thread{
public String ip; // IP
public PingIp(String ip){
this.ip=ip;
}
public void run(){
try{
Process p= Runtime.getRuntime().exec ("ping "+ip+ " -w 300 -n 1");
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
//讀取結(jié)果行
for (int i=1 ; i 7; i++)
input.readLine();
String line= input.readLine();
if (line.length() 17 || line.substring(8,17).equals("timed out"))
ping.put(ip,"false");
else
ping.put(ip,"true");
//線程結(jié)束
threadCount -= 1;
}catch (IOException e){}
}
}
}
在web.xml中加入獲取訪客的filter,然后獲取IP地址,查詢數(shù)據(jù)庫此IP是否有記錄,如果無記錄,則進(jìn)行訪問計(jì)數(shù)+1,并更新數(shù)據(jù)庫相應(yīng)數(shù)據(jù),這樣就可以達(dá)到計(jì)算次數(shù)的操作了