Hi friends this is my python application that provide system information.....
____________________________________
1. #!/usr/bin/env python
2. #-------------------------------------------------------------------------------
3. # Name: systemInformation.py
4. # Version: 1.1
5. # Author: vivek kumar
6. # Created: 21/02/2014
7. # Copyright (c) spyboy 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)
00. 1 - Returns formatted for dispaly (string)
01. 2 - Returns formatted without label (string)
02. 3+ - Raises argumentError exception'''
03. if formStr == 0: string = sys .getwindowsversion()
04. elif formStr == 1 : string = 'Win Info: %s' % formatWindowsInformation ()
05. elif formStr == 2 : string = formatWindowsInformation ()
06. else: raise argumentError ( 'Enter integer from 0-2' )
07. return string
08.
09. def getArchitecture (formStr=0 ) :
10. '''Sets system architecture (bit, linkage/type).
11. Args:
12. 0 - Unformatted (tuple) (default)
13. 1 - Returns just the bit (string)
14. 2 - Returns just the linkage (string)
15. 3 - Fully formatted for display (string)
16. 4 - Returns comma seperated bit and linkage
17. 5+ - Raises argumentError exception.'''
18. architecture = platform.architecture ()
19. if formStr == 0: string = architecture #Unformatted
20. elif formStr == 1 : string = architecture [0] #Bit
21. elif formStr == 2 : string = architecture [1] #Linkage
22. elif formStr == 3 : string = 'Architecture: %s, %s' % (architecture[ 0],
23. architecture [1])
24. elif formStr == 4 : string = '%s, %s' % (architecture[ 0] , architecture[ 1])
25. else: raise argumentError ( 'Enter integer from 0-3' )
26. return string
27.
28. def getMachineType (formStr= False):
29. 'Returns machine type (i386, x86)'
30. if formStr == False : string = platform.machine ()
31. else: string = 'Machine Type: %s' % platform.machine ()
32. return string
33.
34. def getNetworkName (formStr= False):
35. 'Gets machines name on a network (MK02514)'
36. if formStr == False : string = platform.node()
37. else: string = 'Network Name: %s' % platform.node ()
38. return string
39.
40. def getMachinePlatform (formStr= False):
41. 'Gets full machine information Windows-8-6.2.9200'
42. if formStr == False : string = platform.platform()
43. else: string = 'Machine Platform: %s' % platform. platform()
44. return string
45.
46. def processorInformation (formStr= False) :
47. 'Gets processor information'
48. if formStr == False : string = platform.processor()
49. else: string = 'Processor: %s' % platform.processor ()
50. return string
51.
52. def getOperatingSystem (formStr= False):
53. 'Gets OS name'
54. if formStr == False : string = platform.system ()
55. else: string = 'Operating Sys: %s' % platform.system ()
56. return string
57.
58. def platformRelease (formStr= False) :
59. 'Gets the OS version'
60. if formStr == False : string = platform.release ()
61. else: string = 'Platform Release: %s' % platform.release ()
62. return string
63.
64. def returnMachineInfo ( formStr= False ):
65. 'Returns a tuple of: OS, network name, OS version, @, processor info'
66. if formStr == False : string = platform.uname()
67. else: string = 'Machine Info: %s' % platform.uname ()
68. return string
69.
70. def getWindowsVersion ( formStr= False ):
71. 'Get Windows Platform. Specific.'
72. if formStr == False : string = platform.win32_ver ()
73. else: string = 'Win Version: %s' % platform.win32_ver ()
74. return string
75.
76. def getMacOSVersion (formStr= False) :
77. 'Gets Mac Platform. Specific'
78. if formStr == False : string = platform.mac_ver ()
79. else: string = 'Mac Version: %s' % platform.mac_ver ()
80. return string
81.
82. def getUnixVersion (formStr= False):
83. 'Get Unix Platform. Specific'
84. if formStr == False : string = platform.dist()
85. else: string = 'Unix Version: %s' % platform.dist ()
86. return string
87.
88. # -- PYTHON INFOMRATION --
89. def pythonVersion ( formStr= False ):
90. 'Version of Python'
91. if formStr == False : string = sys .version
92. else: string = 'Python Version: %s' % sys .version
93. return string
94.
95. def pythonCAPIVersion ( formStr= False ):
96. 'Python/C Interpreter API Version'
97. if formStr == False : string = sys .api_version
98. else: string = 'Python/C Api Ver: %s' % sys .api_version
99. return string
00.
01. def versionInformation (formStr= False):
02. 'Python version as named tuple - NEEDS FORMATTING'
03. pass
04. if formStr == False :
05. return sys .version_info
06. else:
07. pass
08. #Format Here
09.
10. def getFileSystemEncoding ( formStr= False ):
11. 'Gets the character encoding for the local file system'
12. encoding = sys .getfilesystemencoding()
13. if formStr == False : encoding = encoding
14. elif formStr == True: encoding = encoding.upper ()
15. return encoding
16.
17. # Format Machine Information
18. def formatWindowsInformation (build= False) :
19. 'Formats information from sys.getwindowsversion()'
20. windowsInfo = getWindowsInformation ()
21. winMajor = windowsInfo [0 ] # Major Version
22. winMinor = windowsInfo [1 ] # Minor Version
23. winBuild = windowsInfo [2 ] # Build Number
24. servicePack = windowsInfo [4 ] # Service Pack
25. platform = windowsInfo [3 ] # Platform Type
26. if platform == 0: platType = '3.1' # 0=Win3.1
27. if platform == 1 : platType = '95/98/ME' # 1=95/98/ME
28. if platform == 2 : platType = 'NT' # 2=NT/2000/XP/x64
29. if platform == 3 : platType = 'CE' # 3 = CE
30. winNum = getWindowsVersion ()[ 0]
31. if build == False:
32. formInfo = 'Windows %s %s %i.%i' % ( winNum, platType, winMajor, winMinor )
33. else:
34. formInfo = 'Windows %s %s %i.%i %i' % ( winNum, platType, winMajor, winMinor, winBuild
35. return formInfo
36.
37. def formatEnvironmentInformation (formStr=0 ) :
38. 'Formats information from os.environ'
39. environInfo = os.environ
40. userName = environInfo ['USERNAME' ]
41. compName = environInfo ['COMPUTERNAME' ]
42. domainNa = environInfo ['USERDOMAIN' ]
43. userProf = environInfo ['USERPROFILE' ]
44. if formStr == 1 : string = '%s is logged in at %s on %s' % (userName, compName, domainNa )
45. if formStr == 2 : string = userName
46. if formStr == 3 : string = compName
47. if formStr == 4 : string = domainNa
48. if formStr == 5 : string = userProf
49. if formStr == 0: string = [ userName, compName, domainNa, userProf ]
50. return string
51.
52. # Write Information To File
53. def fileName () :
54. 'Generate filename for information'
55. filename = '%s.txt' % getNetworkName ()
56. return filename
57.
58. def writeInfoFile () :
59. 'Write information to file'
60. file = open(fileName() , 'w' )
61. file.write ( 'OS Name: ' , getOSName ())
62. file.close ()
63.
64. def returnInformation () :
65. 'Returns all information in the syntax - Name: (sep) value()'
66. print ' OS Name:%s%s, %s' % (sep, getOSName (1 ), getOperatingSystem ())
67. print 'Platform (bit):%s%s' % ( sep, getPlatform ())
68. print ' Win Info:%s%s' % ( sep, getWindowsInformation ( 2))
69. print ' Architecture:%s%s' % (sep, getArchitecture ( 4))
70. print 'Processor Type:%s%s' % (sep, getMachineType ())
71. print ' Network Name:%s%s' % ( sep, getNetworkName ())
72. print ' Processor:%s%s' % (sep, processorInformation ())
73. print ' IP Address:%s%s' % ( sep, getIPv4 ())
74. print ' User Name:%s%s' % (sep, formatEnvironmentInformation ( 2))
75. print ' Domian:%s%s' % (sep, formatEnvironmentInformation ( 4))
76.
77. # [Script] Call returnInformation() on execution
78. def getBasicInfo () :
79. returnInformation ()
80.
81. if __name__ == '__main__' :
82. pass
83. #getBasicInfo()
Sunday, February 26, 2017
Hardware data source code python
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment