cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2960
Views
0
Helpful
4
Replies

Needing nslookup command from NX-OS - trying Python but socket not working

Stuff
Level 1
Level 1

What I am trying to do is a reverse lookup to find the name from IP.  The switches I am using don't have shell access so trying Python and getting failures.

 

socket.gethostbyaddr() and socket.gethostbyname() both work for the device's IP and hostname but not for any other.  I can ping by hostname from the CLI so know DNS is working for the device.  Is there something I need to do to enable the external lookup?   Error I get is socket.gaierror: [Errno -2] host nor service provided, or not known

4 Replies 4

omz
VIP Alumni
VIP Alumni

Please add more info .. script snippet may be helpful and what works and what doesn't

What happens when you do - socket.gethostbyname('www.example.domain.com')?

Here is an example. The switch can resolve itself and www.cisco.com from the CLI but only can resolve itself in Python

 

5596-1# ping 5596-1 vrf management
PING 5596-1 (1.1.1.1): 56 data bytes
64 bytes from 1.1.1.1: icmp_seq=0 ttl=255 time=0.323 ms
64 bytes from 1.1.1.1: icmp_seq=1 ttl=255 time=0.216 ms
64 bytes from 1.1.1.1: icmp_seq=2 ttl=255 time=0.204 ms
64 bytes from 1.1.1.1: icmp_seq=3 ttl=255 time=0.205 ms
64 bytes from 1.1.1.1: icmp_seq=4 ttl=255 time=0.203 ms
 
--- 5596-1 ping statistics ---
5 packets transmitted, 5 packets received, 0.00% packet loss
round-trip min/avg/max = 0.203/0.23/0.323 ms
5596-1# ping www.cisco.com
PING e2867.dsca.akamaiedge.net (104.119.17.8): 56 data bytes
Request 0 timed out
Request 1 timed out
Request 2 timed out
Request 3 timed out
Request 4 timed out
 
--- e2867.dsca.akamaiedge.net ping statistics ---
5 packets transmitted, 0 packets received, 100.00% packet loss
5596-1#
5596-1# python
Copyright (c) 2001-2012 Python Software Foundation; All Rights Reserved
 
5596-1# >>> socket.gethostbyname('5596-1')
'1.1.1.1'
5596-1# >>> socket.gethostbyaddr('1.1.1.1')
('5596-1', [], ['1.1.1.1'])
5596-1# >>> socket.gethostbyaddr('104.119.17.8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.herror: [Errno 2] Host name lookup failure
5596-1# >>> socket.gethostbyname('www.cisco.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.gaierror: [Errno -3] temporary failure in name resolution
5596-1# >>> exit
5596-1#

Hi, 

 

Sorry, mis-clicked on "me too" - please ignore that. 

Could you check the permissions of the user you are connected at? You can do it this way by running:

 

>>> import os
>>> os.system('whoami')

 

Kind Regards,
Ivan

No good.

 

5596-1# >>> import os

5596-1# >>> os.system('whoami')

system(whoami): rejected!

-1

 

Also tried on a 7K (running 6.2(24)) and same thing.