Error executing Windows command
Description of Issue/Question
Hello,
I'm trying to run a command to register a linux host with a Windows DNS server using SaltStack Reactor. The command(s) work when I run them in a command prompt but running them via the minion produce the following error message:
"'dnscmd.exe' is not recognized as an internal or external command,\r\noperable program or batch file."
I've tried changing the working directory accordingly but I still can't get the command to run. Here is the event which is returned by the DNS server:
salt/job/20170510090002413792/ret/xxx {
"_stamp": "2017-05-10T16:00:05.967069",
"cmd": "_return",
"fun": "cmd.run",
"fun_args": [
"(dnscmd.exe 127.0.0.1 /EnumRecords zone.name linuxhostname /type A | findstr xxx.xxx.xxx.xxx) || (dnscmd.exe 127.0.0.1 /RecordDelete zone.name linuxhostname A /f & dnscmd.exe 127.0.0.1 /RecordAdd zone.name linuxhostname /CreatePTR A xxx.xxx.xxx.xxx)"
],
"id": "xxx",
"jid": "20170510090002413792",
"retcode": 255,
"return": "'dnscmd.exe' is not recognized as an internal or external command,\r\noperable program or batch file.",
"success": true
}
Setup
Following is the reactor code:
register_host_dns:
local.cmd.run:
- tgt: ${DNSServer}
- arg:
- '(dnscmd.exe 127.0.0.1 /EnumRecords ${Zone} ${MinionName} /type A | findstr ${IPAddress_v4}) || (dnscmd.exe 127.0.0.1 /RecordDelete ${Zone} ${MinionName} A /f & dnscmd.exe 127.0.0.1 /RecordAdd ${Zone} ${MinionName} /CreatePTR A ${IPAddress_v4})'
- cwd: 'C:\Windows\System32'
Following is the code that fires the event:
update_dns_record:
event.send:
- name: 'xxxxx/minion/update/dns'
- data:
ipv4: '${IpAddress_v4}'
Steps to Reproduce Issue
(Include debug logs if possible and relevant.)
Versions Report
Salt Master:
Salt Version:
Salt: 2016.11.4
Dependency Versions:
cffi: 1.6.0
cherrypy: Not Installed
dateutil: Not Installed
docker-py: Not Installed
gitdb: 0.6.4
gitpython: 1.0.1
ioflo: 1.3.8
Jinja2: 2.7.2
libgit2: 0.24.6
libnacl: 1.4.3
M2Crypto: 0.21.1
Mako: 0.8.1
msgpack-pure: Not Installed
msgpack-python: 0.4.8
mysql-python: Not Installed
pycparser: 2.14
pycrypto: 2.6.1
pycryptodome: 3.4.3
pygit2: 0.24.2
Python: 2.7.5 (default, Nov 6 2016, 00:28:07)
python-gnupg: Not Installed
PyYAML: 3.11
PyZMQ: 15.3.0
RAET: 0.6.5
smmap: 0.9.0
timelib: Not Installed
Tornado: 4.2.1
ZMQ: 4.1.4
System Versions:
dist: centos 7.3.1611 Core
machine: x86_64
release: 3.10.0-514.10.2.el7.x86_64
system: Linux
version: CentOS Linux 7.3.1611 Core
Salt Minion:
Salt Version:
Salt: 2016.11.4
Dependency Versions:
cffi: 1.10.0
cherrypy: 7.1.0
dateutil: 2.5.3
docker-py: Not Installed
gitdb: 0.6.4
gitpython: 2.0.8
ioflo: 1.5.5
Jinja2: 2.9.4
libgit2: Not Installed
libnacl: 1.4.5
M2Crypto: Not Installed
Mako: 1.0.4
msgpack-pure: Not Installed
msgpack-python: 0.4.8
mysql-python: Not Installed
pycparser: 2.17
pycrypto: 2.6.1
pycryptodome: Not Installed
pygit2: Not Installed
Python: 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)]
python-gnupg: 0.3.8
PyYAML: 3.11
PyZMQ: 16.0.1
RAET: Not Installed
smmap: 0.9.0
timelib: 0.2.4
Tornado: 4.4.1
ZMQ: 4.1.6
System Versions:
dist:
machine: AMD64
release: 2008ServerR2
system: Windows
version: 2008ServerR2 6.1.7601 SP1 Multiprocessor Free
@twangboy do you know how he could add dnscmd.exe to the path when running with salt?
@haeac Has the minion been restarted since you installed DNS Server Tools?
Could you paste the exact commands you ran? How did you change the working directory and what did you change it to?
@twangboy I set the cwd parameter in the local.cmd.run state of the reactor. Please review snippet in Setup section. Following is the command it's supposed to run:
(dnscmd.exe 127.0.0.1 /EnumRecords abc.local test-01 /type A | findstr 192.168.0.10) || (dnscmd.exe 127.0.0.1 /RecordDelete abc.local test-01 A /f & dnscmd.exe 127.0.0.1 /RecordAdd abc.local test-01 /CreatePTR A 192.168.0.10)
@haeac I guess I'd like to see the state you're using then. Would it be possible to post that?
@twangboy please see below:
Reactor file:
#!mako|yaml
## vim: sts=2 ts=2 sw=2 et ai
<%
AllowedNetworks = ('192.168.0.', '192.168.1.', '192.168.2.')
MinionName = data['id']
DNSServer = 'dns-srv-1'
Zone = 'domain.local'
ValidNetwork = False
if 'ipv4' in data['data']:
IPAddress_v4 = data['data']['ipv4']
else:
IPAddress_v4 = ''
if IPAddress_v4.startswith(AllowedNetworks):
ValidNetwork = True
%>
% if IPAddress_v4 and MinionName and ValidNetwork:
register_host_dns:
local.cmd.run:
- tgt: ${DNSServer}
- arg:
- '(dnscmd.exe 127.0.0.1 /EnumRecords ${Zone} ${MinionName} /type A | findstr ${IPAddress_v4}) || (dnscmd.exe 127.0.0.1 /RecordDelete ${Zone} ${MinionName} A /f & dnscmd.exe 127.0.0.1 /RecordAdd ${Zone} ${MinionName} /CreatePTR A ${IPAddress_v4})'
- cwd: 'C:\Windows\System32'
% endif
State file that fires the event:
#!mako|yaml
<%
OsFamily = salt['grains.get']('os_family', '')
OsMajorRelease = salt['grains.get']('osmajorrelease', {})
IpAddress_v4 = ''
Ipv4s = salt['grains.get']('ipv4', [])
HostName = salt['grains.get']('id', '')
AllowedNetworks = ('192.168.0.', '192.168.1.', '192.168.2.')
for Ip in Ipv4s:
if Ip.startswith(AllowedNetworks):
IpAddress_v4 = Ip
break
%>
update_dns_record:
% if OsFamily == 'RedHat':
event.send:
- name: 'domain/minion/update/dns'
- data:
ipv4: '${IpAddress_v4}'
% else:
test.succeed_without_changes:
- name: "OS_Not_Supported"
% endif
Reactor.conf file:
# vim: sts=2 ts=2 sw=2 et ai
reactor:
- 'domain/minion/update/dns':
- salt://reactor/update_dns_record.sls
@UtahDave Could you look at this? I can't see why it won't find the dnscmd.exe... especially if it's in C:\Windows\System32 which is part of the path... unless it's being redirected, which I don't think would happen from a normal command line, only in a 32bit PowerShell environment...
@haeac It looks like a 32 bit minion running on a 64 bit system. There is no 32 bit version of Server 2008R2 but the minion version report is showing a 32 bit build of Python:
Dependency Versions:
Python: 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)]
So I think the issue is down to filesystem redirection, and your command works at the command prompt as that will be a 64 bit process.
@morganwillcock thanks for the info. I'll update the python version to 64-bit and see if it works.
@morganwillcock thanks for the tip. Installing the 64-bit minion resolved the issue.