mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Merge pull request #25520 from rdutch/win_network_connect
Added connect function used in network module to the win_network
This commit is contained in:
commit
f3b04b14f6
@ -6,6 +6,11 @@ from __future__ import absolute_import
|
||||
|
||||
# Import salt libs
|
||||
import salt.utils
|
||||
import hashlib
|
||||
import datetime
|
||||
import socket
|
||||
import salt.utils.network
|
||||
import salt.utils.validate.net
|
||||
|
||||
try:
|
||||
import salt.utils.winapi
|
||||
@ -289,3 +294,95 @@ def ip_addrs6(interface=None, include_loopback=False):
|
||||
include_loopback=include_loopback)
|
||||
|
||||
ipaddrs6 = ip_addrs6
|
||||
|
||||
|
||||
def connect(host, port=None, **kwargs):
|
||||
'''
|
||||
Test connectivity to a host using a particular
|
||||
port from the minion.
|
||||
|
||||
.. versionadded:: Boron
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' network.connect archlinux.org 80
|
||||
|
||||
salt '*' network.connect archlinux.org 80 timeout=3
|
||||
|
||||
salt '*' network.connect archlinux.org 80 timeout=3 family=ipv4
|
||||
|
||||
salt '*' network.connect google-public-dns-a.google.com port=53 proto=udp timeout=3
|
||||
'''
|
||||
|
||||
ret = {'result': None,
|
||||
'comment': ''}
|
||||
|
||||
if not host:
|
||||
ret['result'] = False
|
||||
ret['comment'] = 'Required argument, host, is missing.'
|
||||
return ret
|
||||
|
||||
if not port:
|
||||
ret['result'] = False
|
||||
ret['comment'] = 'Required argument, port, is missing.'
|
||||
return ret
|
||||
|
||||
proto = kwargs.get('proto', 'tcp')
|
||||
timeout = kwargs.get('timeout', 5)
|
||||
family = kwargs.get('family', None)
|
||||
|
||||
if salt.utils.validate.net.ipv4_addr(host) or salt.utils.validate.net.ipv6_addr(host):
|
||||
address = host
|
||||
else:
|
||||
address = '{0}'.format(salt.utils.network.sanitize_host(host))
|
||||
|
||||
try:
|
||||
if proto == 'udp':
|
||||
__proto = socket.SOL_UDP
|
||||
else:
|
||||
__proto = socket.SOL_TCP
|
||||
proto = 'tcp'
|
||||
|
||||
if family:
|
||||
if family == 'ipv4':
|
||||
__family = socket.AF_INET
|
||||
elif family == 'ipv6':
|
||||
__family = socket.AF_INET6
|
||||
else:
|
||||
__family = 0
|
||||
else:
|
||||
__family = 0
|
||||
|
||||
(family,
|
||||
socktype,
|
||||
_proto,
|
||||
garbage,
|
||||
_address) = socket.getaddrinfo(address, port, __family, 0, __proto)[0]
|
||||
|
||||
skt = socket.socket(family, socktype, _proto)
|
||||
skt.settimeout(timeout)
|
||||
|
||||
if proto == 'udp':
|
||||
# Generate a random string of a
|
||||
# decent size to test UDP connection
|
||||
md5h = hashlib.md5()
|
||||
md5h.update(datetime.datetime.now().strftime('%s'))
|
||||
msg = md5h.hexdigest()
|
||||
skt.sendto(msg, _address)
|
||||
recv, svr = skt.recvfrom(255)
|
||||
skt.close()
|
||||
else:
|
||||
skt.connect(_address)
|
||||
skt.shutdown(2)
|
||||
except Exception as exc:
|
||||
ret['result'] = False
|
||||
ret['comment'] = 'Unable to connect to {0} ({1}) on {2} port {3}'\
|
||||
.format(host, _address[0], proto, port)
|
||||
return ret
|
||||
|
||||
ret['result'] = True
|
||||
ret['comment'] = 'Successfully connected to {0} ({1}) on {2} port {3}'\
|
||||
.format(host, _address[0], proto, port)
|
||||
return ret
|
||||
|
Loading…
Reference in New Issue
Block a user