From 4a4f702296f9b342adece7be7ceb0bc60b3e41f0 Mon Sep 17 00:00:00 2001 From: Tom Weber Date: Fri, 20 Dec 2024 10:59:10 -0500 Subject: [PATCH] enhance ip addr lookup for ssh access method --- saspy/sasiostdio.py | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/saspy/sasiostdio.py b/saspy/sasiostdio.py index a3d6e309..c233743f 100755 --- a/saspy/sasiostdio.py +++ b/saspy/sasiostdio.py @@ -240,24 +240,38 @@ def __init__(self, session, **kwargs): self._prompt = session._sb.sascfg._prompt - if localhost is not None: - self.hostip = localhost - else: - self.hostip = socks.gethostname() - try: - x = subprocess.Popen(('nslookup', self.hostip), stdout=subprocess.PIPE) - z = x.stdout.read() - ip = z.rpartition(b'Address:')[2].strip().decode() + if self.ssh: + if localhost is not None: + self.hostip = localhost + else: + self.hostip = socks.gethostname() + print('No localhost, hostip=', str(self.hostip)) try: - socks.gethostbyaddr(ip) - self.hostip = ip + x = subprocess.Popen(('nslookup', self.hostip), stdout=subprocess.PIPE) + z = x.stdout.read() + ip = z.rpartition(b'Address:')[2].strip().decode() + try: + print('nslookup ip=', str(ip)) + socks.gethostbyaddr(ip) + self.hostip = ip + except: + sock = socks.socket() + sock.bind(('',0)) + sock.settimeout(1) + try: + sock.connect((self.host, 22)) + except: + pass + print('sock=', str(sock)) + ip = sock.getsockname()[0] + self.hostip = ip + print('self.hostip fom connect is ip=', str(ip)) + sock.close() + x.stdout.close() + x.terminate() + x.wait(1) except: pass - x.stdout.close() - x.terminate() - x.wait(1) - except: - pass return