AttributeError: 'L2bpfSocket' object has no attribute 'ins' - python

I am trying to build a Networkscanner that scans my localnetwork.
Here is how I would do it:
Create an ARP Request.
Create an Ethernet Frame.
Place the ARP Request inside the Ethernet Frame.
Send the combined frame and receive responses.
Then parse the responses and print the results.
Here is my code so far:
#!/usr/bin/env python3
import scapy.all as scapy
IP = 'here i would enter my IP adress'
def scan(ip):
arp_packet = scapy.ARP(pdst=ip)
broadcast_packet = scapy.Ether(dst='ff:ff:ff:ff:ff:ff')
arp_broadcast_packet = broadcast_packet/arp_packet
scapy.srp(arp_broadcast_packet, timeout = 1, verbose = False)
scan(IP)
However I am getting the error message :
AttributeError: 'L2bpfSocket' object has no attribute 'ins'
Here is the complete message:
Traceback (most recent call last):
File "/Users/Uni/Dropbox/Mein Mac (Elias MacBook Pro)/Desktop/wifi2.py", line 12, in <module>
scan(IP)
File "/Users/Uni/Dropbox/Mein Mac (Elias MacBook Pro)/Desktop/wifi2.py", line 10, in scan
scapy.srp(arp_broadcast_packet, timeout = 1, verbose = False)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scapy/sendrecv.py", line 552, in srp
s = conf.L2socket(promisc=promisc, iface=iface,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scapy/arch/bpf/supersocket.py", line 242, in __init__
super(L2bpfListenSocket, self).__init__(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scapy/arch/bpf/supersocket.py", line 62, in __init__
(self.ins, self.dev_bpf) = get_dev_bpf()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scapy/arch/bpf/core.py", line 114, in get_dev_bpf
raise Scapy_Exception("No /dev/bpf handle is available !")
scapy.error.Scapy_Exception: No /dev/bpf handle is available !
Exception ignored in: <function _L2bpfSocket.__del__ at 0x7fc3220493a0>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scapy/arch/bpf/supersocket.py", line 139, in __del__
self.close()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scapy/arch/bpf/supersocket.py", line 211, in close
if not self.closed and self.ins is not None:
AttributeError: 'L2bpfSocket' object has no attribute 'ins'
Elias-MBP:~ Uni$
I read that it could be do to the program trying to close a L2bpfSocket that couldn't be created, therefore not having a Attribute ins.
However I have no clue how to fix this, or why the L2bpfSocket couldn't be created in the first place.

Make sure you're running your script as root.
The second issue isn't important, the main one being that Scapy can't open a /dev/bpf handler.

Related

D-Bus - 'ServiceUnknown' exception encountered while calling a remote procedure

I'm trying to call the remote procedure DisplayFolderAndSelect() of Thunar file manager from my own program:
import dbus
bus = dbus.SessionBus()
obj = bus.get_object('org.xfce.Thunar', '/org/xfce/FileManager')
iface = dbus.Interface(obj, 'org.xfce.FileManager')
_thunar_display_folder_and_select = iface.get_dbus_method('DisplayFolderAndSelect')
_thunar_display_folder_and_select('~/Downloads/', 'doc.pdf', '', '')
However I've encountered the following exception at runtime:
Traceback (most recent call last): File "", line 1, in
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py",
line 70, in call
return self._proxy_method(*args, **keywords) File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in
call
**keywords) File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in
call_blocking
message, timeout) dbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name :1.576 was not
provided by any .service files
I'm unable to understand what does this exception mean. And what's the cause behind the exception.
Any thoughts?
I think it is an OS-related issue, restarting D-Bus service solved the problem

Getting "TypeError: 'NoneType' object is not iterable" while doing parallel ssh

I am trying to do parallel ssh on servers. While doing this, i am getting "TypeError: 'NoneType' object is not iterable" this error. Kindly help.
My script is below
from pssh import ParallelSSHClient
from pssh.exceptions import AuthenticationException, UnknownHostException, ConnectionErrorException
def parallelsshjob():
client = ParallelSSHClient(['10.84.226.72','10.84.226.74'], user = 'root', password = 'XXX')
try:
output = client.run_command('racadm getsvctag', sudo=True)
print output
except (AuthenticationException, UnknownHostException, ConnectionErrorException):
pass
#print output
if __name__ == '__main__':
parallelsshjob()
And the Traceback is below
Traceback (most recent call last):
File "parallelssh.py", line 17, in <module>
parallelsshjob()
File "parallelssh.py", line 10, in parallelsshjob
output = client.run_command('racadm getsvctag', sudo=True)
File "/Library/Python/2.7/site-packages/pssh/pssh_client.py", line 520, in run_command
raise ex
TypeError: 'NoneType' object is not iterable
Help me with the solution and also suggest me to use ssh-agent in this same script. Thanks in advance.
From reading the code and debugging a bit on my laptop, I believe the issue is that you don't have a file called ~/.ssh/config. It seems that parallel-ssh has a dependency on OpenSSH configuration, and this is the error you get when that file is missing.
read_openssh_config returns None here: https://github.com/pkittenis/parallel-ssh/blob/master/pssh/utils.py#L79
In turn, SSHClient.__init__ blows up when trying to unpack the values it expects to receive: https://github.com/pkittenis/parallel-ssh/blob/master/pssh/ssh_client.py#L97.
The fix is presumably to get some sort of OpenSSH config file in place, but I'm sorry to say I know nothing about that.
EDIT
After cleaning up some of parallel-ssh's exception handling, here's a better stack trace for the error:
Traceback (most recent call last):
File "test.py", line 11, in <module>
parallelsshjob()
File "test.py", line 7, in parallelsshjob
output = client.run_command('racadm getsvctag', sudo=True)
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/pssh/pssh_client.py", line 517, in run_command
self.get_output(cmd, output)
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/pssh/pssh_client.py", line 601, in get_output
(channel, host, stdout, stderr, stdin) = cmd.get()
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/gevent/greenlet.py", line 480, in get
self._raise_exception()
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/gevent/greenlet.py", line 171, in _raise_exception
reraise(*self.exc_info)
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/gevent/greenlet.py", line 534, in run
result = self._run(*self.args, **self.kwargs)
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/pssh/pssh_client.py", line 559, in _exec_command
channel_timeout=self.channel_timeout)
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/pssh/ssh_client.py", line 98, in __init__
host, config_file=_openssh_config_file)
TypeError: 'NoneType' object is not iterable
This was seemingly a regression in the 0.92.0 version of the library which is now resolved in 0.92.1. Previous versions also work. OpenSSH config should not be a dependency.
To answer your SSH agent question, if there is one running and enabled in the user session it gets used automatically. If you would prefer to provide a private key programmatically can do the following
from pssh import ParallelSSHClient
from pssh.utils import load_private_key
pkey = load_private_key('my_private_key')
client = ParallelSSHClient(hosts, pkey=pkey)
Can also provide an agent with multiple keys programmatically, per below
from pssh import ParallelSSHClient
from pssh.utils import load_private_key
from pssh.agent import SSHAgent
pkey = load_private_key('my_private_key')
agent = SSHAgent()
agent.add_key(pkey)
client = ParallelSSHClient(hosts, agent=agent)
See documentation for more examples.

Scapy Sniff() - Device Is A NoneType

So I'm trying to get scapy to work on my windows computer. After a lot of work I finally was able to load the library without any errors. However when I call the function sniff(count=1) I get an error. It seems that a variable called "device" is getting the value None. Here is the error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\scapy\arch\windows\__init__.py", line 445, in sniff
s = L2socket(type=ETH_P_ALL, *arg, **karg)
File "C:\Python34\lib\site-packages\scapy\arch\pcapdnet.py", line 266, in __init__
self.ins = open_pcap(iface, 1600, self.promisc, 100)
File "C:\Python34\lib\site-packages\scapy\arch\windows\__init__.py", line 223, in <lambda>
pcapdnet.open_pcap = lambda iface,*args,**kargs: _orig_open_pcap(pcap_name(iface),*args,**kargs)
File "C:\Python34\lib\site-packages\scapy\arch\pcapdnet.py", line 251, in <lambda>
open_pcap = lambda *args,**kargs: _PcapWrapper_pypcap(*args,**kargs)
File "C:\Python34\lib\site-packages\scapy\arch\pcapdnet.py", line 215, in __init__
self.iface = create_string_buffer(device.encode('ascii'))
AttributeError: 'NoneType' object has no attribute 'encode'
Thanks In Advance For Any Help :)
After downloading the new development version and tinkering around with the timeouts of some of the powershell commands I fixed it! Guess it was just a bug with the old version. Scapy doesn't have the best Windows support.

Error in running Scapy Sniff function

i wrote this program to sniff icmp packets in the network and print there source address. The code is as follows:
from scapy.all import *
def fun_callback(pkt):
print str(pkt.payload.src)
sniff(prn = fun_callback, filter = 'icmp', timeout =5)
After running this program, I am getting this error.
[root#localhost icmp]# python test.py
WARNING: Failed to execute tcpdump. Check it is installed and in the PATH
WARNING: No route found for IPv6 destination :: (no default route?)
192.168.134.131
192.168.134.131
192.168.134.2
192.168.134.2
fe80::20c:29ff:fee4:a130
134.160.38.1
192.168.134.131
Traceback (most recent call last):
File "test.py", line 5, in <module>
sniff(prn = fun_callback, filter = 'icmp', timeout =5)
File "/usr/lib/python2.7/site-packages/scapy/sendrecv.py", line 586, in sniff
r = prn(p)
File "test.py", line 4, in fun_callback
print str(pkt.payload.src)
File "/usr/lib/python2.7/site-packages/scapy/packet.py", line 176, in __getattr__
fld,v = self.getfield_and_val(attr)
File "/usr/lib/python2.7/site-packages/scapy/packet.py", line 172, in getfield_and_val
return self.payload.getfield_and_val(attr)
File "/usr/lib/python2.7/site-packages/scapy/packet.py", line 172, in getfield_and_val
return self.payload.getfield_and_val(attr)
File "/usr/lib/python2.7/site-packages/scapy/packet.py", line 1057, in getfield_and_val
raise AttributeError(attr)
AttributeError: src
[root#localhost icmp]#
Why this exception is occurring?
You have sniffed a packet with a payload without src attribute. If you want a quick fix for your code, write:
def fun_callback(pkt):
if hasattr(pkt.payload, "src"):
print str(pkt.payload.src)
The problem is, you don't really known what pkt.payload will be. If you want a better fix, try something like:
def fun_callback(pkt):
if IP in pkt: print pkt[IP].src
elif IPv6 in pkt: print pkt[IPv6].src
Or better, with .sprintf():
sniff(prn=lambda pkt: pkt.sprintf("{IP:%IP.src%}{IPv6:%IPv6.src%}"),
filter='icmp', timeout=5))

How to use python kafka client with gevent - is there a libary that actually works?

I am trying to use gevent to write to kafka 0.7.2 using brod on python 2.7.
Here is the error message I get. Guess its due to blocking. brod supports tornado but I use gevent.
No handlers could be found for logger "brod.socket"
Traceback (most recent call last):
File "/var/chef/cache/src/gevent/gevent/greenlet.py", line 328, in run
result = self._run(*self.args, **self.kwargs)
File "worker_server.py", line 204, in execute_kafka_pipe
kafka.produce(topic,payload)
File "/usr/local/lib/python2.7/dist-packages/brod-0.3.2-py2.7.egg/brod/base.py", line 287, in produce
return self._write(request, callback)
File "/usr/local/lib/python2.7/dist-packages/brod-0.3.2-py2.7.egg/brod/blocking.py", line 98, in _write
return self._write(data, callback, retries)
File "/usr/local/lib/python2.7/dist-packages/brod-0.3.2-py2.7.egg/brod/blocking.py", line 89, in _write
wrote_length += self._socket.send(data)
File "/var/chef/cache/src/gevent/gevent/socket.py", line 441, in send
self._wait(self._write_event)
File "/var/chef/cache/src/gevent/gevent/socket.py", line 292, in _wait
assert watcher.callback is None, 'This socket is already used by another greenlet: %r' % (watcher.callback, )
AssertionError: This socket is already used by another greenlet: <bound method Waiter.switch of <gevent.hub.Waiter object at 0x1dece60>>
<Greenlet at 0x1d4b9b0: execute_kafka_pipe('topic-spend', '{"enode": 1, "city": "Cairns", "dl": "en", "wnode)> failed with AssertionError
I tried to use gevent-kakfa but depends on gevent-zookeeper.
When trying to connect to zookeeper i get this message:
Traceback (most recent call last):
File "/home/ubuntu/workspace/rtbhui-devops/servers/worker_server.py", line 68, in <module>
framework = gevent_zookeeper.ZookeeperFramework('localhost:2181', 10)
File "/usr/local/lib/python2.7/dist-packages/gevent_zookeeper/framework.py", line 241, in __init__
self.client = ZookeeperClient(hosts, timeout)
File "/usr/local/lib/python2.7/dist-packages/gevent_zookeeper/client.py", line 211, in __init__
self._event = gevent.core.event(
AttributeError: 'module' object has no attribute 'core'
Exception AttributeError: "'ZookeeperClient' object has no attribute '_event'" in <bound method ZookeeperClient.__del__ of <gevent_zookeeper.client.ZookeeperClient object at 0x274ded0>> ignored
Is there not a python lib that I can I write messages using gevent that works?
You might try https://github.com/hmahmood/kafka-python/tree/gevent-impl
which is currently submitted for PR to the mainline kafka-python library:
https://github.com/mumrah/kafka-python/pull/145
Try Pykafka
Although you should be able to configure kafka-python by passing in the selector used by gevent

Categories