Python Forum
Failing to connect to a host with WMI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Failing to connect to a host with WMI
#1
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.
Reply
#2
It looks like the connection was refused by the site.
The code seems OK.
check your pwd,, etc.
tester_V likes this post
Reply
#3
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.
Reply
#4
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.
Reply
#5
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.
Reply
#6
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
Reply
#7
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failing regex, space before and after the "match" tester_V 6 1,115 Mar-06-2023, 03:03 PM
Last Post: deanhystad
  Failing to print sorted files tester_V 4 1,188 Nov-12-2022, 06:49 PM
Last Post: tester_V
  Failing reading a file and cannot exit it... tester_V 8 1,757 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Failing regex tester_V 3 1,143 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  where to host my python script tomtom 1 1,233 Feb-09-2022, 06:45 AM
Last Post: ndc85430
  Failing to get Stat for a Directory tester_V 11 3,475 Jul-20-2021, 10:59 PM
Last Post: Larz60+
  Windows 10 Installation failing (0x80070005) AdaptedLogic 4 6,062 Dec-31-2020, 07:39 AM
Last Post: caleb_cruze
  Failing to Zip files tester_V 4 2,106 Dec-01-2020, 07:28 AM
Last Post: tester_V
  Python 3.8 on mac failing to start sgandon 0 2,842 Jan-14-2020, 10:58 AM
Last Post: sgandon
  trying to install oandapy and failing ErnestTBass 0 1,875 Feb-24-2019, 06:13 PM
Last Post: ErnestTBass

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020