Sunday 22 December 2013

Get installed software and running process information in local and remote system

Here we can get all the information about local system and remote system through the module. From this module you can connect by remote computer and by below code you can get all information about it.

you have heard about this module in previous post. this module is nothing but WMI module.
you can download from here 

Subversion: http://svn.timgolden.me.uk/wmi/trunk/ 
Windows installer and zip: http://pypi.python.org/pypi/WMI/#downloads 
Older Versions: http://timgolden.me.uk/python/downloads

List all running processes in the current system.

import wmi
c = wmi.WMI ()

for process in c.Win32_Process ():
  print process.ProcessId, process.Name

you can see all running process in remote system also like as below,
List all running processes in the Remote system.

import wmi
c= wmi.WMI(ip, user=username, password=password)

for process in c.Win32_Process ():
  print process.ProcessId, process.Name

Show the percentage free space for each fixed disk in your system,

import wmi
c = wmi.WMI ()

for disk in c.Win32_LogicalDisk (DriveType=3):
  print disk.Caption, "%0.2f%% free" % (100.0 * long (disk.FreeSpace) / long (disk.Size))

Reboot a remote machine,

import wmi
# other_machine = "machine name of your choice"
c = wmi.WMI (computer=other_machine, privileges=["RemoteShutdown"])

os = c.Win32_OperatingSystem (Primary=1)[0]
os.Reboot ()

Shutdown a remote machine,


import wmi
# other_machine = "machine name of your choice"
c = wmi.WMI (computer=other_machine, privileges=["RemoteShutdown"])

os = c.Win32_OperatingSystem (Primary=1)[0]
os.shutdown ()

Show the IP and MAC addresses for IP-enabled network interfaces

import wmi
c = wmi.WMI ()

for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
  print interface.Description, interface.MACAddress
  for ip_address in interface.IPAddress:
    print ip_address

What’s running on startup and from where in your system,?

import wmi
c = wmi.WMI ()

for s in c.Win32_StartupCommand ():
  print "[%s] %s <%s>" % (s.Location, s.Caption, s.Command)

Watch for errors in the event log in your system,
import wmi
c = wmi.WMI (privileges=["Security"])

watcher = c.watch_for (
  notification_type="Creation",
  wmi_class="Win32_NTLogEvent",
  Type="error"
)
while 1:
  error = watcher ()
  print "Error in %s log: %s" %  (error.Logfile, error.Message)
  # send mail to sysadmin etc.

List registry keys in your system,

import _winreg
import wmi

r = wmi.Registry ()
result, names = r.EnumKey (
  hDefKey=_winreg.HKEY_LOCAL_MACHINE,
  sSubKeyName="Software"
)
for key in names:
  print key

Add a new registry key in system,

import _winreg
import wmi

r = wmi.Registry ()
result, = r.CreateKey (
  hDefKey=_winreg.HKEY_LOCAL_MACHINE,
  sSubKeyName=r"Software\TJG"
)

Add a new registry value in system,

import _winreg
import wmi

r = wmi.Registry ()
result, = r.SetStringValue (
  hDefKey=_winreg.HKEY_LOCAL_MACHINE,
  sSubKeyName=r"Software\TJG",
  sValueName="ApplicationName",
  sValue="TJG App"
)

Show shared drives in system,

import wmi
c = wmi.WMI ()

for share in c.Win32_Share ():
  print share.Name, share.Path

Show disk partitions in local system,
import wmi
c = wmi.WMI ()

for physical_disk in c.Win32_DiskDrive ():
  for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"):
    for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"):
      print physical_disk.Caption, partition.Caption, logical_disk.Caption

Install a product in local system,

import wmi
c = wmi.WMI ()

c.Win32_Product.Install (
  PackageLocation="c:/temp/python-2.4.2.msi",
  AllUsers=False
)
Find Drive Types in your system,

import wmi

DRIVE_TYPES = {
  0 : "Unknown",
  1 : "No Root Directory",
  2 : "Removable Disk",
  3 : "Local Disk",
  4 : "Network Drive",
  5 : "Compact Disc",
  6 : "RAM Disk"
}

c = wmi.WMI ()
for drive in c.Win32_LogicalDisk ():
  print drive.Caption, DRIVE_TYPES[drive.DriveType]

   
 Thanks Guys

No comments:

Post a Comment