Advertisement
2002VB Internet/ HTML #21398

Piss easy UNIX rexec

Once upon a time a wrote a telnet client... Why ? Because I needed to control some UNIX functions from a windows frontend... I couldn't help thinking how messy it was and that they're had to be an easier way... Well, there is!!! Most UNIX machines have a daemon called rexecd which allows you to do a quick logon and run a command, capturing the output (exactly all that I needed)... I searched high and low for the protocol; SUN, astalavista, HP but to no avail... Eventually, I decided to stop looking and do a hack job but, miraculously, I stumbled accross the protocol in the man pages of our HP-UX system!!!! It's actually piss easy!!!! You just send a port, a login, a password and a command... No messy transactions like the horrble telnet program I wrote... It really couldn't be simpler... You'll need access to a UNIX / LINUX machine to try this out (I tried it on HP-UX 11.00 but I expect that it's pretty standard). Now, even though everybody flames me, I'm sharing it with you.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Option Explicit
Dim wscon As Boolean
'--------------------------------------------------------------
' This code written by John Edward Colman (c) 22/01/2001
' jcolman@bigfoot.com
'
' Create two winsock objects ws and wc
' Create two text-boxes t1 and t2 (set them to multiline=True)
' Create a command button Command1
'
Private Sub Command1_Click()
 
 'Close the sockets if they're open
 CloseSock wc
 CloseSock ws
 wscon = False
 
 'Clear the text boxes
 t1.Text = vbNullString
 t2.Text = vbNullString
 
 'Listen on local port 123 (You can use a different number)
 wc.LocalPort = "123"
 wc.Protocol = sckTCPProtocol
 wc.Listen
 
 'Connect to remote port 512 (this is the rexec port)
 ws.RemotePort = "512"
 ws.RemoteHost = "172.26.2.45"
 ws.Protocol = sckTCPProtocol
 ws.Connect
 
 'Wait until connected
 While wscon = False
  DoEvents
 Wend
 
 'Send your local port number (or you can send just vbNullChar to receive the errors with the output)
 ws.SendData "123" + vbNullChar
 'Send the login
 ws.SendData "edge402" + vbNullChar
 'Send the password
 ws.SendData "fred" + vbNullChar
 'Send the command
 ws.SendData "ls -a" + vbNullChar
End Sub
Private Sub wc_Close()
'Mark socket closure
 t1.Text = t1.Text & vbCrLf & "Closed."
End Sub
Private Sub ws_Close()
'Mark socket closure
 t2.Text = t2.Text & vbCrLf & "Closed."
End Sub
Private Sub wc_ConnectionRequest(ByVal requestID As Long)
'Rexecd tries to connect here to send stderr
 CloseSock wc
 wc.Accept requestID
End Sub
Private Sub wc_DataArrival(ByVal bytesTotal As Long)
'Receive error data
 Dim ee As String
 wc.GetData ee
 t1.Text = t1.Text & ee
End Sub
Private Sub ws_Connect()
'The main loop is waiting for this event
 wscon = True
End Sub
Private Sub ws_DataArrival(ByVal bytesTotal As Long)
'Receive output
 Dim ee As String
 ws.GetData ee
 t2.Text = t2.Text & ee
End Sub
Private Sub CloseSock(ByRef sock As Winsock)
'Close specified socket if open
 While sock.State <> sckClosed
  sock.Close
  DoEvents
 Wend
End Sub
Original Comments (3)
Recovered from Wayback Machine