2010-12-17

Running X window through SSH tunnel

* Prerequisite
The secure shell daemon (sshd) in the Unix box should be set up to support X11 forwarding, to enable this
1. Login as root
2. Edit /etc/ssh/sshd_config adding next line
X11Forwarding yes
(In HP-UX, sshd_config is in /opt/ssh/etc/)
3. Restart sshd
in AIX: /etc/rc.d/rc2.d/Ssshd {stop|start} OR stopsrc -s sshd; startsrc -s sshd
in Linux: /etc/init.d/sshd {stop|start}
in Solaris: svcadm refresh ssh
in HP-UX: /sbin/init.d/secsh {stop|start}


* Case 1: my PC with cygwin directly connects to remote Unix box (my PC <----> Unix)

1. Start cygwin XWin Server using Windows menu or using startxwin command in cygwin command window.

2. Set up secure shell tunnel by running next command in a cygwin command window.
cygwin$ ssh -X -Y -C user@remote_unix_host

3. In the shell session opened in above step, make sure DISPLAY environment variable is set and run X comands like xterm, xclock, ...

Sample session:

cygwin_in_WinXP$ ssh -X -Y -C johndoe@debian
johndoe@debian's password: XXX
johndoe@debian ~$ echo $DISPLAY
localhost:11.0

johndoe@debian ~$ xterm &
[1] 1234
johndoe@debian ~$

xterm should pop up in my Windows XP box.


* Case 2: my PC with cygwin cannot connect to the remote Unix box as it's behind firewall having private IP address 192.nnn.nnn.nnn. (my PC <----> SSH relay host <----> Unix)

Let's call ssh relay host ssh_box and target Unix host xwin_box.

0. Add next lines in your Windows hosts file (C:\Windows\System32\drivers\etc\hosts).
127.0.0.1 locahost xwin_box
10.10.1.10 ssh_box # public IP address, so no problem for direct connection

Also make sure xwin_box is registered in the /etc/hosts file of ssh_box too.
(In fact, aliasing xwin_box is optional as localhost can be used in step 3 below)

1. Start cygwin XWin Server using Windows menu or using startxwin command in cygwin command window.

2. Set up secure shell tunnel by running next command in a cygwin command window.
cygwin$ ssh -C -L 2222:xwin_box:22 user@ssh_box

3. Run next command in another cygwin command window.
cygwin$ ssh -X -Y -C -p 2222 xuser@xwin_box
or
cygwin$ ssh -X -Y -p 2222 xuser@localhost

4. In the shell session opened in above step 3, make sure DISPLAY environment variable is set and run X comands like xterm, xclock, ...

2010-04-05

read timeout in ksh

Timing out read in Korn shell

It is possible to time out the read command using coroutines.
We need a helper script, let's call it timout_read.sh, and main script with read command.

1. timeout_read.sh
#! /usr/bin/ksh

typeset -i timeout_after_this=$1
sleep $timeout_after_this

kill -0 $PPID 2>/dev/null && kill -USR1 $PPID
#ps -p $PPID >/dev/null && kill -USR1 $PPID 2>/dev/null

2. main.sh

#! /usr/bin/ksh

myvar=.yes ## default value.
timeout=${1:-4}

read_myvar()
{
trap return USR1
timeout_read.sh $1 &
read myvar?"Your value for myvar: "
trap "" USR1
}

## do something

read_myvar $timeout
if [[ $myvar = .yes ]]; then
# do default action
echo Read timed out. myvar = $myvar
else
# do proper action depending on user input
echo You entered $myvar
fi
* read in bash has -t option for this functionality.

2010-03-21

* properties of binary relation
let R be a binary relation in A.
R is
reflexive in A if aRa for all a ∈ A.
symmetric in A if aRb implies bRa for all a, b ∈ A.
antisymmetric in A if aRb and bRa imply a = b for all a, b ∈ A.
asymmetric in A if aRb implies that bRa does not hold for any a, b ∈ A.
transitive in A if aRb and bRc imply aRc for all a, b, c ∈ A.

* equivalence
R is an equivalence on A if it is reflexive, symmetric, and transitive.

* ordered set
let R be a relation in A.

- poset (partially ordered set)
R is a (partial) ordering of A if it is reflexive, antisymmetric, and transitive.
(A, R) is called an ordered set.

ex: "less than or equal to" relation
divisibility relation | : a|b iff a divides b.

- strict ordering
R is a strict ordering if it is asymmetric and transitive.
ex: "less than" relation

- loset (linearly ordered set)
an ordering of A is called linear or total if any two elements of A are comparable.
(A, R) is called a linearly ordered set.

( a and b are comparable in an (partial) ordering R if aRb or bRa; in a strict ordering if aRb or bRa or a = b)

ex: < is total, but | is not.

- woset (well ordered set)
linear ordering R of A is a well-ordering if every non-empty subset of A has a R-least element.
(A, R) is called a well-ordered set.

* notions of greatest and least
let ≤ be an (partial) ordering of A, and let B is a subset of A.

- least/greatest/minimal/maximal
b ∈ B is the least element of B in the ordering ≤ if b≤x for every x ∈ B.
b ∈ B is the greatest element of B in ≤ if x≤b for every x ∈ B

b ∈ B is a minimal element of B in ≤ if there exists no x ∈ B such that x≤b and x != b.
b ∈ B is a maximal element of B in ≤ if there is no x ∈ B such that b≤x and x != b.

ex: B = positive integers greater than 1 = {2, 3, 4, ...} does not have a least element in |, but it has many minimal elements (for example, 2, 3, 5, etc)

- lower bound/upper bound/infimum/supremum
a ∈ A is a lower bound of B if a≤x for all x ∈ B.
a ∈ A is an upper bound of B if x≤a for all x ∈ B.

a ∈ A is an infimum (or greatest lower bound) of B if it is the greatest element of the set of all lower bounds of B.
a ∈ A is a supremum (or least upper bound) of B if it is the least element of the set of all upper bounds of B.