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 to connect by 'net use' tester_V 1 158 Apr-20-2024, 06:31 AM
Last Post: tester_V
  Failing regex tester_V 3 1,177 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  where to host my python script tomtom 1 1,264 Feb-09-2022, 06:45 AM
Last Post: ndc85430
  Failing to get Stat for a Directory tester_V 11 3,549 Jul-20-2021, 10:59 PM
Last Post: Larz60+
  Failing to Zip files tester_V 4 2,156 Dec-01-2020, 07:28 AM
Last Post: tester_V
  Python 3.8 on mac failing to start sgandon 0 2,906 Jan-14-2020, 10:58 AM
Last Post: sgandon
  trying to install oandapy and failing ErnestTBass 0 1,900 Feb-24-2019, 06:13 PM
Last Post: ErnestTBass
  Pyinstaller failing JP_ROMANO 2 4,065 Jan-16-2019, 06:07 PM
Last Post: JP_ROMANO
  Why is my for loop failing? microphone_head 4 2,983 Sep-11-2018, 01:21 PM
Last Post: microphone_head
  Get host from ip antmar904 3 2,883 Jan-31-2018, 08:27 PM
Last Post: antmar904

Forum Jump:

User Panel Messages

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