summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorDongInn Kim <dikim33@gmail.com>2012-05-13 16:18:35 -0400
committerDongInn Kim <dikim33@gmail.com>2012-05-13 16:21:15 -0400
commit9761afca02e873897c01a8402be5a68a72584b12 (patch)
tree6239ac6cd8bc27db337d39545763c8dc63ee13ff /tools
parentdc63ebe24b7c76b721aa8ed7d8fae278f1a8aa11 (diff)
downloadbcfg2-9761afca02e873897c01a8402be5a68a72584b12.tar.gz
bcfg2-9761afca02e873897c01a8402be5a68a72584b12.tar.bz2
bcfg2-9761afca02e873897c01a8402be5a68a72584b12.zip
Add a script to generate accounts.xml from the existing /etc/passwd file.
Diffstat (limited to 'tools')
-rwxr-xr-xtools/accounts2xml.py120
1 files changed, 120 insertions, 0 deletions
diff --git a/tools/accounts2xml.py b/tools/accounts2xml.py
new file mode 100755
index 000000000..623205018
--- /dev/null
+++ b/tools/accounts2xml.py
@@ -0,0 +1,120 @@
+#!/usr/bin/env python
+#===============================================================================
+#
+# FILE: accounts2xml.py
+#
+# USAGE: ./accounts2xml.py filename node_name
+#
+# DESCRIPTION: A python script to generate accounts.xml containing only the login
+# users from the given /etc/passwd file
+#
+# OPTIONS: ---
+# REQUIREMENTS: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: DongInn Kim (), dikim@cs.indiana.edu
+# ORGANIZATION: Center for Research in Extreme Scale Technologies
+# VERSION: 1.0
+# CREATED: 05/13/2012 01:44:43 PM
+# REVISION: ---
+#===============================================================================
+
+# encoding: utf-8
+
+"""
+accounts2xml.py
+
+This script coverts a csv file to an XML.
+The script takes 2 paramenters
+* filename
+* row node name
+
+e.g., ./accounts2xml.py /etc/passwd UnixUser
+
+Created by Giovanni Collazo on 2011-02-19.
+Copyright (c) 2011 24veces.com. All rights reserved.
+
+Modified by DongInn Kim on 2012-05-13
+Copyright (c) 2012 Indiana University. All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+"""
+
+import sys
+import csv
+import os
+import re
+from xml.dom.minidom import Document
+
+def main(args):
+
+ try:
+ filename = "./copied_passwd"
+ with file(args[1], 'r') as original: data = original.read()
+ with file(filename, 'w') as modified: modified.write("name:pass:uid:gid:gecos:home:shell\n" + data); modified.close()
+ safe_filename = "Properties"
+ except IndexError:
+ print "ERROR: Please provide a filename.csv as the first argument"
+ sys.exit()
+
+ try:
+ single_item = args[2]
+ except IndexError:
+ print "ERROR: Please provide a name to be used for each row node"
+ sys.exit()
+
+ f = csv.reader(open(filename, 'rb'), delimiter=':')
+
+ doc = Document()
+ root_element = doc.createElement(safe_filename)
+ doc.appendChild(root_element)
+
+ columns = f.next()
+
+ for row in f:
+ match = re.search(r'/bin/\w*sh', row[6]) # adjust this line to match the right shell path
+ if match:
+ item = doc.createElement(single_item)
+ root_element.appendChild(item)
+ extra_groups = os.popen("groups %s" % row[0]).readline()[:-1]
+ item.setAttribute('extra_groups', extra_groups.split(' : ')[1])
+ create_col_nodes(columns, item, doc, row)
+
+ output_file = "accounts.xml"
+ doc.writexml(open(output_file, 'w'), addindent=' ', newl='\n') # Write file
+
+ print "Done: Created %s" % output_file
+ os.remove(filename)
+
+def create_col_nodes(cols, item, doc, row):
+ for col in cols:
+ att = doc.createAttribute(str.replace(col, " ", "_").lower())
+ att.nodeValue = row.pop(0)
+
+ if col != "pass":
+ item.setAttributeNode(att)
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv))
+
+# vim:set sr et ts=4 sw=4 ft=python fenc=utf-8: // See Vim, :help 'modeline'
+# Created: Sun, 13 May 2012 13:44:43 -0400
+
+