I want to get the fqdn from the hostname.
I am am executiong this command on a linux server:
python -c "import sys, socket; sys.stdout.write(socket.gethostbyaddr('')[0])"
THis command works well and returns the fqdn from the hostname. But for some other servers, it returns this error:
Traceback (most recent call last):
File "", line 1, in
socket.herror: [Errno 1] Unknown host
When i do "host " linux command, I get the fqdn of the failed host names with python command.
Anyone have a solution for that plz?
Difficulty using Python's socket.gethostbyaddr() covers this nicely.
The gist is that you must verify there's a PTR record. If not, you'll have to handle it via a try, except clause.
Specifically look at the part comparing the a valid and non-valid PTR records.
Related
I use mcpi: https://github.com/AdventuresInMinecraft/AdventuresInMinecraft-Linux
Starting the local server.
After, run program:
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Hello Minecraft World")
I am facing the below error:
Traceback (most recent call last):
File "/home/home/AdventuresInMinecraft/MyAdventures/HelloMinecraftWorld.py", line 2, in mc = minecraft.Minecraft.create()
File "/home/home/.local/lib/python3.6/site-packages/mcpi/minecraft.py", line 376, in create return Minecraft(Connection(address, port))
File "/home/home/.local/lib/python3.6/site-packages/mcpi/connection.py", line 17, in init self.socket.connect((address, port))
ConnectionRefusedError: [Errno 111] Connection refused
A ConnectionRefusedError means that the address + port combination was unable to be secured for this particular Minecraft server and thus raised an exception. This could be because some other application is already using the port of interest, the port is unavailable because of the OS, or a handful of other networking configuration mishaps.
But perhaps a better series of questions to ask yourself is:
What is the default address and port that minecraft.Minecraft.create() will attempt to launch / listen at?
Do I have access to that server (address + port)?
If I do have access, are there any security issues (AKA Firewall)?
This post has already addressed the root issue of your question, and I hope it gives you a good start at understanding the foundation of your problem.
Notice how their question mentions s.connect((host,port)) and your stack trace has self.socket.connect((address, port)) Looks like the same thing to me!
Some more reading:
- localhost
- check if port is in use
I encountered the same issue. I looked into the code of mcpi and found that the default port is 4711. However, a Minecraft Server's default port is 25565. All you need to do is add 2 parameters on the create() function. Code(Python):
mc = minecraft.Minecraft.create(address="127.0.0.1", port=25565)
btw change "address" in the code to the host of the server (only if you modified the "server.properties" file).
Also, ConnectionRefusedError doesn't mean that it's not secured, I believe it means that either the server is not online, it doesn't exist, or the server refused it for some reason.
EDIT:
Oops sorry I just found out that mcpi actually connects to the RaspberryJam plugin which is hosted on another IP and port. The plugin runs on port 4711. So mcpi has the right port.
So check if you have the RaspberryJam plugin installed. If not, download it from
https://www.spigotmc.org/resources/raspberryjuice.22724/
And put the .jar file inside the plugins folder in your server directory.
I need to automate transferring of a file from one server to a client's SFTP server. I've done this hundreds of time using Python's pysftp package. However, on this occasion, there's a HostkeyAlgorithm that I need to set. I've read through Paramiko's doc since pysftp seems lacking of this option entirely and is built on Paramiko. But I honestly don't know what to do (I don't get to play with networking things often). I've been sending manually through bash with the following:
sftp -o HostkeyAlgorithms=+ssh-dss user#host.com
I've tried the following in Python to no success:
import paramiko
_host='somehostname.com'
_user='thisguy'
_pass='you_get_the_idea'
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
client.connect(_host, 22, _user, _pass)
This returns:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 424, in connect
passphrase,
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 714, in _auth
raise saved_exception
paramiko.ssh_exception.AuthenticationException: Authentication failed.
So I guess the question is where/how do I add the -o HostkeyAlgorithms=+ssh-dss when setting up my Paramiko connection?
Paramiko will use host key algorithm matching a host key that you configure for your session.
You do not specify any host key, instead you blindly accept all host keys (MissingHostKeyPolicy), what is a security flaw. You lose a protection against MITM attacks.
For a correct (and secure) approach, see:
Python - pysftp / paramiko - Verify host key using its fingerprint
Verify host key with pysftp
Though, I actually do not understand, why do you want to set "HostkeyAlgorithms", if you do not even verify the host key due to MissingHostKeyPolicy? – The "Authentication failed" error is for sure not related to host key.
I have been trying to get the ipaddress of the person who logged into the machine using the below code but I get a error.
>>> import socket
>>> socket.gethostbyname_ex(socket.gethostname())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
socket.gaierror: [Errno -2] Name or service not known
The same code works in other linux box.
Not sure I fix it.
Error has occurred just because of not setting up hostname properly. Set the hostname at three different places, which are in -
/etc/hostname
/etc/hosts
run command $ hostname
then logout and login again. You are done.
Check what is being returned by socket.gethostname() and see if you can ping it. Basically this is a lookup failure. Check your /etc/hosts to see if it is listed. I know it seems strange, but I think if the hostname being returned does not have an entry, you'll get a name service failure which is what that is.
If you are working with IPv6 or with servers with multiple network interfaces, this command will not work correctly.
Instead, you can use this command that tries to connect to the Google DNS server at 8.8.8.8 at port 53, and return your ip:
import socket
print([(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])
I have a problem with a python server I am creating. It works on my home machine, but when I've tried to run it on a different machine it does not work. When compiled using pyinstaller, the window immideatly closes, and when ran as a raw python file (python 2.7.10 is installed on both my home machine and the machine it is not working on) it throws the error:
Traceback (most recent call last):
File "fileModifyServer.py", line 136, in <module>
startServer()
File "fileModifyServer.py", line 11, in startServer
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
File "N:\Python27\lib\socket.py", line 191, in __init__
_sock = _realsocket(family, type, proto)
socket.error: [Errno 10022] An invalid argument was supplied
My code it is referencing to is as follows:
import socket
def startServer():
global serversocket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((socket.gethostname(), 8010))
serversocket.listen(5)
print "Server started"
The traceback you have is strange. It indicates a line when attempting to instantiate the socket, which would indicate a problem with your python installation or network stack. It also indicates that error occurred on line 11, but in your code the line in question appears on line 6. I'm not sure how it happened here, but I know this can happen if you edit files while your program is running and then it crashes. The traceback simply prints out the line number from the file in question that caused the error, and the file source doesn't appear to be read until the error occurs; Therefore the traceback will reflect the line in the modified file, which isn't the line that was present when the program was compiled, and thus is not the line that actually caused the problem.
Without looking at the traceback, I do see an error with your code. You are attempting to bind your server to an invalid interface. The hostname returned by socket.gethostname is not an interface. From the documentation:
If you want to know the current machine’s IP address, you may want to use gethostbyname(gethostname()).
This operation assumes that there is a valid address-to-host mapping for the host, and the assumption does not always hold.
# for example
local_ip_address = socket.gethostbyname(socket.gethostname())
Which will return a string representation of your local ip address. Unfortunately, that would still throw an error, as it is not an interface that you can bind to.
Some interfaces that you can bind to include "0.0.0.0", which means all available interfaces, and "localhost", which means "local" connections only, so no external network traffic allowed.
i was writing a small IRC bot when much to my dismay, i got an error that i cannot seem to understand or fix. the code i used worked before but now windows seems to not be happy with it.
Error:
socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
This is the quick code i knocked up:
import socket
s = socket.socket().connect(("irc.cryto.net", 6667))
s.send("NICK kNij\r\n")
s.send("USER kNij 0 0 kNij :derp :3\r\n")
inputfile = s.makefile()
while 1:
line = inputfile.readline()
print line
Edit: it seems to be an all over problem with some sockets
That can never work. connect returns None (at least on Windows 7 with Python 2.7.2).
Try:
import socket
s = socket.socket()
s.connect(("Lidingo.SE.EU.Undernet.org", 6667))
s.send("NICK kNij\r\n")
s.send("USER kNij 0 0 kNij :derp :3\r\n")
inputfile = s.makefile()
while 1:
line = inputfile.readline()
print line,
(I changed the server to make sure the code really works)
Now, why you get that error, and not, like me:
Traceback (most recent call last):
File "D:\workspaces\generic\SO_Python\9337618.py", line 4, in <module>
s.send("NICK kNij\r\n")
AttributeError: 'NoneType' object has no attribute 'send'
is a mistery...
maybe the port 8000 is not accessible.
try change the port number to 8888 by using python manage.py runserver 8888 command.
it worked for me