getting hostname of remote computers on the local network not setup in /etc/hosts - python-3.x

I have a new learning, I was trying to get hostname using python's socket.
so from my macbook I ran the below code:
socket.gethostbyaddr("192.168.1.111")
and I get the ('rock64', [], ['192.168.1.111']) then I tried IP address of a computer that is not on the network anymore but used to be:
socket.gethostbyaddr("192.168.1.189")
and it returned: ('mint', [], ['192.168.1.189']) then I realised its coming from the /etc/hosts file.
now in that host file I also have this entry:
/etc/hosts
172.217.25.3 google.com.hk
but if I try to get host from ip of wan address i get different results than expected!
socket.gethostbyaddr("172.217.25.3")
that returns ('hkg07s24-in-f3.1e100.net', ['3.25.217.172.in-addr.arpa'], ['172.217.25.3'])
so I am not wondering where in the later case of WAN ip address I am getting the hostname and why in case of local computer IP's I am getting hostname from the configured /etc/hosts file ?
How can we get hostname of host computers on the local network without socket.gethostbyaddr having to look into /etc/hosts file or by other means ?

This is opinion based answer to the question "how to build registry of network devices on your local network?"
The best way to build registry of devices on your local network is to setup ntopng on your gateway. It uses DPI (Deep Packet Inspection) Technics to collect information about hosts.
NTOPNG has nice user interface and displays host names (when possible).
You can assign aliases for specific hosts which do not leak host names via any protocol.
For some reasons ntopng developers did not include alias into json response for request http://YOUR-SERVER:3000/lua/host_get_json.lua?ifid=2&host=IP-OF-DEVICE .
You can add it manually by adding lines require "mac_utils" and hj["alias"]=getDeviceName(hj["mac_address"]) into file /usr/share/ntopng/scripts/lua/host_get_json.lua
You can use REST API to interrogate ntopng and use provided information for building any script you need.

Related

Bash: pinging servers and writing the IP to a file

I currently have to find the IP-Adresses of a lot of servers.
I have written a bash script on linux that creates all the server names.
When I write it like this, I get all servernames, even if there no longer have an IP.
host $f$n$d >> IPs.txt
Is there a way to only write the servers into the file, that still have an IP, together with the servername.
Kind regards Elias
I think the local DNS resolver caches the IP addresses, so you will get the corresponding IPs even in the scenario you described.
I suggest you to start the script with a full local DNS flush, depending on your distribution and DNS client: Here are son techinques to flush it.

Setting up NFS permissions on Synology for Linux (KDE Neon)

I've followed the instructions on the this Synology article to set up NFS access between my Synology NAS and my Linux (KDE Neon) machine.
If I set the hostname to my local IP it works fine... but the trouble is my local IP changes so I'd need to keep updating the NAS permission settings.
If I use my hostname, let's call it my-linux-machine I get the following error:
mount.nfs: access denied by server while mounting 192.168.1.11:/volume1/Video
Synology support have advised that:
I've checked the log, and found you've tried to set the client Host name: my-linux-machine as you've mentioned.
For your information, this Host name should be a validate Domain name which can be resolved by your NAS's DNS server.
Thus, you'll need to apply a domain name for your Linux (KDE Neon) machine, and a server name is not able to use directly here.
To verify the domain name, you can check it by "Ping" command to your Linux server from your NAS server.
You may specify a host in three ways:
Single host
Wildcards: *, *.synology.com
Network segment: 203.74.205.32/255.255.255.0, 203.74.205.32/24
Here's where I'm stuck.
Could someone help me understand which of those three options are best suited to my needs?
Single host: sounds like the single IP method, or a FQDN which I'm unsure as to what my 'domain' would be if none of it needs to talk to the world wide web.
Wilcards: also mentions domain names... again, my setup doesn't need to leave my home network.
Network segment: could maybe work? But I'm unsure how to figure what my segment would be.
Thanks in advance!
In principle, you should be able configure the IP using a mask (based on the answer on the Synology forum):
192.168.1.0/255.255.255.0
Hope this helps!

How can I restrict FTP and SMB access to computers on the internal network

I have Installed samba and tried adding this line config file for samba
Listen=yes
Listen_address= 127.0.0.1
I have also put this in vsftpd.cnf
It doesn't work
The IP addresses 127.x.x.x are always restricted to the local host only, so when other computers try that they connect only to themselves. Assuming you have Linux on a Raspberry Pi, see what other IP addresses it has that connect with the outside world like this:
/sbin/ifconfig
and then use one of those addresses instead. Also try to ping the address from your other computers - if that doesn't work then ftp has little chance of working either.
The vsftpd.conf documentation suggests that the default is to listen on all local addresses. You could try that by commenting out the listen_address line with a # at the start of the line, and then restart the vsftpd server process.
Do not confuse samba and vsftp. They are two separate server processes that use different protocols, port numbers and configuration files.
Beware of case sensitive options, the wrong case will not work. You wrote Listen but probably meant listen ;-)

Troubles accessing the VirtualHost on a local net

Hi I have a Huawei ADSL modem with a dynamic IP. I set up a dynamic dns with freedns and I can easily access my Virtual host on apache from outside my private network, from internet.
So trying to access it from a local network I get a modems login page.
I have the rule in my modems NAT to connect port 80 to my server but it seems it doesnt work from internal net.
My server hosting the virtual host is 192.168.0.1 , the modem has the 192.168.0.254 address.
I tried to put my external domain name into the /etc/hosts file, but if I do that then asterisk server gets confused.
What could be the cause of the problem ? And what would be the best solution to this problem ?
Should I set up a caching DNS on the private side of the net ?
Hugger is half right... Its because your router is blocking loopbacks. The easiest way around this is to edit the hosts file (See the Wiki page) on the computer that is behind the network to see the local IP of the computer as the virtual host your going for. For example if your server is on 192.168.0.1 and the virtual hostname is www.imrad.com then insert
192.168.0.1 www.imrad.com
into your hosts file. If your computer leaves the local network (like a laptop you take to work) and joins a network not behind your router you need to comment out the line in the hosts file.
well actually to access that you must find the local ip address of the server by going into command line/prompt and typing ipconfig /all then you will find ipv4 address. Take note of that.
Go to another computer and your that ip and it will show.
The reason that was happening is because the router/modem knows you are in its network so it will think you ar trying to go top the login page of it. That happens to me too.

Same server, same program but started once using one network card and after with another

I have a Linux server with multiple ips (so, multiple eth0, eth0:0, eth0:1 etc).
The script I'm trying to start is a php CLI script which is downloading stuff from an another server API, and I would like to change the IP based on different parameters. Once the script is started, I don't need anymore to change the ip OF THAT SPECIFIC script until his end.
Do you have any clue if it is possible to achieve it?
My other solution was to install Xen or OpenVZ and create N different VPS per each IP, but as you can see is definitely a PITA :-)
You don't specify how you connect to the other server, but with sockets you can try socket_bind.
EDIT:
With curl you can try curl_setopt.
CURLOPT_INTERFACE The name of the outgoing network interface to use. This can be an interface name, an IP address or a host name.
I know how to do it in C - you use bind() on your socket before you call connect(), and you bind to the IP address assigned to the desired interface, passing 0 for port. I don't know how to do it in PHP.

Resources