0

anyone could help me with python trying to use NET use, I don't know the diferences between / in python and perl, because code in perl works

$runMap = "C:\\Windows\\System32\\net.exe use \\\\$ip\\D\$ /persistent:no /user:$user_name $passwd"; 
system($runMap);

But in Python 3 don't work

os.system("C:/Windows/System32/net.exe use Z: \\\\ip/D:/ /persistent:no /user:user pass")
styvane
  • 59,869
  • 19
  • 150
  • 156
  • 1. [Use raw strings.](http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-in-python-and-what-are-raw-string-l) 2. `C:/Windows/System32/net.exe` aren't those backslashes supposed to go in the other direction? – NightShadeQueen Jul 16 '15 at 19:53
  • first understand what you're doing before attempting to run an exe. I would recommend doing os.system("cd") (if windows) os.system("pwd") (if linux). Then you'll understand where you are – FirebladeDan Jul 16 '15 at 19:59
  • You are clearly running different commands, so why do you expect same results? – el.pescado - нет войне Mar 20 '18 at 07:57

1 Answers1

5

Perl is using interpolation, that is, it is possible to embed variables inside a double quoted string, since Perl 5 interpolated variables start with a $ or a @ marker. In your case you are embedding $user_name and $passwd.

Python variable names are not prefixed by a "magic character" (sigil), so you cannot embed them inside strings except by using formatting statements. There are a couple of regimes, here is one which is a similar idea to printf:

cmd = "C:/Windows/System32/net.exe use Z: \\\\ip/D:/ /persistent:no /user:%s %s" % (username, passwd)

os.system(cmd)

As an ex-Perlmonger I missed interpolation so much I wrote a Python module to support it. While I learnt a lot about Python doing it, it was otherwise a waste of time. Python programming is a different style, you don't need interpolation any more.

By the way, unlike Perl's system(), Python's os.system() will always spawn a shell (as does C's). Therefore it is generally considered to be deprecated. The subprocess.Popen() method gives much more control.

EDIT:

With the advent of Python 3.6 and Literal String Interpolation (specified in PEP 498) - more commonly known as f-strings - my original post needs another way to do it.

Single or double quotes may be used, even triple quotes. Basically we just put the Python expression, commonly a variable, inside braces (similar to Ruby).

So, for example:

os.system(f"C:/Windows/System32/net.exe use Z: \\\\ip/D:/ /persistent:no /user:{username} {passwd}")

The comment about subprocess.Popen() is also out of date, since Python 3.5 the preferred interface is now subprocess.run().

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • `pass` is a reserved keyword in Python. Also, `\\ip/D:/` is an invalid UNC path. I think the OP wants `\\ip\D$`. Also, drive `Z:` may be assigned already, so I'd pass `*` to let the net command assign the drive letter, and then search the output for the assigned drive letter. For example: `net_path = r'%s\System32\net.exe' % os.environ['SystemRoot'];` `unc_path = r'\\ip\D$';` `cmd = '%s use * %s /persistent:no /user:%s %s' % (net_path, unc_path, user, password);` `output = subprocess.check_output(cmd, universal_newlines=True);` `drive = re.findall('\s([A-Z]:)\s', output)[0]`. – Eryk Sun Jul 16 '15 at 21:17
  • @eryksun: oops, I missed the `pass`. – cdarke Jul 16 '15 at 21:46
  • Also the UNC path is invalid and not the same as the Perl code, so the command fails. The OP wants the `D$` 'hidden' share for drive D:. – Eryk Sun Jul 16 '15 at 22:01
  • Also the OP shouldn't assume that Windows is installed to `C:\Windows`. – Eryk Sun Jul 16 '15 at 22:06
  • Thanks I try use your example but invalid password msg with net use and password is correct, maybe the error occurs because have special characters, like #@... in passwd. – guilherme medeiros Jul 17 '15 at 13:28