Friday, May 16, 2008

Windows ip config with batch scripts

It is possible to configure TCP/IP with simple batch scripts in Windows XP, even though the information is well hidden. There are a few typical Windows annoyances attached, but for a notebook user who needs to change settings often, double-clicking a batch file is definitely much better than the network configuration GUI.

The almost secret - even though it is included in the default XP install - tool is called netsh.

Below are a few examples. The main idiosyncrasy is that you need the name of the interface to configure, and that name is somewhat unpredictable. In particular, it is localized. For the normal Ethernet interface, in an English system, it starts with "Local Area Connection". But it is different in other language versions of Windows. There is no simple standard name like "eth0" or "en0".

Look up your interface names in "Network Connections", or by typing ipconfig or
netsh interface show interface
at the command prompt.

A simple batch file which will set your LAN interface to DHCP:
SET LAN=Local Area Connection

netsh interface ip set address name="%LAN%" source=dhcp
netsh interface ip set dns name="%LAN%" source=dhcp register=NONE
netsh interface ip set wins name="%LAN%" source=dhcp

This one will set it to a fixed IP address:
SET LAN=Local Area Connection

SET IP=192.168.1.56
SET MASK=255.255.255.0

SET GW=192.168.1.1
SET DNS=%GW%

netsh interface ip set address name="%LAN%" source=static addr=%IP% mask=%MASK%
netsh interface ip set address name="%LAN%" gateway=%GW% gwmetric=0
netsh interface ip set dns name="%LAN%" source=static addr=%DNS% register=NONE

Finally, a more complete example, which sets both the LAN and WIFI interfaces can take an optional second DNS server, and set the WINS server:
SET LAN=Local Area Connection 5
SET WIFI=Wireless

SET IPLAN=192.168.1.56
SET MASKLAN=255.255.255.0

SET IPWIFI=192.168.1.57
SET MASKWIFI=255.255.255.0

SET GW=192.168.1.1
SET DNS=192.168.1.1
SET DNS2=
SET WINS=none

netsh interface ip set address name="%LAN%" source=static addr=%IPLAN% mask=%MASKLAN%
netsh interface ip set address name="%LAN%" gateway=%GW% gwmetric=0
netsh interface ip set dns name="%LAN%" source=static addr=%DNS% register=NONE
IF NOT "%DNS2%" == "" netsh interface ip add dns name="%LAN%" addr=%DNS2% index=2
netsh interface ip set wins name="%LAN%" source=static addr=%WINS%

netsh interface ip set address name="%WIFI%" source=static addr=%IPWIFI% mask=%MASKWIFI%
netsh interface ip set address name="%WIFI%" gateway=%GW% gwmetric=0
netsh interface ip set dns name="%WIFI%" source=static addr=%DNS% register=NONE
IF NOT "%DNS2%" == "" netsh interface ip add dns name="%WIFI%" addr=%DNS2% index=2
netsh interface ip set wins name="%WIFI%" source=static addr=none
netsh interface ip set wins name="%WIFI%" source=static addr=%WINS%


You can also use netsh to configure the Windows firewall, check network connectivity, etc.

As a ping replacement, try this:
netsh diag ping adapter 1
(replace "1" with your network adapter's index number, which you can find with netsh diag show adapter). It will automatically ping your gateway, your DNS server(s) and your own IP address.

For more information, try these links:
How can I configure TCP/IP settings from the Command Prompt?
or
10 things you should know about the NETSH tool
or just do a web search on "netsh".

Labels: , , , , ,

Tuesday, May 06, 2008

CSS and javascript progress meter

For a little video project, my daughter wanted a computer screen displaying a fake progress bar. I haven't played with GUI programming languages for ages (the last time must have been with Delphi Pascal some 15 years ago). So I thought this should certainly be possible to do in a web browser with some CSS and javascript, and would also be an opportunity to learn some javascript and DOM interaction basics.

I first found a few examples on the web, but they all used animated GIFs. I didn't want to bother with creating animated gifs, and besides, I wanted to be able to style the progress window using only CSS, so that sizes and colors could be changed quickly enough for my impatient daughter.

So this is a solution using only CSS and javascript, and no image at all. Have a look at the source code of this CSS and javascript progress bar demo.

Both the CSS and the javascript are embedded in the HTML file.

(Tested in Firefox 2.0.0.14, Opera 9.01, Safari 3.1.1, MSIE 5.01 and 6.0. All in WinXP SP2).

Labels: , , , , ,

Thursday, May 01, 2008

Fix slow ssh response

When logging into an ssh server you may experience a long delay after authentication, and before you get the prompt. This will happen with most sshd servers on a home network or on a new network while it is being set up.

The reason is that sshd tries to do a reverse lookup on the connecting IP, which takes a while to time out.

To speed up the initial response of ssh, the solution is to prevent these reverse lookups, at least until the network has a working DNS which can resolve the connecting IPs to names. To do this, set "UseDNS no" in your sshd_config file and force sshd to re-read it's configuration.
sudo -s # if you are not root, like on Ubuntu or Mac

file=/etc/sshd_config
# or
file=/etc/ssh/sshd_config
# or on a Mac
file=/private/etc/sshd_config

perl -i.bak -pe 's/^\s*#?UseDNS\s+.*/UseDNS no/i' $file
grep -qi 'UseDNS no' $file || echo UseDNS no >> $file
# on Linux:
kill -HUP `cat /var/run/sshd.pid`
(the last kill line to force sshd re-read it's config file doesn't work on Mac)
While searching for this solution, I came across other configuration settings. They didn't apply to my case, but if you still have problems, you may want to set "GSSAPIKeyExchange no" in your client configuration file which is usually in /etc/ssh_config (ssh_ , not sshd_ !). Or look into IPv6 problems.

Now I have to find an equivalent solution for rsync.

Labels: , , , , ,