Saturday, May 23, 2009

Unix commands on windows: Uwin

Uwin is a superb alternative to cygwin, created by AT&T Research group and currently available as free download (AT&T says its "for students and universities", but in fact anyone can download it). Uwin is truly unix for windows because it is an official fork of the POSIX standard tools built for windows.

The official site for uwin (at the time of this writing) is very messy, and filled with links that keep throwing you back and forth, I find it hard to navigate, which is why I have to blog about it here.

Advantages:

@ Light-wight
Its much cleaner than cygwin and extrememtly light-weight. Seriously, the base package 16 MB in size, includes all common unix tools like sed, awk ....

@ Better integration with the native windows OS
You can run all the common unix commands right inside the windows command shell. You can even execute shell scripts from inside the windows command shell.

@ Power of Unix/POSIX
The base package includes ls, ps, pwd, cksum, diff, vi, grep, fgrep, egrep, awk, nawk, find, sed, cut, sort, ssh, scp, touch, tail, getopts, getconf, basename, dirname, cp, mv, date, sdiff, tr, uname, who, and much more (and also the unix tool more ;) ).

@ Add softwares
You can also build additional softwares from sources.

Dis-advantages:

@ SSH1
Like cygwin, the default version of SSH that ships with uwin is version 1. But, thank God there is a OpenSSH for windows update available, which will give you the SSH Version 2, so this is not such a big deal then.

Links:

- Uwin

(requires username and password)
Username: I accept www.opensource.org/licenses/cpl
Password: .
(thats rght, just a dot)


Additional Step:
Add Uwin's bin directory to windows PATH variable:
C:\Program Files\UWIN\usr\bin

This will give you easy access to all the uwin commands (like ps, pwd, cksum, vi, grep, fgrep, egrep, awk, nawk, find, sed, sort, ssh, scp, touch, tail) from a standard cmd window.


Friday, February 27, 2009

Scripting Windows using AutoIt

On Vista, NVIDIA doesn't provide any convenient way to switch Display profiles. The default NVIDIA Control Panel allows you to save profiles, but there is no means to just launch these profiles by double clicking an icon, or from some context menu. This is quite inconvienient if you frequently switch monitors.

I came across a neat way to solve this problem. First, save monitor profiles for your liking. I have 4 profiles, from which I frequentl switch between 1 and 2.


I installed AutoIt.
[AutoIt is to Windows, as Expect is to Unix.]

The I created 4 scripts, one each to launch these Desktop Profiles.

..................................................................................
1 Monitor Only.au3

Run("nvcplui.exe")
WinWaitActive("NVIDIA Control Panel")
Sleep(1500)
Send("!p")
Sleep(100)
Send("l")
Sleep(100)
WinWaitActive("Load Desktop Profile")
Sleep(100)
ControlClick("Load Desktop Profile", "FolderView", 1, "left", 2, 100, 50)
Sleep(100)
Send("{ENTER}")
Sleep(100)
WinWaitActive("nvdisps")
Sleep(100)
Send("y")
Sleep(100)
WinWaitActive("Apply Changes")
Sleep(100)
Send("y")
Sleep(100)
WinWaitActive("NVIDIA Control Panel")
Sleep(100)
WinClose("NVIDIA Control Panel")
..................................................................................
2 TV Only.au3
(Just change the 10th line, and change the X, Y axis co-ordinates for mouse pointer to click)

ControlClick("Load Desktop Profile", "FolderView", 1, "left", 2, 100, 90)
..................................................................................

3 Monitor + TV.au3

ControlClick("Load Desktop Profile", "FolderView", 1, "left", 2, 100, 130)
..................................................................................

4 TV + Monitor.au3

ControlClick("Load Desktop Profile", "FolderView", 1, "left", 2, 100, 170)
..................................................................................

After this we can create a shortcut to these on the desktop or quick launch and assign keyboard shortcut to it for easy access like CTRL+ALT+SHIFT+1, CTRL+ALT+SHIFT+2, etc.

Another Problem solved ;).
Thanks to MJFox for his post and his script.

Thursday, October 02, 2008

SSH Automation using Expect

Here's a cool script that will login to an SSH Server without prompting for a password. It can execute any start-up commands if needed.

The password is hard coded into the script which is lame, but this is my first attempt, I'm working on better/encrypted ways, but hey sometimes simple is best.

#!/usr/bin/expect -f
spawn ssh amol@sandbox

expect "amol@sandbox's password: "

send -- "PASSWORD\r"
expect "(sandbox:amol)$ "
send -- "pbsu -h sandbox root\r"
expect "Enter amol's pb-password:"
send -- "PASSWORD\r"
expect "(sandbox:root)# "
interact
# send -- "ksh -o vi\r"

# expect eof

Where:
Username : amol
Server : sandbox
Password: PASSWORD

The Last uncommented line "interact" is the point where you get the prompt for hand-typing commands. The script can be made Fully automatic by removing this statement.

The simplest way to remove the hard coded password would be to either prompt for it at stdin or to pass it using command line arguments. I'm thinking if we can do this in a more secure way like may be storing an encrypted passoword in a text file and then decrypting it on the fly while running the script. Storing it in a file will remove the need for a prompt and hence more convienient.

The Most secure way to do passwordless SSH automation is to use Public Key Authentication, however, if the SSH Server does not allow passwordless authentication then you cannot use it, which applied in my case.

Here's a good Tutorial on Expect.