Welcome, guest | Sign In | My Account | Store | Cart

RSH sometimes hangs if it cannot resolve to a given host. So here is a quick recipe in python

Python, 46 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python

import subprocess
import time
import sys
import os
from optparse import OptionParser

rsh = "/usr/bin/rsh"
    
def rshTimed(host, command, timeOut):
  rshD = subprocess.Popen([rsh,host,command],stdout=sys.stdout,stderr=sys.stdout)
  timeStart = time.time()
  if(timeOut != 0):
    while(1):
      # Check if timeOut secs have passed since the execution of rsh
      if(int(time.time() - timeStart) > int(timeOut)):
	# Kill the process if timed out
	rshD.kill()
	break
      # Check if the execution of rsh is done before the timeOut value
      elif(rshD.poll() == 0):
	break
      # Sleep for a sec before the next loop.
      time.sleep(1)
  try:
    # If rsh is killed then reap it 
    eStatus = os.waitpid(rshD.pid, 0)[1]
  except:
    # If rsh has been completed successfully 
    eStatus = rshD.returncode
  return(eStatus)
     
  
    
if(__name__ == "__main__"):
  usage = "%prog [-t TIMEOUT] hostname command"
  optParser = OptionParser(usage)
  optParser.add_option("-t","--timeOut",dest="timeOut",help="Timeout value for rsh in seconds",default=0,metavar="TIMEOUT")
  (options, args) = optParser.parse_args()
  if(len(args) < 2):
    optParser.error("incorrect number of arguments")
    sys.exit(1)
  eStatus = rshTimed(args[0],args[1],options.timeOut)
  print("\n")
  sys.exit(eStatus)

1 comment

Shrinidhi Rao (author) 13 years, 7 months ago  # | flag

Hope someone finds this useful :)

Created by Shrinidhi Rao on Mon, 13 Sep 2010 (MIT)
Python recipes (4591)
Shrinidhi Rao's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks