I'm working on a HID bluetooth device with the code on the device in Python; at the moment it can connect to a PC by running:
os.system("hciconfig hcio class 0x002560")
os.system("hciconfig hcio name DataPaqWalk")
Then we can use pybluez to connect the sockets and wait for a connection:
print("Waiting for connections")
self.scontrol=BluetoothSocket(L2CAP)
self.sinterrupt=BluetoothSocket(L2CAP)
self.scontrol.listen(1) # Limit of 1 connection
self.sinterrupt.listen(1)
self.ccontrol,cinfo = self.scontrol.accept()
self.cinterrupt, cinfo = self.sinterrupt.accept()
This works and we have a thread polling with hcitool con to detect the Windows PC (adapter) disconnecting where we dump the sockets and listen again. The hci is setup with no security so the PC connecting to it automatically pairs - this all works.
However the issue comes when the device is powered off; the PC correctly detects that the device is gone and it remains in the paired state. What I want to do is to get the device to automatically connect to the PC it is paired with. I've obviously got the Mac address of the PC and I am trying to connect using:
(P_CTRL is 17 and P_INTR is 19)
self.ccontrol,cinfo = self.scontrol.connect(('C8:FF:28:79:05:D4', self.P_CTRL))
self.controlClientMac = cinfo[0]
self.controlClientPsm = cinfo[1]
print ('control is ' + self.controlClientMac + " " + str(self.controlClientPsm))
self.cinterrupt,cinfo = self.scontrol.connect(('C8:FF:28:79:05:D4', self.P_INTR))
self.interruptClientMac = cinfo[0]
self.interruptClientPsm = cinfo[1]
print ('interrupt is ' + self.interruptClientMac + " " + str(self.interruptClientPsm))
This basically tries to connect and gives me back:
Traceback (most recent call last):
File "server/btk_server.py", line 267, in <module>
myservice = BTKbService();
File "server/btk_server.py", line 226, in __init__
self.device.listen();
File "server/btk_server.py", line 174, in listen
self.ccontrol,cinfo = self.scontrol.connect(('C8:FF:28:79:05:D4', self.P_CTRL))
File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: (111, 'Connection refused')
In the bluetooth windows dialog you can see that it flicks to connected but straight back to paired. The question is; how do I get the device to connect to the paired windows adapter? Note that I'm getting a similar response in bluetoothctl.
Wouldnt you be able to run a script on the rpi upon startup automatically that starts looking for a bluetooth socket? https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/
Related
I used pycharm for writing my code and plcsim for simulatio. When I run my code and run plcsim I get an error.
I used Tia portal V16 and s7-1200 and I want to communicate between S7-1200 and python-snap7
This is the error:
PS C:\Users\Badro\PycharmProjects\pythonProject1\venv> py test.py
b' TCP : Unreachable peer'
Traceback (most recent call last):
- File "C:\Users\Badro\PycharmProjects\pythonProject1\venv\test.py", line 8, in <module>
plc.connect(IP, RACK, SLOT)
- File "C:\Users\Badro\AppData\Local\Programs\Python\Python310\lib\site-
packages\snap7\client.py",
line 24, in f
check_error(code, context="client")
- File "C:\Users\Badro\AppData\Local\Programs\Python\Python310\lib\site-
packages\snap7\common.py",
line 89, in check_error
raise RuntimeError(error)
RuntimeError: b' TCP : Unreachable peer'enter code here
and this is my code:
import snap7
IP = '192.168.100.100'
RACK = 0
SLOT = 1
plc = snap7.client.Client()
plc.connect(IP, RACK, SLOT)
print(plc.get_cpu_state())
I watched this videohttps://www.youtube.com/watch?v=BKnK4AT_WKs
Where is the problem or is the problem that plcsim must be the real plc for simulation not plcsim?
PLCSIM cannot simulate a network interface on simulated PLCs.
You have to install PLCSIM Advanced V3.0, which also simulates the network interface for each simulated PLC.
I have PLCSIM V13 SP1 and have tested your program and it works.
I think you need to install NetToPLCSim which allows you to connect to the Plc to test your application using the network interface of the PC running the simulation.
It is recommended that you run NetToPLCSim as an administrator.
I'm trying to communicate with a http server which is running on debian strech from a brand new out of the box cisco device. Now, the so called zero touch configuration is no problem:
The Switch gets an IP address and such via DHCP and a link to where to fetch it's initial configuration.
The switch gets its basic configuration such as user credentials etc.
The problem rises when I try to search through a database on the server from the switch. In this database some variables are stored. Depending the serialnumber of the switch, it should receive a specific hostname, Mgmt address etc.
On those new switches there is a python module integrated so I ran some tests. I tried to fetch the serial number and got them whithout any problems. The moment I tried to write the serial number on a txt file on the server I got this error
Traceback (most recent call last): File "", line 1, in
IOError: [Errno 2] No such file or directory:
'http://10.232.152.19:80/temp.txt'
Code so far:
from cli import cli
def get_serial():
serial = cli("show version | include System Serial\n")
serial = (serial.split()[-1])
f = open ("http://10.232.152.19:80/temp.txt", "a")
f.write(serial)
f.close
get_serial()
The problem you are facing is because you are trying to open a file from the network. You need to download the file first in you system and then open it. You should use urllib to fetch the file and then open it. then save it and again push it back.
import urllib
txt = urllib.urlopen(target_url).read()
I've looked at several very similar examples, but I'm doing something wrong...probably because I'm mixing up local or remote binding addresses or well, not sure. I've yet to find a document that can describe what each is supposed to be to a newb like me.
I have a raspberry pi in a robot which has MariaDB installed. I can connect to the server from my PC with SQL Workbench.
I have a second Pi that needs a python script that can send data to the first pi...
IP Addresses, names and passwords have been changed to protect the innocent, but the whole thing is a closed network anyways.
import mysql.connector
import sshtunnel
_host = Robot's IP Address
_ssh_port = 22
_username = Robot user login
_password = Robot Password
_remote_bind_address = Robot's IP Address
_remote_mysql_port = 3308
_local_bind_address = Second Pi's IP Address
_local_mysql_port = 3308
_db_user = Database User Name
_db_password = Database Password
_db_name = "joycap"
with sshtunnel.SSHTunnelForwarder(
(_host, _ssh_port),
ssh_username=_username,
ssh_password=_password,
remote_bind_address=(_remote_bind_address, _remote_mysql_port),
local_bind_address=(_local_bind_address,_local_mysql_port)
) as tunnel:
connection = mysql.connector.connect(
user=_db_user,
passwd=_db_password,
host=_local_bind_address,
database=_db_name,
port=_local_mysql_port)
Here's the error I'm current getting...
pi#raspberrypi:~ $ python dbtest1.py
2018-04-05 06:11:42,262| ERROR | Secsh channel 0 open FAILED: Connection refused: Connect failed
2018-04-05 06:11:42,277| ERROR | Could not establish connection from ('Second Pi's IP Address', 3308) to remote side of the tunnel
Traceback (most recent call last):
File "dbtest1.py", line 29, in <module>
port=_local_mysql_port)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/__init__.py", line 184, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 100, in __init__
self.connect(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/abstracts.py", line 733, in connect
self._open_connection()
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 241, in _open_connection
self._do_handshake()
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 108, in _do_handshake
packet = self._socket.recv()
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/network.py", line 248, in recv_plain
raise errors.InterfaceError(errno=2013)
mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query
Would appreciate any advice! Would think someone would have made a video on doing this by now :)
Regards,
Matt
Finally worked it out through trial and error.
_host = "192.168.8.1" #Server where database is hosted
_ssh_port = 22 #ssh port
_username = "pi" #user name to host server
_password = "raspberry" #password to host server
_remote_bind_address = "127.0.0.1" #local connection IP to db on host(should probably not change)
_remote_mysql_port = 3306
_local_bind_address = "0.0.0.0" #local bind address(should probably not change)
_local_mysql_port = 3306
_db_user = "databaseuser"
_db_password = "databasepass"
_db_name = "databasename"
I am trying to connect to my Neo4j graph database server from a new machine. I can successfully connect from an older machine but do not wish to use the older one anymore.
I have reduced the problem to a simple script that returns an exception:
from neo4j.v1 import GraphDatabase, basic_auth
auth = basic_auth("username","password")
session = GraphDatabase.driver("bolt://remote.server:7687",auth=auth).session()
statement = """MATCH (a:Protein)
WHERE a.name={name}
RETURN a.Accession"""
tx = session.begin_transaction()
record = tx.run(statement,{'name':"ARCH_HUMAN"}).single()
print record['a.Accession']
session.close()
And the error message is:
File "Test.py", line 10, in <module>
tx = session.begin_transaction()
File "/home/username/anaconda2/lib/python2.7/site-packages/neo4j/v1/api.py", line 432, in begin_transaction
self._connect()
File "/home/username/anaconda2/lib/python2.7/site-packages/neo4j/v1/api.py", line 269, in _connect
self._connection = self._acquirer(access_mode)
File "/home/username/anaconda2/lib/python2.7/site-packages/neo4j/v1/direct.py", line 52, in acquire
raise ServiceUnavailable("Cannot acquire connection to {!r}".format(self.address))
neo4j.exceptions.ServiceUnavailable: Cannot acquire connection to Address(host='remote.server', port=7687)
Port 7687 is open (confirmed via netstat -tulpn and iptables -L), and neo4j is configured to listen to 0.0.0.0:7687. In addition, .neo4j/known_hosts contains an entry for host 0.0.0.0
What's strange is that I get a different error message (neo4j.exceptions.AuthError) if I break the authentication by using an incorrect password. So the connection is being made to check the password, but still I cannot connect with the correct auth.
What's going on?
I too had the same issue and turns out the driver was the issue.
I did some experiments and found out that the last driver that it works for is neo4j-driver==v1.1.0 but the next version neo4j-driver==v1.2.0 it stops working for some reason.
Try uncomment dbms.connectors.default_listen_address=0.0.0.0 And check this
# Bolt connector
dbms.connector.bolt.enabled=true
dbms.connector.bolt.tls_level=OPTIONAL
dbms.connector.bolt.listen_address=:7687
# HTTP Connector. There must be exactly one HTTP connector.
dbms.connector.http.enabled=true
dbms.connector.http.listen_address=:7474
# HTTPS Connector. There can be zero or one HTTPS connectors.
dbms.connector.https.enabled=true
dbms.connector.https.listen_address=:7473
local-host --->Aterm server (security server ) -----> target-machine(
I am trying to write a code in Python using Paramiko to first SSH from local-host to the target-machine. From the target-machine, I want to capture some outputs and store them locally either as a variable or as a file (havent got to that point yet). I found an example from stackoverflow where they talk about using nested SSH with paramiko, and I follow it but I get stuck here:
i need just reaching the target-machine
My code:
import paramiko
import sys
import subprocess
hostname = '10.10.10.1'
port = 22
username = 'mohamed.hosseny'
password ='Pass#1'
client = paramiko.Transport((hostname, port))
client.connect(username=username, password=password)
client.close()
but i found the below error message :
Traceback (most recent call last):
File "C:/Users/mohamed.hosseny/Desktop/Paramiko.py", line 13, in <module>
client = paramiko.Transport((hostname, port))
File "C:\Python27\lib\site-packages\paramiko\transport.py", line 332, in
__init__
'Unable to connect to {}: {}'.format(hostname, reason))
SSHException: Unable to connect to 10.10.10.1: [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
paramiko.Transport is a lower level API. Don't use it unless you have a good reason. Instead, you can use paramiko.SSHClient.