So, I've decided to write a script, I've been taking pieces from here and there and here's what I've got.
At first I've tried to do everything with WMI, but it didn't work with VPN connections that are not for all users and come to think about it, WMI uses the system account, so it doesn't have access to my connections, so I'm executing route and ipconfig and parsing their output.
The biggest challenge was finding out which connection I should share, I wanted it to select the right interface automatically, so if I have an active VPN it will use that connection. The only assumption I had while coding these methods is that the last interface to go up was the one with the internet and its either a coincidence or by design, it was showing first in the route print 8.8.8.8 command. so far its been working, but if it is a coincidence and not by design, I might need to replace it with a more consistent function, I couldn't find anything about how to find which connection was the active/internet/VPN one, but if you think about it, you just need to find the default route, and see which IP is on which interface and you're done.
Then, from getting the interface name, I could get the connection name via ipconfig /all or use the API from "HNetCfg.HNetShare.1" -> EnumEveryConnection -> NetConnectionProps -> Name/DeviceName, you can see an example from Microsoft in ShowConnections.vbs
And then all I needed to do was update the IP range (if it was changed), start the hostednetwork, set static ips on the virtual interface and start ICS.
netsh interface ip set address name="Connection 2" source=static addr=192.168.0.1 mask=255.255.255.0 netsh interface ip set dns name="Connection 2" source=static addr=none register=PRIMARY netsh interface ip set wins name="Connection 2" source=static addr=none
netsh wlan set hostednetwork mode=allow ssid=MyAccessPoint key=mypasskey netsh wlan start hostednetwork
Starting ICS is a 2 step process, first you start the private network, then the public.
ics.vbs "Public Connection" "Private Connection" true
While working out some problems in the script, it has come to my attention that most of the things I was able to do with ipconfig is possible to do through the "HNetCfg.HNetShare.1" object, I guess if I'll have spare time, I'll change it, but its good enough for now.
For stopping the virtual router, I needed to change back the virtual adapter's IP to DHCP, stop the hostednetwork and stop ICS.
netsh wlan set hostednetwork mode=disallow
netsh wlan stop hostednetwork
netsh interface ip set address name="Connection 2" source=dhcp netsh interface ip set dns name="Connection 2" source=dhcp register=PRIMARY netsh interface ip set wins name="Connection 2" source=dhcp
ics.vbs "Public Connection" "Private Connection" false
I've included a status, basically its executing
netsh wlan show hostednetwork
and the results are similar to
>status Hosted network settings ----------------------- Mode : Allowed SSID name : "MyAccessPoint" Max number of clients : 100 Authentication : WPA2-Personal Cipher : CCMP Hosted network status --------------------- Status : Started BSSID : xx:xx:xx:xx:xx:xx Radio type : 802.11x Channel : x Number of clients : 1 xx:xx:xx:xx:xx:xx Authenticated
You can find the scripts here:
https://github.com/drorgl/ForBlog/tree/master/ICS
The scripts have been tested on Windows 7 only!
I would like to thank the following for posting their work and making my life easier:
Jerry Lees - for his Registry read/write
Richard Mueller - for leading me in the right direction
Big_Daddy - Whoever you are, thank you for this.
Also, interesting links that I came across during the search:
WIFI API - http://managedwifi.codeplex.com/
Microsoft - About the Wireless Hosted Network - http://msdn.microsoft.com/en-us/library/dd815243(v=vs.85).aspx
How to Change the IP Range for the Internet Connection Sharing DHCP service - http://support.microsoft.com/kb/230148
My pleasure, glad it helped you in the past. (Even though I'm finding it 11 years afterward)
ReplyDelete