Contact

Subscribe via Email

Subscribe via RSS

Categories

Creative Commons Attribution 4.0 International License
© Rakhesh Sasidharan

[Aside] Improving your PuTTY connections

Wish I had come across this during my PuTTY days!

The TL;DR summary is that by default PuTTY uses an ASCII encoding while most Linux and other OSes uses UTF-8 encoding. It’s because of this mismatch that manpages and such sometimes appear with â characters. Change the PuTTY encoding and find happiness! 

The System.Net.Webclient class

The system.net.webclient class can be used to deal with web pages.

To download and display pages this class has couple of methods:

  • DownloadData downloads the page and displays it as an array of bytes.
  • DownloadString downloads the page and displays it as one long string.
  • DownloadFile downloads the page and saves it to a file name you specify.

The class also has properties you can set to be used while downloading a page. For instance:

  • QueryString to specify pairs of query parameters and their values. For example: to do a Google search for the word “rakhesh” one can fetch the page http://www.google.com/search?q=rakhesh. This q=rakhesh is a query string, with q being a parameter and rakhesh being a value to the parameter. To do the same via the system.net.webclient class one would do the following:

  • Headers to specify pairs of headers that can be set when requesting the web page:

  • Credentials to specify credentials for accessing the web page:

  • ResponseHeaders to view the headers received in response.

There are other properties and methods too, the above are what I had a chance to look at today.

Windows ignores the hosts file

If you find that Windows ignores your hosts file try the following: open the file, select save as, and while saving it change encoding from Unicode (or whatever it is) to ANSI. Be sure to select the file type as “All files” so Notepad doesn’t append a “.txt” to the file name (and double check after saving as sometimes Notepad still appends a “.txt”).

How is this PowerShell related?

I was using Out-File to put something into the hosts file. Windows expects the hosts file to be in ANSI encoding – and seems to ignore it otherwise – but Out-File uses Unicode encoding by default; so the net result is that Windows ignores the resulting hosts file. Even if I removed all the entries in it and made fresh ones for testing, since the file is still in Unicode encoding Windows ignores it. Finally, thanks to a great post I figured a workaround which led to realizing the problem.

Moral of the story: when writing to the hosts file using Out-File specify the encoding as ASCII. Like thus:

And if you are a PowerShell geek, you can avoid the longish method above of changing the hosts file type to ANSI through something like this:

Easy peasy!