1#!/usr/bin/env python 2# 3# (C) 2001 by Argonne National Laboratory. 4# See COPYRIGHT in top-level directory. 5# 6 7## NOTE: we do NOT allow this pgm to run via mpdroot 8 9""" 10usage: mpdcleanup [-h] [-v] [-f <hostsfile>] [-r <rshcmd>] [-u <user>] [-c <cleancmd>] [-k 'killcmd'] [-n <num_from_hostsfile>] 11 or: mpdcleanup [--help] [--verbose] [--file=<hostsfile>] [--rsh=<rshcmd>] [--user=<user>] 12 [--clean=<cleancmd>] [--kill="killcmd"] 13Removes the Unix socket on local (the default) and remote machines 14This is useful in case the mpd crashed badly and did not remove it, which it normally does 15""" 16from time import ctime 17__author__ = "Ralph Butler and Rusty Lusk" 18__date__ = ctime() 19__version__ = "$Revision: 1.11 $" 20__credits__ = "" 21 22 23import sys, os, socket 24 25from getopt import getopt 26from mpdlib import mpd_get_my_username, mpd_same_ips, mpd_set_tmpdir 27 28def mpdcleanup(): 29 rshCmd = 'ssh' 30 user = mpd_get_my_username() 31 killCmd = '' # perhaps '~/bin/kj mpd' (in quotes) 32 cleanCmd = 'rm -f ' 33 hostsFile = '' 34 verbose = 0 35 numFromHostsFile = 0 # chgd below 36 try: 37 (opts, args) = getopt(sys.argv[1:], 'hvf:r:u:c:k:n:', 38 ['help', 'verbose', 'file=', 'rsh=', 'user=', 'clean=','kill=']) 39 except: 40 print 'invalid arg(s) specified' 41 usage() 42 else: 43 for opt in opts: 44 if opt[0] == '-r' or opt[0] == '--rsh': 45 rshCmd = opt[1] 46 elif opt[0] == '-u' or opt[0] == '--user': 47 user = opt[1] 48 elif opt[0] == '-f' or opt[0] == '--file': 49 hostsFile = opt[1] 50 elif opt[0] == '-h' or opt[0] == '--help': 51 usage() 52 elif opt[0] == '-v' or opt[0] == '--verbose': 53 verbose = 1 54 elif opt[0] == '-n': 55 numFromHostsFile = int(opt[1]) 56 elif opt[0] == '-c' or opt[0] == '--clean': 57 cleanCmd = opt[1] 58 elif opt[0] == '-k' or opt[0] == '--kill': 59 killCmd = opt[1] 60 if args: 61 print 'invalid arg(s) specified: ' + ' '.join(args) 62 usage() 63 64 if os.environ.has_key('MPD_CON_EXT'): 65 conExt = '_' + os.environ['MPD_CON_EXT'] 66 else: 67 conExt = '' 68 if os.environ.has_key('MPD_TMPDIR'): 69 tmpdir = os.environ['MPD_TMPDIR'] 70 else: 71 tmpdir = '/tmp' 72 cleanFile = tmpdir + '/mpd2.console_' + user + conExt 73 if rshCmd == 'ssh': 74 xOpt = '-x' 75 else: 76 xOpt = '' 77 try: localIP = socket.gethostbyname_ex(socket.gethostname())[2] 78 except: localIP = 'unknownlocal' 79 80 if hostsFile: 81 try: 82 f = open(hostsFile,'r') 83 except: 84 print 'Not cleaning up on remote hosts; file %s not found' % hostsFile 85 sys.exit(0) 86 hosts = f.readlines() 87 if numFromHostsFile: 88 hosts = hosts[0:numFromHostsFile] 89 for host in hosts: 90 host = host.strip() 91 if host[0] != '#': 92 try: remoteIP = socket.gethostbyname_ex(host)[2] 93 except: remoteIP = 'unknownremote' 94 if localIP == remoteIP: # local machine handled last below loop 95 continue 96 cmd = '%s %s -n %s %s %s &' % (rshCmd, xOpt, host, cleanCmd, cleanFile) 97 if verbose: 98 print 'cmd=:%s:' % (cmd) 99 os.system(cmd) 100 if killCmd: 101 cmd = "%s %s -n %s \"/bin/sh -c '%s' &\"" % (rshCmd, xOpt, host, killCmd) 102 if verbose: 103 print "cmd=:%s:" % (cmd) 104 os.system(cmd) 105 106 ## clean up local machine last 107 cmd = '%s %s' % (cleanCmd,cleanFile) 108 if verbose: 109 print 'cmd=:%s:' % (cmd) 110 os.system(cmd) 111 if killCmd: 112 if verbose: 113 print 'cmd=:%s:' % (killCmd) 114 os.system(killCmd) 115 116def usage(): 117 print __doc__ 118 sys.exit(-1) 119 120 121if __name__ == '__main__': 122 mpdcleanup() 123