Cygwin as UNIX emulator
(A) Use Cygwin as UNIX environment on Windows for shell scripting.
(B) Run sshd as Windows service.
(C) Run crontab as Windows service.
(D) Use bind tools in Cygwin (host, dig)
The ability to write and test shell scripts on your home PC before running them on a remote host is very useful. Normally one would edit remotely via ssh with much lag. Local writing and testing on your Windows machine lets you work out the various parts of your scripts much more easily and quickly, without having to install or boot up into a UNIX operating system.
Access to UNIX tools like sed, grep, awk, crontab etc. is also very handy to have in Windows. You can even run sshd in cygwin and remotely connect to your Windows computer securely.
Method Part A
Note: I recommend you use bash as your interactive shell, even if you code shell scripts in ksh. Why? Bash on cygwin has tab-completion, arrow key history, colour term etc. already setup, and it's a bit of pain to do it in ksh.
Download the latest version of cygwin setup from http://www.cygwin.com
Choose "Install from Internet", and directories which suit you.
When at the "Select Packages" page add "rxvt" and whatever utilities you need, for example: vim, pdksh, openssh, cron, whois...
After installation open the installation directory and copy cgywin.bat something like login.bat
If you modify cygwin.bat directly it will be overwritten whenever you install new packages.
Modify login.bat; replace the line with "bash" to:
rxvt -fn FixedSys -sl 1000 -geometry 90x30 -sr -e bash -l
Here's a run-down of the above line:
- rxvt - this is a 'terminal client' or an alternative to that dos box popup. You need it to display various programs (vim, mutt etc.) correctly.
- -fn FixedSys - this is fontname, makes it all look a little nicer (change this to what you want)
- -sl 1000 - the "buffer", or how many lines the terminal will remember (so you can scroll up)
- -geometry 90x30 - sets the width of the window (90) and the height(30). I use this on 800x600 and it's perfect
- -sr - sets the scrollbar on the right - Windows behaviour
- -e bash -l - executes bash, your shell, as a login shell
Save login.bat and optionally change any links made by the setup program to point to login.bat
You're done! Check out your .bashrc and .bashprofile files and uncomment any useful aliases.
I added an alias to my .bashrc:
So now you can launch login.bat and test our your shell scripts, before deploying them on the remote host.
Method Part B
Once you have installed the openssh packages you need to run
Home@paws ~ $ ssh-host-config
Specifying "CYGWIN=ntsec term" when asked.
Enter "yes" when the script will ask you whether to add sshd as a Windows service.
From then on you can remotely connect to your Windows computer, assuming you have opened port 22 (or whichever port you wish to run sshd on, configurable in /etc/sshd_config) in your firewall.
To start the service type:
Home@paws /etc $ net start sshd
Method Part C
Once you have installed the cron package you need to run
Home@paws /etc $ cygrunsrv -I cron -d "CYGWIN cron" -p /usr/sbin/cron -a -D
This will install a Windows Service with cron. Make sure /var/run/cron.pid is writeable by SYSTEM user (deleting it if it exists is one way)
To start the service type:
Home@paws /etc $ net start cron
If you have any problems starting the services check the Windows Event Log, under Applications. You should be able to solve any issues from there.
Use the command "crontab -e" to edit your crontab, and don't forget "man 5 crontab"!
Method Part D
The ISC Bind tools are extremely useful for DNS related queries. Among them are "dig", "nslookup", "host".
As of writing a "native" cygwin version of Bind has not been packaged and made available through the setup program.
I had success with installing the Windows binary for ISC Bind and including the Bind binary path to the system PATH variable.
The Windows binary of Bind 9.3.1 can be found here.
Once installed, modify the system PATH (right-click on My Computer -> Properties -> Advanced -> Environment Variables) to include the "bin" directory of bind, and start the "ISC bind" service which should have been installed.
You should now be able to use "dig" etc.!
Home@paws ~ $ which host /cygdrive/c/windows/system32/dns/bin/host Home@paws ~ $ host poff.sixbit.org poff.sixbit.org has address 126.96.36.199 Home@paws ~ $