I have a problem. So, i connected 3 huawei e303 modems to my computer. I can see their interfaces and IPs that were given them by system. Here is an example
`modems = []
modem_temp = modem_sys()
for temp in interfaces():
addr = ifaddresses(temp)
if addr.get(AF_INET, None):
if '58:2c:80' in addr[AF_INET][0]['addr']: # 58:2c:80 is huawei
modem_temp.set_ip_adress(addr[AF_INET][0]['addr'])
print(modem_temp.ip_adress)
modem_temp.set_modem_interface(temp)
print(modem_temp.modem_interface)
modems.append(modem_temp)`
Where modem_sys is my class that contains only info about modem.
Output is:
169.254.169.233 {E549A313-E2CF-489A-A08B-A263CA7CBDB0}
192.168.1.100 {F3477F61-FC2D-4752-9FD9-0CF76529EA60}
169.254.170.23 {6F59AD83-A773-4D24-BF01-24FE7D9B6723}
But I can use to execute xml commands only 1 modem, because i can do them only on 192.168.1.1 address. So how can simultaniously use all modems to execute xml-commands? For example, after detecting modems, print device info by response = requests.get('http://192.168.1.1/api/device/information'). I'm using python3. I will be very appriciate for any help.
Related
Unsure how to phrase this, as I am new to Python.
Basically, I have this method in Python (below) that is scanning for devices available on my network. The Python code returns only 6 devices, and not the one I am searching for (Roku)
def scanner(self):
"""
Method that scans the network for available IP addresses.
"""
if len(self.ip) == 0:
network = '192.168.1.0/24'
else:
network = self.ip + '/24'
print("Scanning please wait...")
nm = nmap.PortScanner()
nm.scan(hosts=network, arguments='-sn')
host_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()]
for item in nm.all_hosts():
print(item)
for host, status in host_list:
self.ip_addresses.append(host)
self.choose_address()
When I use sudo nmap -sn 192.168.1.0/24 in my terminal, I get back 9 devices, including the Roku. Does anyone have any insight into why this doesn't want to work for me?
I am running python 3.7 on macOS
I am trying to have my code scan my wifi network to see when my phone joins the specified wifi network, i provide the routers ip address ("ip") and the phones assigned ip address when it connects to that specified wifi ("device")
When i run my code i get no errors but i also get no output
I have tried using "subprocess.Popen(["say, 'Iphone just connected to the network'"]) to speak but that did not work so i tried echo and say, which also did not work.
At this point i'm not sure what part of my code is the part that is not working, this is my first try at automation so any help would be greatly appreciated.
Thank you
import sys
import subprocess
import os
from decouple import config
ip = "192.168.*.***"
device = "192.168.*.***"
proc = subprocess.Popen(["ping", ip],stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if not line:
break
connected_ip = line.decode('utf-8').split()[3]
if connected_ip == device:
os.system("echo 'Iphone just connected to the network'")
os.system("say 'Iphone just connected to the network'")
'''subprocess.Popen(["say, 'Iphone just connected to the network'"])'''
For example, I have connected to a LAN using the ethernet cable and to the internet using the WiFi. My computer has got 2 more interfaces that are currently not used.
The script should check for only active interfaces and return their IPs, router IPs, and the device names.
I would use netifaces. Like this:
import netifaces as ni
for iface in ni.interfaces():
print('interface : {0}'.format(iface))
try:
addrs = ni.ifaddresses(iface)
for k, v in addrs[ni.AF_INET][0].items():
print(' {0} : {1}'.format(k, v))
except:
pass
gways = ni.gateways()
print('gateway: {0}'.format(gways['default'][ni.AF_INET][0]))
first i'm sorry if my english is not very good because i'm french.
I'm a beginner on bluetooth development with pyBluez (i already know TCP and UDP sockets) and i have a little problem with the server.
when i run the following code, my computer seems to be in a listening state but when my phone(nokia xpress music 5800) try to detect it, it doesn't see anything!
and there is two surprising things for me:
1)there is no fonction in my code configuring a name like "my computer". maybe my phone find my address but,because it can't resolve the name by lookup_name()-like function, it considers my computers isn't on?
2)when i replace serversock.bind(("",PORT_ANY)) by serversock.bind(("00:12:34:56:78:9A",PORT_ANY)) where 00:12:34:56:78:9A is the address given by hciconfig tool for my bluetooth device, i'm getting :
_bluetooth.error: (98, 'Address already in use'
just a stupid question because i think the answer is yes bacause of channels:
is it possible to have multiple program listening on the SAME bluetooth interfaces.
i run the program on root
from bluetooth import *
serversock=BluetoothSocket(RFCOMM)
serversock.bind(("",PORT_ANY))
serversock.listen(1)
advertise_service(serversock,"SampleServer",service_classes = [ SERIAL_PORT_CLASS ],profiles = [ SERIAL_PORT_PROFILE ] )
clientsock,clientinfo = serversock.accept()
print "Accepted connection from " , clientinfo
clientsock.send("PyBluez server says Hello!! ")
data = clientsock.recv(1024)
print "received: " , data
clientsock.close()
serversock.close()
thank you
Check the server's visibility with hciconfig:
# hciconfig
hci0: Type: USB
BD Address: 00:11:22:12:34:56 ACL MTU: 192:8 SCO MTU: 64:8
UP RUNNING PSCAN ISCAN
RX bytes:296 acl:0 sco:0 events:26 errors:0
TX bytes:536 acl:0 sco:0 commands:22 errors:0
In case your local device is invisible, set visibility on with:
hciconfig hci0 piscan
Turn off visibility if needed with:
hciconfig hci0 noscan
On my linux machine, 1 of 3 network interfaces may be actually connected to the internet. I need to get the IP address of the currently connected interface, keeping in mind that my other 2 interfaces may be assigned IP addresses, just not be connected.
I can just ping a website through each of my interfaces to determine which one has connectivity, but I'd like to get this faster than waiting for a ping time out. And I'd like to not have to rely on an external website being up.
Update:
All my interfaces may have ip addresses and gateways. This is for an embedded device. So we allow the user to choose between say eth0 and eth1. But if there's no connection on the interface that the user tells us to use, we fall back to say eth2 which (in theory) will always work.
So what I need to do is first check if the user's selection is connected and if so return that IP. Otherwise I need to get the ip of eth2. I can get the IPs of the interfaces just fine, it's just determining which one is actually connected.
If the default gateway for the system is reliable, then grab that from the output from route -n the line that contains " UG " (note the spaces) will also contain the IP of the gateway and interface name of the active interface.
the solution is here : http://code.activestate.com/recipes/439093-get-names-of-all-up-network-interfaces-linux-only/
import fcntl
import array
import struct
import socket
import platform
"""
global constants. If you don't like 'em here,
move 'em inside the function definition.
"""
SIOCGIFCONF = 0x8912
MAXBYTES = 8096
def localifs():
"""
Used to get a list of the up interfaces and associated IP addresses
on this machine (linux only).
Returns:
List of interface tuples. Each tuple consists of
(interface name, interface IP)
"""
global SIOCGIFCONF
global MAXBYTES
arch = platform.architecture()[0]
# I really don't know what to call these right now
var1 = -1
var2 = -1
if arch == '32bit':
var1 = 32
var2 = 32
elif arch == '64bit':
var1 = 16
var2 = 40
else:
raise OSError("Unknown architecture: %s" % arch)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
names = array.array('B', '\0' * MAXBYTES)
outbytes = struct.unpack('iL', fcntl.ioctl(
sock.fileno(),
SIOCGIFCONF,
struct.pack('iL', MAXBYTES, names.buffer_info()[0])
))[0]
namestr = names.tostring()
return [(namestr[i:i+var1].split('\0', 1)[0], socket.inet_ntoa(namestr[i+20:i+24])) \
for i in xrange(0, outbytes, var2)]
print localifs()