PGTS PGTS Pty. Ltd.   ACN: 007 008 568

point Site Navigation

point Other Blog Threads



  Valid HTML 4.01 Transitional

   Stop The Internet Filter!

   No Clean Feed

   The Internet Filter Is An Ex-parrot!






PGTS Blog Archive

Thread: Tips/Tricks For Programming etc

GP JPG
Failure is not an option. It comes bundled with Microsoft Windows

Cygwin - Never Leave Unix Without It


Chronogical Blog Entries:



Date: Thu, 07 Feb 2008 23:03:17 +1100

Quite often I find myself working on Windows workstations. Depending on where I am working and the company policies, it is not always possible to install Cygwin. At a bare minimum I would like to have Gvim and perl. This means that you have a reasonably good editor and pretty damn good scripting engine. These can be copied onto a single CD.

Better still, if you can get access to it, is Cygwin. This is easily the best option ... Well, actually Linux is the best option ... But Cygwin includes virtually the full suite of Unix commands .. and you can run them from a Windows command session.

Here are some useful tips and tricks I found while installing Cygwin.

Installing New Fonts In Windows.

This not really a Cygwin tip, but I had to look this up, since Microsoft believe they have to change the menus around to give the illusion that something has been "improved". So, some of you cygwinners may find it helpful ... To manually install or re-install a font in Windows, you must:

  1. Click Start, and then click Run.
  2. Type %windir%\fonts, and then click OK.
  3. On the File menu, click Install New Font.
  4. In the Drives box, click the drive that has the floppy or CD-ROM that contains the fonts you want to add. If you are installing fonts from a floppy disk, this is typically drive A or drive B. If you are installing the fonts from a compact disc, your CD-ROM drive is typically drive D. Double-click the folder that contains the fonts.
  5. Click the font you want to add. To select more than one font at a time, press and hold down the CTRL key while you click each font.
  6. Click to select the Copy Fonts To Fonts Folder check box. The Windows\Fonts folder is where the fonts that are included with Windows are stored.
  7. Click OK.

Fixing Cygwin man pages.

For some reason the last couple of versions of Cygwin I have used have had broken man pages (haven't gone into this in depth -- it might be some shortcut I took). Here is what I did to get them working.

Modify the file /etc/defaults/usr/share/misc/man.conf. In it you will find some comments that tell you what to do. Basically just add the -c option to these lines:

	TROFF         /usr/bin/groff -Tps -mandoc
	NROFF         /usr/bin/nroff -Tlatin1 -mandoc
	JNROFF        /usr/bin/groff -Tnippon -mandocj

BTW: Handy hint for perl cygwinners. Those of you who are spoiled by your man pages being automagically updated when you install perl packages in BSD or Linux, will be a bit frustrated when they don't appear in Cygwin. So if you are too lazy to work out how to use pod2 man, don't forget the perldoc command -- which in Cygwin works just like man (except that it doesn't seem to know about the console width)

Living with NTLM.

Sometimes you may have to try and work with NTLM, or grapple with the sad fact that your PC support group has recently switched to NTLM.

In one case, I was not able to use wget (this may have been due to the way the proxy was setup, rather than NTLM). My favourite browser w3m would not work, ncftpget might have worked if I could have figured out how to configure it. Even that reliable old classic, lynx (an oldie but a goldie) was broken.

All of which reminded me (once again) of why I love to hate Microsoft.

You can still get command line access to the Internet, however.

I used the Cygwin setup program to download the latest version of cURL (7.15.4), and I was able to get it to work, despite all sorts of dire warnings to the effect that I should not use NTLM (believe me if it was my choice, I wouldn't).

This meant using the following options:

	curl --proxy-ntlm --ntlm -U userid:password http://www.foo.com/bar.txt

Which could be added to the .curlrc file ... yes I know it is not secure (but then, neither is .netrc). If you want you can make it a little more secure by doing the following.

  1. create a .curlrc file on your own hard disk e.g. /home/user/.curlrc (substitute [user] for your own name)
  2. Because it is your own hard disk you can write protect this file. Use chmod and then link back to the network HOMEPATH
		chmod 600 /home/user/.curlrc
		ln -s /home/user/.curlrc .curlrc

Obviously the same will work for .netrc (You'll have to use force when writing changes to the config files)

All of this came about because at first I seemed to have trouble with the current working folder. When I used curl I found that it was not able to negotiate with the proxy unless the current working directory was the HOMEPATH (as supplied by Windows). I investigated $HOMEDRIVE and $HOMEPATH and read many confusing reports about these variables. I even tried experimenting with them. Then as mysteriously as they appeared, these problems disappeared (I think after I re-booted the workstation a couple of times - duohh!). But it seems from then on I was locked in to using a $HOME that was the same as the $HOMEDRIVE$HOMEPATH allocated by Windows Admin. Hence the kludge above (linking to .curlrc)

This still left the problem of cpan getting lost every time I tried to run it from the command line. I had to persuade cpan to only use cURL and not try the other (broken) methods. So I took out my Procrustean implements and set to work on cpan ... I started it up and ran these commands:

	o conf wget ""
	o conf ncftpget ""
	o conf lynx ""
	o conf curl /usr/bin/curl
	o conf commit

(The last line is necessary if you want to keep your changes)

CPAN still had some difficulty getting packages. But it seems to learn as it goes. Possibly some changes in the .curlrc file would make it smoother. Update: later versions of CPAN seemed to learn how to access files using the proxy -- so I'm not entirely sure what was happening here. A lot of what happens in CPAN seems like magic to me (that's good magic not dark magic).

But overall, cURL works fine. It is a powerful little Unix command line program and has great potential for automating many online tasks. If you own your own Unix environment, you can download a copy. It could do all sorts of things in a Unix environment as well as a Cygwin environment.

Persuading Vim to Use Pretty Colours.

If you are like me, you will much prefer black on white. This can be accomplished by changing the (Windows) colour properties on the Cygwin command window, and while you are about it you might want to make the screen wider, taller, give yourself more history and check Edit options for "QuickEdit" and "Insert" Modes. Then, when asked to choose between:

  1. Apply properties to current window only.
  2. Modify shortcut that started this window.
... choose the second option.

This leaves vim still using the rather ugly default colours. To change vim, Add this to your .vimrc:

	set background=light
	" I also like the following options
	set autoindent
	" Switch syntax highlighting on, when the terminal has colors
	" Also highlight the most recent search pattern.
	if &t_Co > 2 || has("gui_running")
	  syntax on
	  set hlsearch
	endif
	" if you don't want backups, use this line
	set nobackup

Changing the Title in a Cygwin Window.

This is rather trivial. You can change the title of the window with correct sequence of escape characters. Any one of the following three commands will do this:

Actually there are probably hundreds of ways that imaginative cygwinners could do this. (Suggestions are welcome).

Note: in Windows the "title" command is built into the CMD shell. So a title change can be accomplished with the command "title Foo Bar" (which will give the window the title of "Foo Bar". If you are a smart Windows programmer (there aren't many of these left today) you could use "title Foo Bar %something%" where %something is a variable in your .bat .cmd file etc. I don't know (or care much) how the CMD shell accomplishes this, but I wouldn't be surprised if there were some form of ANSI escape sequences involved somewhere.

Using OpenSSH on Cygwin.

OpenSSH works pretty much as it does anywhere. There are a few Windows specific things that I came across however. After you've installed the suite of programs you could use the ssh-agent command pretty much as you would in Unix. For example, if you enter the command ssh-agent, you will see something like this:

	SSH_AUTH_SOCK=/tmp/ssh-qzz3CLn7xP/agent.2936; export SSH_AUTH_SOCK;
	SSH_AGENT_PID=2928; export SSH_AGENT_PID;
	echo Agent pid 2928;

Cut and paste this to your screen (actually the last one is not really necessary -- it is more for processes that capture this screen output and run it). Now you have an agent running on a socket as you would in Unix. The only thing to be aware of is that exiting from the command line is like logging off in Unix. So your agent will be terminated. Of course the usual caveats apply. Unless you really know what you are doing it is probably not advisable to have multiple agents running.

One of the problems I seemed to encounter in the Windows world, was certificates not generated by the standard openSSH commands. These arrived with the file extension of .ppk. In Ubuntu you could convert this file with the following command:

	puttygen foreign.ppk -O private-openssh -o foreign.key

Of course if you had Ubuntu you would not be fiddling around with Cygwin. In windows, you may have to use the GUI version of puttygen. This can be downloaded as part of the Win32 putty package. You start the puttygen package. Then load the file (e.g. foreign.ppk) and export it as an OpenSSH format file.

If you look at the two files that result (the .ppk original and the converted file), you will see that the key is the same, but the format is slightly different.


Other Blog Posts In This Thread:

Copyright     2008, Gerry Patterson. All Rights Reserved.