Skip to content

Commit 4a4f702

Browse files
committed
enhance ip addr lookup for ssh access method
1 parent 678632e commit 4a4f702

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

saspy/sasiostdio.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,24 +240,38 @@ def __init__(self, session, **kwargs):
240240

241241
self._prompt = session._sb.sascfg._prompt
242242

243-
if localhost is not None:
244-
self.hostip = localhost
245-
else:
246-
self.hostip = socks.gethostname()
247-
try:
248-
x = subprocess.Popen(('nslookup', self.hostip), stdout=subprocess.PIPE)
249-
z = x.stdout.read()
250-
ip = z.rpartition(b'Address:')[2].strip().decode()
243+
if self.ssh:
244+
if localhost is not None:
245+
self.hostip = localhost
246+
else:
247+
self.hostip = socks.gethostname()
248+
print('No localhost, hostip=', str(self.hostip))
251249
try:
252-
socks.gethostbyaddr(ip)
253-
self.hostip = ip
250+
x = subprocess.Popen(('nslookup', self.hostip), stdout=subprocess.PIPE)
251+
z = x.stdout.read()
252+
ip = z.rpartition(b'Address:')[2].strip().decode()
253+
try:
254+
print('nslookup ip=', str(ip))
255+
socks.gethostbyaddr(ip)
256+
self.hostip = ip
257+
except:
258+
sock = socks.socket()
259+
sock.bind(('',0))
260+
sock.settimeout(1)
261+
try:
262+
sock.connect((self.host, 22))
263+
except:
264+
pass
265+
print('sock=', str(sock))
266+
ip = sock.getsockname()[0]
267+
self.hostip = ip
268+
print('self.hostip fom connect is ip=', str(ip))
269+
sock.close()
270+
x.stdout.close()
271+
x.terminate()
272+
x.wait(1)
254273
except:
255274
pass
256-
x.stdout.close()
257-
x.terminate()
258-
x.wait(1)
259-
except:
260-
pass
261275

262276
return
263277

0 commit comments

Comments
 (0)