0

So I'm trying to change the ntp server settings in Windows (XP and 7) using the following:

import subprocess
subprocess.call(['net', 'stop', 'w32time'])
subprocess.call(['reg', 'add','HKLM\Software\Microsoft\Windows\CurrentVersion\DateTime\Servers', '/f /v \"0\" /t REG_SZ /d \"ntp.craven.k12.nc.us\"'])
subprocess.call(['reg', 'add', 'HKLM\Software\Microsoft\Windows\CurrentVersion\DateTime\Servers', '/f /v \"(Default)\" /t REG_SZ /d \"0\"'])
subprocess.call(['net', 'start', 'w32time'])
subprocess.call(['w32tm', '/resync'])

But this fails miserably. I'm sure the problem lies in how I'm formatting the parameters, but I have yet to come upon how to do it properly.

davelupt
  • 1,845
  • 4
  • 21
  • 32
  • Yes, the first and last two commands run as I would expect them too, it's the ones that edit the registry that have proved tricky. – davelupt Mar 10 '14 at 18:26
  • unrelated: you don't need `subprocess` to edit the registry, see [winreg](http://docs.python.org/3/library/winreg.html) – jfs Mar 10 '14 at 18:34
  • You need to escape the '\' character or use raw strings, e.g., `'HKLM\\Software\\Microsoft\\...'` or `r'HKLM\Software\Microsoft\...'` – tdelaney Mar 10 '14 at 18:36
  • @J.F.Sebastian I'm coming to the conclusion that there's a good reason for the inclusion of winreg in version 3, but I'm limited to using 2.7. I've noticed that if I perform a subprocess.call with a reg query it will give a different response than had I run it directly. Any idea why? – davelupt Mar 10 '14 at 20:13
  • winreg is called _winreg in Python 2 – jfs Mar 10 '14 at 20:48
  • Provide complete commands and their results (probably as a new question). My guess: the commands are different or they are not idempotent – jfs Mar 10 '14 at 20:51
  • Thanks much @J.F.Sebastian, Figured out I running 32 bit Python on a 64 bit machine, so my results were from Wow6432Node. Was able to fix it with help from: http://stackoverflow.com/questions/11808462/avoid-registry-wow6432node-redirection – davelupt Mar 11 '14 at 12:29

1 Answers1

2

Your last arguments are not splitted. You probably need to replace '/f /v \"0\" /t REG_SZ ...' with ] + ['/f', '/v', '0', '/t', 'REG_SZ'] + [...].

As an alternative, pass the whole command as a string (as you would on the command line).

jfs
  • 399,953
  • 195
  • 994
  • 1,670