socket.gethostbyaddr() on a linux system and local network - python

I have a django application using this function, i'm trying to get the computer name of the ip address accessing my application. I'm doing this by using django-ipware to get the client's ip address, this part is working fine. Then i'm using socket.gethostbyaddr() to get the client's computer name, this works fine on my windows development machine.
def get_comp_name(request):
client_ip = get_client_ip(request)
try:
comp_name = socket.gethostbyaddr(client_ip[0])[0]
except socket.herror:
comp_name = ''
When i tried to deploy to a centOS 7 machine, I get the following error when executing socket.gethostbyaddr() on local network ip addresses.
socket.herror: [Errno 1] Unknown host
I can ping local ip addresses without issue. Am I missing a configuration on my centOS 7 machine?

Your DNS server needs to have an entry for this to work. Check /etc/resolv.conf if the DNS server IP is correct, check if DNS server is reachable from the CentOS node and finally, check if the entry in the DNS server is correct.

Related

mysql connector error 1130: host not allowed to connect to this MySQL server

I just upgraded to Verizon Fios internet this morning. I have a python script that accesses a database on a MySQL (mariadb) server on a raspberrypi (both are on the same network). It worked with my old internet/IP address, however now I am getting:
mysql.connector.errors.DatabaseError: 1130: Host 'my_computer_name.myfiosgateway.com' is not allowed to connect to this MySQL server
Steps I took leading up to error:
Confirmed new IP address of computer running the script
Changed IP address in configuration file that the python script uses to new IP address
I created a new user in the mysql server with the new host IP address of the laptop running the script
Granted all privileges to the new user on the database on the server
Flushed privileges
Restarted the mysql server
I'm not sure why the error is returning the computer's name, and not the IP address, but I even did the above steps with the exact host name returned in the error and it's still not working. I also compared the GRANTS on the MySQL server from the old IP address to the new one, and they are the same.
I'm able to ping the raspberrypi from my laptop with no issues.
Am I forgetting something? Is there a setting I may have to change on my router? That's the only thing that has changed from before when the script would work.
dbConfig.py
dbConfig = {
'host': 'xxx.xxx.x.xxx',
'database': 'NHL',
'user': 'greg',
'password': 'xxxxxx',
}
Script that accesses database
import dbConfig as guiConfig
import csv
import time
# connect to server
conn = mysql.connect(**guiConfig.dbConfig)
The bind-address in my .cnf file is commented out.
Is it possible that the Laptop running the script has another IP address?
Since the TCP/IP connection to the database works, there must be something wrong with the created MariaDB user or it's privileges. You could set the following parameter in the my.cnf in order to skip the DNS resolving. This improves the connection performance and it seems to confuse you. You should then see the Source IP address of the connection.
[mysqld]
skip-name-resolve

Host command and ifconfig giving different ips

I am using server(server_name.corp.com) inside a corporate company. On the server i am running a flask server to listen on 0.0.0.0:5000.
servers are not exposed to outside world but accessible via vpns.
Now when i run host server_name.corp.com in the box i get some ip1(10.*.*.*)
When i run ifconfig in the box it gives me ip2(10.*.*.*).
Also if i run ping server_name.corp.com in same box i get ip2.
Also i can ssh into server with ip1 not ip2
I am able to access the flask server at ip1:5000 but not on ip2:5000.
I am not into networking so fully confused on why there are 2 different ips and why i can access ip1:5000 from browser not ip2:5000.
Also what is equivalent of host command in python ( how to get ip1 from python. I am using socktet.gethostbyname(server_name.corp.com) which gives me ip2)
As far as I can tell, you have some kind of routing configured that allows external connections to the server by hostname (or ip1), but it does not allow connection by ip2. And there is nothing unusual in this. Probably, the system administrator can advise why it is done just like this. Assuming that there are no assynchronous network routes, the following function can help to determine public ip of server:
import socket
def get_ip():
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(("8.8.8.8", 80))
local_address = sock.getsockname()
sock.close()
local_address = local_address[0]
except OSError:
local_address = socket.gethostbyname(socket.gethostname())
return local_address
Not quite clear about the network status by your statements, I can only tell that if you want to get ip1 by python, you could use standard lib subprocess, which usually be used to execute os command. (See subprocess.Popen)

Cannot console into new created Openstack instance

I installed Openstack Mitaka using Devstack on an Ubuntu virtual machine in Virtualbox. After installation, things seemed to be alright. Then I start a new Cirros instance, and it succeeded. However, when I tried to console this instance using ip nets exec xxxxxx ssh cirros#172.24.4.3 it returned ssh: connect to host 172.24.4.3 port 22: no route to host. Result for ip nets exec xxxxxx ping 172.24.4.3 is Destination Host Unreachable.
I checked the ip netsh, the result was like:
qrouter-xxxxxxx
qdhcp-xxxxxx
If I use ip nets exec qrouter-xxxxxx ip addr show , the result includes:
127.0.0.1, 10.0.0.1, 172.24.4.2, and another item with no ip address.
If I user ip nets exec qdhcp-xxxxxx ip addr show, the result includes:
127.0.0.1, 10.0.0.2
The instance is connected to public network as shown in Openstack Dashboard. And if I try to connect to the console in WebUI - Dashboard/Compute/Instances/xxxx/Console, there was an error shown on the page, saying:
Error response. Error code 404.Message: File not found.
Error code explanation: 404 = Nothing matches the given URI.
So how can I console into the instances created by Openstack? Is it something to do with the network configuration?
Thank you for your answers in advance.

Python 2.7 [Errno 113] No route to host

I have 2 computers on the same LAN. The first PC has an ip address 192.168.178.30, the other PC has an ip address 192.168.178.26.
Ping, traceroute, telnet, ssh, everything works between the two PCs. Both PCs run the same OS - CentOS 7 and both PCs have the same python version 2.7.5 (checked with the python -V command).
I copied simple python code from a computer networking book.
client.py
from socket import *
serverName = '192.168.178.30'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = raw_input('Input lowercase sentence: ')
clientSocket.send(sentence)
modifiedSentence = clientSocket.recv(1024)
print 'From Server:', modifiedSentence
clientSocket.close()
server.py
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('192.168.178.30',serverPort))
serverSocket.listen(5)
print 'The server is ready to receive'
while 1:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024)
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence)
connectionSocket.close()
The code works when it is ran on the same PC (where the server is listening on localhost).
When I run the client code on one PC and the server code on the other PC I get this error on the client side.
Traceback (most recent call last):
File "client.py", line 5, in <module>
clientSocket.connect((serverName,serverPort))
File "/usr/lib64/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 113] No route to host
Can someone help?
Check the firewall (on the server).
I stopped the firewall like Messa suggested and now it works.
service firewalld stop
I still don't understand what the problem was. I even tried using different distributions. Do all distributions have strict firewalls or something. For example Ubuntu to Ubuntu, Ubuntu to CentOS, CentOS to Ubuntu I still had the same problem (error).
~]#supervisord
Error: No config file found at default paths (/usr/etc/supervisord.conf, /usr/supervisord.conf, supervisord.conf, etc/supervisord.conf, /etc/supervisord.conf); use the -c option to specify a config file at a different path
For help, use /usr/bin/supervisord -h
You should use
ln -s /etc/supervisor/supervisord.conf /usr/etc/supervisord.conf
None of this stuff worked for me. I just connected both the devices to the same WiFi network and my program worked!
You can also get this same error ([Errno 113] No route to host) if you are trying to connect 2 devices on the same network. the error can be fixed by double checking to make sure both devices are connected to the mqtt_client or whatever you are using. as soon as I connected the device I was trying to talk to everything worked as to be expected.I would also check to make sure the right IP_Address is being passed
We had this problem. I am putting our findings here in case anyone else stumbles across this question like I did.
Our configuration:
Host A: IP address 192.168.0.1, netmask 255.255.255.0
Host B: IP address 192.168.1.1, netmask 255.255.254.0
Neither host has a default gateway.
We are connecting from Host B to Host A. (That is not a typo.) The connect call succeeds, but when we try to send data, we get errno 113 aka. EHOSTUNREACH aka. "No route to host".
The fix, of course, was to change the subnet on Host A to match Host B.
We were surprised to see this error on a connection within the same subnet / same LAN. And we were surprised that connect succeeded followed by send failing. And we were surprised to see this error on Host B even though the network configuration on Host B itself was fine.
Somehow, the incorrect subnet on Host A caused this error on Host B...
...and just like that, today I learned about ICMP "destination unreachable" messages.

How to Transfer Files from Client to Server Computer by using python script?

I am writing a python script to copy python(say ABC.py) files from one directory to another
directory with the same folder name(say ABC) as script name excluding .py.
In the local system it works fine and copying the files from one directory to others by
creating the same name folder.
But actually I want copy these files from my local system (windows XP) to the remote
system(Linux) located in other country on which I execute my script. But I am getting
the error as "Destination Path not found" means I am not able to connect to remote
that's why.
I use SSH Secure client.
I use an IP Address and Port number to connect to the remote server.
Then it asks for user id and password.
But I am not able to connect to the remote server by my python script.
Can Any one help me out how can I do this??
paramiko provides a SFTPClient that can be used to do this.
import paramiko
source = r'C:\Somedir\somefile.txt'
dest = r'/home/user/file.txt'
hostname = 'linux.server.com'
port = 22 # default port for SSH
username = 'user'
password = 'secret'
try:
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(source, dest)
finally:
t.close()
I used the same script, but my host failed to respond. My host is in different network.
WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Categories