summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--get_first_open_port.py/description1
-rwxr-xr-xget_first_open_port.py/get_first_open_port.py47
-rw-r--r--get_first_open_port.py/usage23
3 files changed, 71 insertions, 0 deletions
diff --git a/get_first_open_port.py/description b/get_first_open_port.py/description
new file mode 100644
index 0000000..4371b6e
--- /dev/null
+++ b/get_first_open_port.py/description
@@ -0,0 +1 @@
+ansible tasks to find first available remote port
diff --git a/get_first_open_port.py/get_first_open_port.py b/get_first_open_port.py/get_first_open_port.py
new file mode 100755
index 0000000..5188c51
--- /dev/null
+++ b/get_first_open_port.py/get_first_open_port.py
@@ -0,0 +1,47 @@
+#!/usr/bin/python2
+# Filename: get_first_open_port.py
+# Location: /etc/ansible/roles/install_sccm/files/
+# Author: bgstack15
+# Startdate: 2018-10-02 10:13
+# Title: Script that Gets the First Open Port
+# Purpose: Return to standard output the first valid host and port to use as a http proxy
+# Project: projects derived from ansible role certreq
+# History:
+# Usage:
+# in ansible
+# Reference:
+# https://stackoverflow.com/questions/19196105/python-how-to-check-if-a-network-port-is-open-on-linux
+# string split https://stackoverflow.com/questions/6670290/split-string-into-different-variables-instead-of-array-in-python
+# Improve:
+# Documentation:
+
+import socket, sys
+from contextlib import closing
+
+GFOP_VERSION="2018-10-02a"
+
+def check_socket(host, port):
+ with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
+ sock.settimeout(2)
+ if sock.connect_ex((host,port)) == 0:
+ print host + ":" + str(port)
+ return True
+ return False
+
+
+x = 0
+for myarg in sys.argv:
+
+ # show version
+ if myarg == "--version" or myarg == "-V":
+ print(sys.argv[0]+" "+GFOP_VERSION)
+ sys.exit(0)
+
+ x = x + 1
+ # skip the script $0 name itself
+ if x > 1:
+ # split on the colon
+ host, port = myarg.split(":",2)
+ # short-circuit upon first successful one
+ if check_socket(host,int(port)):
+ sys.exit(0)
diff --git a/get_first_open_port.py/usage b/get_first_open_port.py/usage
new file mode 100644
index 0000000..61b537c
--- /dev/null
+++ b/get_first_open_port.py/usage
@@ -0,0 +1,23 @@
+vars:
+ http_proxies:
+ - 192.168.1.5:3128
+ - proxy5.internal.example.com:3128
+
+tasks:
+
+ - name: learn which proxy to use
+ script: get_first_open_port.py {{ http_proxies | join( " " ) }}
+ changed_when: false
+ register: open_ports
+
+ - set_fact:
+ http_proxy: "{{ open_ports.stdout_lines[0] }}"
+ when:
+ - 'open_ports.stdout | length > 0'
+ failed_when:
+ - 'open_ports.stdout | length = 0'
+
+ - name: use http_proxy environment variable
+ script: script_needing_internet.sh -i {{ inputvar }}
+ environment:
+ http_proxy: "{{ http_proxy | default(omit) }}"
bgstack15