I'm writing a code for packet sniffing in python and in windows 10. According to my searching, I saw that most of the developers said that Linux is better for packet sniffing but I can't use Linux right now, So how could i fix this error?
I tried using print(ctypes.windll.shell32.IsUserAnAdmin()) and it gave 0 value.
This is my code:
def main():
print(ctypes.windll.shell32.IsUserAnAdmin())
conn = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_IP)
while True:
raw_data, addr = conn.recvfrom(65535)
dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data)
print("\nEthernet Frame:")
print("Destination: {}, Source: {}, Protocol: {}".format(dest_mac,src_mac,eth_proto))
And I've got this error:
OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions
I know it's something about permission but how can i do that? Thank you in advance :)
Related
When I run this bit of code:
import socket
domainName = 'test.domain.io'
old_ip = socket.gethostbyname(domainName)
print(old_ip)
I get this error in PyCharm:
"File "C:/Users/UserName/PycharmProjects/AddyGet/ThingstoTry.py", line 5, in
old_ip = socket.gethostbyname(domainName)
socket.gaierror: [Errno 11001] getaddrinfo failed"
When I point the debugger at line 5, I see the following errors:
"ret.metadata= {NameError}name 'ret' is not defined"
"route53= {NameError}name 'route53' is not defined"
"zone= {NameError}name 'zone' is not defined"
"zone= {NameError}name 'zone' is not defined"
"route53.connection= {NameError}name 'route53' is not defined"
The domain is hosting on route53, but I'm not importing route53 because I didn't think I would need to just to retrieve an IP address. So, do I need to be importing route53 and working with route53 methods just to get this IP address? Any insights would be greatly appreciated.
Also worth mentioning, because it's a "socket.gaierror", I tried the solutions here:
"getaddrinfo failed", what does that mean?
I am not behind a firewall (I disabled Window's firewall). There are no proxy environmental variables by default on my system, and putting myself behind a proxy also didn't help.
On my computer, running the code example, I get output 23.221.222.250.
It might not be a Python issue, but more an OS issue. What happens if you open a Windows cmd prompt and type:
ping test.domain.io
Here's my output (repetition and stats edited out):
Pinging test.domain.io [23.221.222.250] with 32 bytes of data:
Reply from 23.221.222.250: bytes=32 time=69ms TTL=47
...
This might be a something simple here, but I'm still learning python.
Basically I'm trying to pull an IP address from a hostname, which works fine, but if the host does not resolve it errors. I have it now so that once it resolves the IP address it populates it to a text box, so what i'm trying to do here is if it fails to resolve... To put a message in that text box saying no host found or whatever. I get an error: "socket.gaierror: [Errno 11004] getaddrinfo failed" when it does not resolve.
This is the code i have:
def findip():
host = hname.get() # Pulls host from text box1
ip = gethostbyname(host)
ipaddress.set(ip) #exports to text box2
return
So what i don't know is the If command needed for the failure (if that makes any sense) it would be something like:
if "gethostbyname fails"
ipaddress.set("Host does not resolve")
else
ipaddress.set(ip)
You have to try and catch the exception, this way:
def findip():
host = hname.get()
try:
ip = gethostbyname(host)
except socket.gaierror:
ip = "Host does not resolve"
ipaddress.set(ip)
Just make sure you have the socket module imported or it won't work, if you have no need for the socket module you can import the the exception only, so you need to do either of these:
import socket
import socket.gaierror
I have this simple minimal 'working' example below that opens a connection to google every two seconds. When I run this script when I have a working internet connection, I get the Success message, and when I then disconnect, I get the Fail message and when I reconnect again I get the Success again. So far, so good.
However, when I start the script when the internet is disconnected, I get the Fail messages, and when I connect later, I never get the Success message. I keep getting the error:
urlopen error [Errno -2] Name or service not known
What is going on?
import urllib2, time
while True:
try:
print('Trying')
response = urllib2.urlopen('http://www.google.com')
print('Success')
time.sleep(2)
except Exception, e:
print('Fail ' + str(e))
time.sleep(2)
This happens because the DNS name "www.google.com" cannot be resolved. If there is no internet connection the DNS server is probably not reachable to resolve this entry.
It seems I misread your question the first time. The behaviour you describe is, on Linux, a peculiarity of glibc. It only reads "/etc/resolv.conf" once, when loading. glibc can be forced to re-read "/etc/resolv.conf" via the res_init() function.
One solution would be to wrap the res_init() function and call it before calling getaddrinfo() (which is indirectly used by urllib2.urlopen().
You might try the following (still assuming you're using Linux):
import ctypes
libc = ctypes.cdll.LoadLibrary('libc.so.6')
res_init = libc.__res_init
# ...
res_init()
response = urllib2.urlopen('http://www.google.com')
This might of course be optimized by waiting until "/etc/resolv.conf" is modified before calling res_init().
Another solution would be to install e.g. nscd (name service cache daemon).
For me, it was a proxy problem.
Running the following before import urllib.request helped
import os
os.environ['http_proxy']=''
response = urllib.request.urlopen('http://www.google.com')
I'm posting this because I tried searching for the answer myself and I was not able to find a solution. I was eventually able to figure out a way to get this to work & I hope this helps someone else in the future.
Scenario:
In Windows XP, I'm using Python with Pandas & Quandl to get data for a US Equity security using the following line of code:
bars = Quandl.get("GOOG/NYSE_SPY", collapse="daily")
Unfortunately, I was getting the following error:
urllib2.URLError: <urlopen error [Errno 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>
#user3150079: kindly Ctrl+X / Ctrl+V your solution as an [Answer]. Such MOV is perfectly within StackOverflow
Solution:
I recognized that this was an issue with trying to contact a server without properly targeting my network's proxy server. Since I was not able to set the system variable for HTTP_PROXY, I added the following line which corrected the issue:
import os
os.environ['HTTP_PROXY']="10.11.123.456:8080"
Thanks - I'm interested to hear about any improvements to this solution or other suggestions.
You can set your user environment variable HTTP_PROXY if you can't or won't set the system environment variable:
set HTTP_PROXY "10.11.123.456:8080"
python yourscript.py
and to permanently set it (using setx from Windows XP Service Pack 2 Support Tools):
setx HTTP_PROXY "10.11.123.456:8080"
python yourscript.py
Other ways to get this environment variable set include: registry entries, putting os.environ["HTTP_PROXY"] = ..." insitecustomize.py`.
More control using requests without using the Quandl Package:
import requests
def main():
proxies = {'http': 'http://proxy.yourdomain.com:port',
'https': 'http://proxy.yourdomain.com:port',}
url = 'https://www.quandl.com/api/v3/datasets/GOOG/NYSE_SPY.json?collapse=daily'
response = requests.get(url, proxies=proxies)
status = response.status_code
html_text = response.text
repo_data = response.json()
print(repo_data)
print(status)
print('HTML TEXT')
print('=========')
print(html_text)
if __name__ == '__main__':
main()
I've been trying to write a simple script to grab my ip and upload it to an ftp server. I can do it if i use just use a simple string but im struggling at uploading the IP address. Im thinking something like
def testftp():
filename="testing1.txt" #know this should all be indented
targetfile=open(filename, 'a')
#get my ip, dont think this is the way to do it, but it works in interactive anyways
getmyip=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
getmyip.connect(("google.com", 80))
print(getmyip.getsockname()[0])
targetfile.write("Your ip address is: ")
targetfile.write(getip)
tragetfile.write('\n')
session = ftplib.FTP('ftp.com', 'user', 'pass')
file = open('testing1.txt', 'rb')
session.storbinary('STOR testing1.txt', file)
file.close()
session.quit()
testftp()
I get an "TypeError: expected a character buffer object" error and i've googled that but i cant seem to find anything specific to this example. ehh. So i hit a brick wall and thought id finally post here. Hopefully someone can help.. Thanks in advance.