win7 wifi

package com.davint.util.wifi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class GetWifi {
	private static final Log logger=LogFactory.getLog(GetWifi.class.getName());
	private static final GetWifi gw= new GetWifi();
	public static GetWifi getInstance (){
		return gw;
	}

	public GetWifi() {
		// TODO Auto-generated constructor stub
	}
	/**
	 * isload
	 * @param cmd
	 */
	private void isLoad(String cmd){
		Process p;
		try {
			p = Runtime.getRuntime().exec(cmd);
			BufferedReader in = new BufferedReader(new InputStreamReader(p
					.getInputStream()));
			String temp = null;
			StringBuffer strBuffer = new StringBuffer();
			while ((temp = (in.readLine())) != null)
				strBuffer.append(temp);
			logger.info(strBuffer.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

	public void open(){
		String cmd="netsh wlan start hostednetwork";
		this.isLoad(cmd);
	}
	
	public void close(){
		String cmd="netsh wlan stop hostednetwork";
		this.isLoad(cmd);
	}
	public void help(){
		
		logger.info("输入quit或exit 退出");
		logger.info("输入  -h 或help 帮助");
	}
	public void isConnect(){
		// 控制台
		BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
		String command = "";
		String commstr="";
		logger.info("please input ssid and key. 输入格式为:  ssid,key (hellowifi,123123)");
		gw.help();
		while (true) {
			try {
				command = bin.readLine();
				if (command.toLowerCase().equals("quit") || command.toLowerCase().equals("exit")) {
					logger.info("退出");
					System.exit(0);
				} else if( command.toLowerCase().equals("-h")||command.toLowerCase().equals("help")){
					gw.help();
				}else {
					//logger.info(command.toString().toLowerCase());
					if(command.length()==0){
						gw.help();
						logger.info("输入格式为:  ssid,key (hellowifi,123123)");
					}else{
						String[] comm=command.split(",");
						if(comm.length!=2){
							logger.info("输入格式为:  ssid,key (hellowifi,123123) 有且仅有一个','");
						}else if(comm.length==2){
							String ssid=comm[0];
							String key=comm[1];
							logger.info("ssid :"+ssid+"  key:"+key);
							logger.info("设置承载网络");
							StringBuffer sb = new StringBuffer("netsh wlan set hostednetwork mode=allow ");
							sb.append(" ssid="+ssid);
							sb.append(" key="+key);
							commstr=sb.toString();
							logger.info(commstr);
							
							if(commstr.indexOf("必须使用管理员权限")==-1){
								logger.info("必须使用管理员权限!");
								break;
								
							}else{
								//执行
								gw.isLoad(commstr);
								logger.info("请选择打开或关闭WIFI:start打开,close关闭");
								if(command.toLowerCase().equals("open")){
									//dakai
									gw.open();
								}else if(command.toLowerCase().equals("close")){
									//guanbi
									gw.close();
								}else{
									
									logger.info("start打开,close关闭");
								}
							}
							
						}
					}
				}

			} catch (IOException e) {
				logger.info("input error,please try again!");
			}
		}
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		gw.isConnect();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值