Posted By


JordanRowles on 04/12/14

Tagged


Statistics


Viewed 1008 times
Favorited by 1 user(s)

SystemInformation.py


/ Published in: Python
Save to your folder(s)

Gets system, machine, OS and networking information and either writes it to a file or returns the data as a string.


Copy this code and paste it in your HTML
  1. #!/usr/bin/env python
  2. #-------------------------------------------------------------------------------
  3. # Name: systemInformation.py
  4. # Version: 1.1
  5. # Author: Jordan Rowles
  6. # Created: 21/02/2014
  7. # Copyright (c) Jordan Rowles 2014
  8. # GNU General Public License Version 3 or Later
  9. #-------------------------------------------------------------------------------
  10. import os, sys, platform
  11. import socket, subprocess
  12. import formatDate
  13.  
  14. sep = ' ' # 2 Spaces
  15.  
  16. class argumentError(ValueError):
  17. 'Error relating to arguments when calling functions'
  18.  
  19. def getMachineInfoDict():
  20. 'Gets all information inside a dictionary'
  21. infoDict = {
  22. 'OS Name:' : getOSName(),
  23. 'Platform (Bit):' : getPlatform(),
  24. 'Win Reg Ver:' : registryVerWin(),
  25. 'Win Version:' : getWindowsInformation(),
  26. 'Architecture:' : getArchitecture(),
  27. 'Machine Type:' : getMachineType(),
  28. 'Network Name:' : getNetworkName(),
  29. 'Machine Platform:' : getMachinePlatform(),
  30. 'Processor:' : processorInformation(),
  31. 'Operating Sys:' : getOperatingSystem(),
  32. 'Platform Release:' : platformRelease(),
  33. 'Machine Info:' : returnMachineInfo(),
  34. 'Win Version:' : getWindowsVersion(),
  35. 'Mac Version:' : getMacOSVersion(),
  36. 'Unix Version:' : getUnixVersion(),
  37. 'Python Version' : pythonVersion(),
  38. 'Python/C API Ver:' : pythonCAPIVersion(),
  39. 'Version Information:' : versionInformation()}
  40. return infoDict
  41. def getInfoDictKeys():
  42. infoDictKeys = infoDict.keys()
  43. return infoDictKeys
  44. def getInfoDictVals():
  45. infoDictVals = infoDict.values()
  46. return infoDictVals
  47.  
  48. def getIPv4():
  49. 'Returns local IP (v4)'
  50. IP = socket.gethostbyname(socket.gethostname())
  51. return IP
  52.  
  53. def getInformationFromCMD():
  54. '''Uses subprocess module to enter ipconfig into cmd, parse the string
  55. and return it as a key-value dictionary.
  56. '''
  57. process = subprocess.Popen('ipconfig /all', stdout=subprocess.PIPE, shell=True)
  58. output = process.communicate()
  59. output = output[0]
  60. # Parse and Format into dictionary
  61. result = {}
  62. for eachRow in output.split('\n'):
  63. if ': ' in eachRow:
  64. key, value = eachRow.split(': ')
  65. result[key.strip(' .')] = value.strip()
  66. return result
  67.  
  68. ## SOME RETURNS NEED TO BE PROPERLY FORMATTED TO STRINGS
  69. ## I.E. FROM TUPLES AND LISTS
  70. def getOSName(formStr=0):
  71. '''OS Names: posix, nt, os2, ce, java or rescos
  72. Args:
  73. 0 - Unformatted (string)
  74. 1 - Capitalised (may not work for os2?)
  75. 2 - Returns formatted for display (string)
  76. 3+ - Raises argumentError exception
  77. '''
  78. if formStr == 0: string = os.name
  79. elif formStr == 1: string = os.name.upper()
  80. elif formStr == 2: string = 'OS Name: %s' % os.name
  81. else: raise argumentError('Enter integer from 0-2')
  82. return string
  83.  
  84. def getPlatform(formStr=False):
  85. 'atheos, riscos, os2emx, os2, darwin, cygwin, win32 or linux2'
  86. if formStr == False: string = sys.platform
  87. else: string = 'Platform (bit): %s' % sys.platform
  88. return string
  89.  
  90. def registryVerWin(formStr=False):
  91. 'Windows registry keys version'
  92. if formStr == False: string = sys.winver
  93. else: string = 'Win Reg Version: %s' % sys.winver
  94. return string
  95.  
  96. def getWindowsInformation(formStr=0):
  97. '''Gets Windows information: Major, Minor, Build, Platform and SP.
  98. Args:
  99. 0 - Unformatted (function and tuple) (default)
  100. 1 - Returns formatted for dispaly (string)
  101. 2 - Returns formatted without label (string)
  102. 3+ - Raises argumentError exception'''
  103. if formStr == 0: string = sys.getwindowsversion()
  104. elif formStr == 1: string = 'Win Info: %s' % formatWindowsInformation()
  105. elif formStr == 2: string = formatWindowsInformation()
  106. else: raise argumentError('Enter integer from 0-2')
  107. return string
  108.  
  109. def getArchitecture(formStr=0):
  110. '''Sets system architecture (bit, linkage/type).
  111. Args:
  112. 0 - Unformatted (tuple) (default)
  113. 1 - Returns just the bit (string)
  114. 2 - Returns just the linkage (string)
  115. 3 - Fully formatted for display (string)
  116. 4 - Returns comma seperated bit and linkage
  117. 5+ - Raises argumentError exception.'''
  118. architecture = platform.architecture()
  119. if formStr == 0: string = architecture #Unformatted
  120. elif formStr == 1: string = architecture[0] #Bit
  121. elif formStr == 2: string = architecture[1] #Linkage
  122. elif formStr == 3: string = 'Architecture: %s, %s' % (architecture[0],
  123. architecture[1])
  124. elif formStr == 4: string = '%s, %s' % (architecture[0], architecture[1])
  125. else: raise argumentError('Enter integer from 0-3')
  126. return string
  127.  
  128. def getMachineType(formStr=False):
  129. 'Returns machine type (i386, x86)'
  130. if formStr == False: string = platform.machine()
  131. else: string = 'Machine Type: %s' % platform.machine()
  132. return string
  133.  
  134. def getNetworkName(formStr=False):
  135. 'Gets machines name on a network (MK02514)'
  136. if formStr == False: string = platform.node()
  137. else: string = 'Network Name: %s' % platform.node()
  138. return string
  139.  
  140. def getMachinePlatform(formStr=False):
  141. 'Gets full machine information Windows-8-6.2.9200'
  142. if formStr == False: string = platform.platform()
  143. else: string = 'Machine Platform: %s' % platform.platform()
  144. return string
  145.  
  146. def processorInformation(formStr=False):
  147. 'Gets processor information'
  148. if formStr == False: string = platform.processor()
  149. else: string = 'Processor: %s' % platform.processor()
  150. return string
  151.  
  152. def getOperatingSystem(formStr=False):
  153. 'Gets OS name'
  154. if formStr == False: string = platform.system()
  155. else: string = 'Operating Sys: %s' % platform.system()
  156. return string
  157.  
  158. def platformRelease(formStr=False):
  159. 'Gets the OS version'
  160. if formStr == False: string = platform.release()
  161. else: string = 'Platform Release: %s' % platform.release()
  162. return string
  163.  
  164. def returnMachineInfo(formStr=False):
  165. 'Returns a tuple of: OS, network name, OS version, @, processor info'
  166. if formStr == False: string = platform.uname()
  167. else: string = 'Machine Info: %s' % platform.uname()
  168. return string
  169.  
  170. def getWindowsVersion(formStr=False):
  171. 'Get Windows Platform. Specific.'
  172. if formStr == False: string = platform.win32_ver()
  173. else: string = 'Win Version: %s' % platform.win32_ver()
  174. return string
  175.  
  176. def getMacOSVersion(formStr=False):
  177. 'Gets Mac Platform. Specific'
  178. if formStr == False: string = platform.mac_ver()
  179. else: string = 'Mac Version: %s' % platform.mac_ver()
  180. return string
  181.  
  182. def getUnixVersion(formStr=False):
  183. 'Get Unix Platform. Specific'
  184. if formStr == False: string = platform.dist()
  185. else: string = 'Unix Version: %s' % platform.dist()
  186. return string
  187.  
  188. # -- PYTHON INFOMRATION --
  189. def pythonVersion(formStr=False):
  190. 'Version of Python'
  191. if formStr == False: string = sys.version
  192. else: string = 'Python Version: %s' % sys.version
  193. return string
  194.  
  195. def pythonCAPIVersion(formStr=False):
  196. 'Python/C Interpreter API Version'
  197. if formStr == False: string = sys.api_version
  198. else: string = 'Python/C Api Ver: %s' % sys.api_version
  199. return string
  200.  
  201. def versionInformation(formStr=False):
  202. 'Python version as named tuple - NEEDS FORMATTING'
  203. pass
  204. if formStr == False:
  205. return sys.version_info
  206. else:
  207. pass
  208. #Format Here
  209.  
  210. def getFileSystemEncoding(formStr=False):
  211. 'Gets the character encoding for the local file system'
  212. encoding = sys.getfilesystemencoding()
  213. if formStr == False: encoding = encoding
  214. elif formStr == True: encoding = encoding.upper()
  215. return encoding
  216.  
  217. # Format Machine Information
  218. def formatWindowsInformation(build=False):
  219. 'Formats information from sys.getwindowsversion()'
  220. windowsInfo = getWindowsInformation()
  221. winMajor = windowsInfo[0] # Major Version
  222. winMinor = windowsInfo[1] # Minor Version
  223. winBuild = windowsInfo[2] # Build Number
  224. servicePack = windowsInfo[4] # Service Pack
  225. platform = windowsInfo[3] # Platform Type
  226. if platform == 0: platType = '3.1' # 0=Win3.1
  227. if platform == 1: platType = '95/98/ME' # 1=95/98/ME
  228. if platform == 2: platType = 'NT' # 2=NT/2000/XP/x64
  229. if platform == 3: platType = 'CE' # 3 = CE
  230. winNum = getWindowsVersion()[0]
  231. if build == False:
  232. formInfo = 'Windows %s %s %i.%i' % (winNum, platType, winMajor, winMinor)
  233. else:
  234. formInfo = 'Windows %s %s %i.%i %i' % (winNum, platType, winMajor, winMinor, winBuild)
  235. return formInfo
  236.  
  237. def formatEnvironmentInformation(formStr=0):
  238. 'Formats information from os.environ'
  239. environInfo = os.environ
  240. userName = environInfo['USERNAME']
  241. compName = environInfo['COMPUTERNAME']
  242. domainNa = environInfo['USERDOMAIN']
  243. userProf = environInfo['USERPROFILE']
  244. if formStr == 1: string = '%s is logged in at %s on %s' % (userName, compName, domainNa)
  245. if formStr == 2: string = userName
  246. if formStr == 3: string = compName
  247. if formStr == 4: string = domainNa
  248. if formStr == 5: string = userProf
  249. if formStr == 0: string = [userName, compName, domainNa, userProf]
  250. return string
  251.  
  252. # Write Information To File
  253. def fileName():
  254. 'Generate filename for information'
  255. filename = '%s.txt' % getNetworkName()
  256. return filename
  257.  
  258. def writeInfoFile():
  259. 'Write information to file'
  260. file = open(fileName(), 'w')
  261. file.write('OS Name: ', getOSName())
  262. file.close()
  263.  
  264. def returnInformation():
  265. 'Returns all information in the syntax - Name: (sep) value()'
  266. print ' OS Name:%s%s, %s' % (sep, getOSName(1), getOperatingSystem())
  267. print 'Platform (bit):%s%s' % (sep, getPlatform())
  268. print ' Win Info:%s%s' % (sep, getWindowsInformation(2))
  269. print ' Architecture:%s%s' % (sep, getArchitecture(4))
  270. print 'Processor Type:%s%s' % (sep, getMachineType())
  271. print ' Network Name:%s%s' % (sep, getNetworkName())
  272. print ' Processor:%s%s' % (sep, processorInformation())
  273. print ' IP Address:%s%s' % (sep, getIPv4())
  274. print ' User Name:%s%s' % (sep, formatEnvironmentInformation(2))
  275. print ' Domian:%s%s' % (sep, formatEnvironmentInformation(4))
  276.  
  277. # [Script] Call returnInformation() on execution
  278. def getBasicInfo():
  279. returnInformation()
  280.  
  281. if __name__ == '__main__':
  282. pass
  283. #getBasicInfo()

URL: SystemInformationPy

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.