Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't execute msg (and other) Windows commands via subprocess

I have been having some problems with subprocess.call(), subprocess.run(), subprocess.Popen(), os.system(), (and other functions to run command prompt commands) as I can't seem to get the msg command to work.

import subprocess
subprocess.call("msg * Hey",shell=True)

In theory, this should send "Hey" to every computer on the network unfortunately, this isn't running at all and I'm not quite sure why. I can run it on cmd successfully, but can't get it to work from Python.

I'd love to hear why this doesn't work and hopeful a way to fix my code or an alternate solution.

Edit: Solved, thanks for everyone's help. Upgrading to 64-bit Python did the trick.

like image 327
Hensage Avatar asked Jun 01 '18 22:06

Hensage


Video Answer


1 Answers

In 64-bit Windows, "msg.exe" is only distributed as a native 64-bit binary. This file won't be found if you're using 32-bit Python, which executes under WOW64 emulation. WOW64 redirects "System32" access to the "SysWOW64" directory. In Windows 7+, use the virtual "SysNative" directory to access the real "System32". For example:

sysroot = os.environ['SystemRoot']
sysnative = os.path.join(sysroot, 'SysNative')
if not os.path.exists(sysnative):
    sysnative = os.path.join(sysroot, 'System32')

msgexe_path = os.path.join(sysnative, 'msg.exe')
subprocess.call([msgexe_path, '*', 'Hey'])

Note that msg.exe has nothing to do with CMD, so there's no reason to use shell=True.

like image 65
Eryk Sun Avatar answered Nov 10 '22 18:11

Eryk Sun