Commit 11845745 authored by Tristan Cavelier's avatar Tristan Cavelier

Release 0.41.0: add is-local-tcp-port-opened script

parent c50a0c1e
......@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
import glob
import os
version = '0.40.4'
version = '0.41.0'
name = 'slapos.toolbox'
long_description = open("README.txt").read() + "\n" + \
open("CHANGES.txt").read() + "\n"
......@@ -53,6 +53,7 @@ setup(name=name,
entry_points={
'console_scripts': [
'agent = slapos.agent.agent:main [agent]',
'is-local-tcp-port-opened = slapos.promise.islocaltcpportopened:main',
'clouddestroy = slapos.cloudmgr.destroy:main',
'cloudgetprivatekey = slapos.cloudmgr.getprivatekey:main',
'cloudgetpubliciplist = slapos.cloudmgr.getpubliciplist:main',
......
#!/usr/bin/env python
"""
Check if a specific ip address and port is listening.
No connection establishment is done during the check.
Uses:
- /proc/net/tcp
- /proc/net/tcp6
"""
import sys
import socket
import struct
assert struct.calcsize('I') == 4
def is_local_tcp_port_opened(ip_address, port):
family = socket.getaddrinfo(ip_address, 0)[0][0]
conf = {
socket.AF_INET6: (4, "/proc/net/tcp6"),
socket.AF_INET: (1, "/proc/net/tcp"),
}
int_count = conf[family][0]
tcp_path = conf[family][1]
ip_addr_hex = ('%08X' * int_count) % struct.unpack('I' * int_count, socket.inet_pton(family, ip_address))
full_addr_hex = ip_addr_hex + ":%04X" % port
return any(full_addr_hex == line.split()[1] for line in open(tcp_path).readlines())
def main():
if is_local_tcp_port_opened(sys.argv[1], int(sys.argv[2])):
return 0
return 1
if __name__ == "__main__":
sys.exit(main())
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment