Python Help

Hey everyone,

I was hoping for some python programming assistance.
I have limited experience in Python but wanting to learn.

My python script:

##############################################
import winrm

s = winrm.Session(‘127.0.0.1’, auth=(‘dude’,‘dude’))
r = session.run_ps(“hostname”)
print(result.std_out)
##############################################

I got the script above from

Anyway, when I run my script I get the following error:
’ AttributeError: ‘module’ object has no attribute ‘Session’ ’

Bit lost since I believe I have everything I need to run winrm (I installed pywinrm==0.2.2 from the site above).

Maybe its an old module or something is out-dated in the example code? (speculating)
Note; also installed; ‘pip install pywinrm’

Hi, Check the script if run in python (2.7) or python3. Your syntax looks like python3 and you must call it as python3 myscript.py.

If you are interested in winrm there is a project named evil-winrm that works great. It is on github. Google it.

Hey AcidBat, since you have 0 (or very minimal knowledge) of python, I will try to explain as much as I can with my also limited knowledge of Python ■■■■■.

When you do s = winrm.Session('127.0.0.1', auth=('dude','dude')) You are making an instance of the winrm class with the Session being the method.

and at the second line you do r = session.run_ps("hostname") from my limited observation it seems to mean that you want to execute the cmdlet “hostname” in powershell, the only issue you are doing right now is the fact that you need to replace session with the svariable, and change print(result.std_out) to print(r.std_out)

I am currently at school so I can’t directly test this code myself, so here is the full code:

import winrm

s = winrm.Session('127.0.0.1', auth=('dude', 'dude'))
r = s.run_ps("hostname")
print(r.std_out)

Please let me know if this works!

EDIT: Forgot to mention, I am no prodigy/expert python programming of any kind, I simply wish to help you and take everything I (a noob) says with a grain of salt :smiley:

Type your comment> @Darvidor said:

Hi, Check the script if run in python (2.7) or python3. Your syntax looks like python3 and you must call it as python3 myscript.py.

If you are interested in winrm there is a project named evil-winrm that works great. It is on github. Google it.

Thank you for the reply mate,

I tried the script using python2.7 & python3.
Python2.7 gave me the same results and Python3 slightly different:
‘AttributeError: partially initialized module ‘winrm’ has no attribute ‘Session’ (most likely due to a circular import)’ … ??

I have used evil-winrm several-times and its good, just wanted to make something simple myself to test a connection (troubleshooting something that I am working on)

@PapyrusTheGuru said:
Hey AcidBat, since you have 0 (or very minimal knowledge) of python, I will try to explain as much as I can with my also limited knowledge of Python ■■■■■.

When you do s = winrm.Session('127.0.0.1', auth=('dude','dude')) You are making an instance of the winrm class with the Session being the method.

and at the second line you do r = session.run_ps("hostname") from my limited observation it seems to mean that you want to execute the cmdlet “hostname” in powershell, the only issue you are doing right now is the fact that you need to replace session with the svariable, and change print(result.std_out) to print(r.std_out)

I am currently at school so I can’t directly test this code myself, so here is the full code:

import winrm

s = winrm.Session('127.0.0.1', auth=('dude', 'dude'))
r = s.run_ps("hostname")
print(r.std_out)

Please let me know if this works!

EDIT: Forgot to mention, I am no prodigy/expert python programming of any kind, I simply wish to help you and take everything I (a noob) says with a grain of salt :smiley:

that actually worked like a charm with python3…

Thank you @PapyrusTheGuru & @Darvidor | your combined answers put me on the right track :slight_smile:

Type your comment> @acidbat said:

@PapyrusTheGuru said:
Hey AcidBat, since you have 0 (or very minimal knowledge) of python, I will try to explain as much as I can with my also limited knowledge of Python ■■■■■.

When you do s = winrm.Session('127.0.0.1', auth=('dude','dude')) You are making an instance of the winrm class with the Session being the method.

and at the second line you do r = session.run_ps("hostname") from my limited observation it seems to mean that you want to execute the cmdlet “hostname” in powershell, the only issue you are doing right now is the fact that you need to replace session with the svariable, and change print(result.std_out) to print(r.std_out)

I am currently at school so I can’t directly test this code myself, so here is the full code:

import winrm

s = winrm.Session('127.0.0.1', auth=('dude', 'dude'))
r = s.run_ps("hostname")
print(r.std_out)

Please let me know if this works!

EDIT: Forgot to mention, I am no prodigy/expert python programming of any kind, I simply wish to help you and take everything I (a noob) says with a grain of salt :smiley:

that actually worked like a charm with python3…

Thank you @PapyrusTheGuru & @Darvidor | your combined answers put me on the right track :slight_smile:

Super glad to hear that! I hope the project you’re currently working on will educate you a lot about Python, as it certainly is an amazing language. And two heads are better than one!

Type your comment> @PapyrusTheGuru said:

Super glad to hear that! I hope the project you’re currently working on will educate you a lot about Python, as it certainly is an amazing language. And two heads are better than one!

It certainly is mate :slight_smile:

Type your comment> @sparkla said:

What’s really helpful to me in general is using the python console and tabbing (autocompleting) my way through a problem, printing out variables and so on. It’s much better than setting up debuggers and linters for such small problems.

Good tip mate, thanks for that :slight_smile:

When using dependencies try to use virtualenv, it will allow you to separate installed dependencies per project and keep everything clean.

Type your comment> @kademlia said:

When using dependencies try to use virtualenv, it will allow you to separate installed dependencies per project and keep everything clean.

Thanks @kademlia - I will look into the virtualenv option :slight_smile:

Spoiler Removed