I posted some old code I wrote to mimic rexec. We needed it to massage some data before sending it to rexecd on another machine. You may find it useful. Look for it here. It may give you some ideas.
Comment on Re: Moving around with password protected machines.