Python Forum

Full Version: Failing to connect to a host with WMI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Happy Friday!
I think I managed to install "WMI" module.
When I use:
 import wmi
My script does not error out. The remote host is online, I used the admin username and pass.
Script:
try:
    connection = wmi.WMI('245.234.44.56', user="Some_User", password="password")
    print("connection is established")
except:
    print("connection failed")
Output:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'SWbemLocator', 'Access is denied. ', None, 0, -2147024891), None)
I'm wondering if Python has everything installed to make the connection...

Thank you.
It looks like the connection was refused by the site.
The code seems OK.
check your pwd,, etc.
I do not understand why it did not work.
I tried different snippet and this one works:
import wmi

ip = '101.17.48.11'
username = 'Some_user'
password = 'somepassword'
from socket import *
try:
    print("Establishing connection to %s" %ip)
    connection = wmi.WMI(ip, user=username, password=password)
    print("Connection established")
except wmi.x_wmi:
    print("Your Username and Password of "+getfqdn(ip)+" are wrong.")
I'd like to get a processes list of the remote host I just connected .
Should I make a batch file and make a 'call' to run it or call the Power shell?
What is a proper way to do this?

Thank you.
Hi,
What about starting a batch file on a remote host?
I can copy a batch file to read the processes of a Host.
Does anyone know what is the command for that?
I cannot find anything useful on how to do that.

Thank you.
Well, I was looking for an example for a few days and just found it.
Plugged it in ad it works!
here is the whole snipped if anyone is interested:

import wmi
from socket import *

ip = '101.17.48.11'
username = 'Some_user'
password = 'somepassword'
proc=open('c:\\somedir','w')
try:
    print("Establishing connection to %s" %ip)
    connection = wmi.WMI(ip, user=username, password=password)
    connection = wmi.WMI(ip)
    print("Connection established")

    for process in connection.Win32_Process():
        #print("ID: {0}\nProcessName: {1}\n".format(process.ProcessId,process.Name))
        print(f"Proccess-ID/Nmae -> {process.ProcessId} - {process.Name} ")
        proc.write(str(process.ProcessId)+","+process.Name+"\n")         
except wmi.x_wmi:
    print("Your Username and Password of "+getfqdn(ip)+" are wrong.")
proc.close()
Thank you everyone for the support and coaching!
I think this is the best place to learn Python.
Some where along the line, you changed IP addresses, first attempt shows 245.234.44.56
The one that works shows: 101.17.48.11
(Aug-04-2021, 01:27 AM)Larz60+ Wrote: [ -> ]Some where along the line, you changed IP addresses, first attempt shows 245.234.44.56
The one that works shows: 101.17.48.11

it is not real IP it was just an example.
I'm ok now.
THank you!