Python alternative to linux bluetooth command "rfcomm connect" - python

Requirement:
I need to connect to a remote bluetooth device & port and send data using a device file.
1. First scan the nearest bluetooth devices
2. connect to a remote BT addr & channel and communicate using a device file (/dev/rfcomm0)
I'm stuck at the second step. I'm able to do it through linux shell
sudo rfcomm connect /dev/rfcomm0 00:11:22:33:44:55 1 &
This works and then I open my python interpreter and communicate to the remote device using the rfcomm0 device file.
But my requirement is such that the device addr could be changing. So I want to connect and release the connections through python program.
I tried using python subprocess.
But the problem is it returns immediately with a returncode 0 and then the connection is established after certain latency.
import subprocess
host = '00:11:22:33:44:55'
port = "1"
subprocess.call(["rfcomm connect",host,port,"&"],shell=True)
I'm looking if there is any pyBluez or any other python alternative to achieve this.

import subprocess
host = input()
port = 1
cmd = "sudo rfcomm connect /dev/rfcomm0 {} {} &".format(host, port)
conn = subprocess.Popen(cmd, shell=True)
if conn.returncode is None:
print("error in opening connection")
import the subprocess module
Read bluetooth address from the user(host)
port number can also be read as input, I am considering the default port 1
cmd = "sudo rfcomm connect /dev/rfcomm0 {} {} &".format(host, port) will create a command from the given arguments
There are many ways to read the output and errors after the execution of command. Read more about Popen#https://docs.python.org/3/library/subprocess.html

You can you the os module to run Shell commands. You can store the return value like this:
from os import system
Returnedstring = system("Shell command")

Related

Unable to view Serial Ports (ComPorts) on android

I installed Octo4A on my android phone.
It installed Alpine linux and python3.
When I run a python script to view the serial ports.
It says no ports are found, but it does find the ports on my windows computer using the same script:
import serial.tools.list_ports as ports
def getAvailablePorts():
availablePorts = list(ports.comports())
return availablePorts
availablePorts = getAvailablePorts()
for port in availablePorts:
print("Available port: " + port.device)
The output on windows:
Available port: COM3
Here are the serials listed in Octo4a:
How can I get a list of the available ports and connect to it on android using python3?
If you look at the code, the way pyserial looks for ports in Linux is by trying to find the following strings:
/dev/ttyS* # built-in serial ports
/dev/ttyUSB* # usb-serial with own driver
/dev/ttyXRUSB* # xr-usb-serial port exar (DELL Edge 3001)
/dev/ttyACM* # usb-serial with CDC-ACM profile
/dev/ttyAMA* # ARM internal port (raspi)
/dev/rfcomm* # BT serial devices
/dev/ttyAP* # Advantech multi-port serial controllers
/dev/ttyGS* # https://www.kernel.org/doc/Documentation/usb/gadget_serial.txt
In Octo4A, serial ports are apparently called /dev/ttyOcto4a so they will not be found by list_ports().
Of course, that does not mean pyserial won't work, you can try to instantiate and open your serial port directly with:
import serial
ser = serial.Serial(port='/dev/ttyOcto4a')
ser.isOpen()
I have not tried this myself, so I can not guarantee it will work.

Connecting using Python Paramiko through jump host that offers an interactive selection of target servers

I am trying to connect to a server using SSH protocol through a jump server. When I connect through a terminal using a protocol, the jump server opens a shell and asks for a server number from the list of available servers provided, followed by a user or password. Using the library Paramiko.
My code:
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
hostname="server_ip",
username="user",
password="pass",
look_for_keys=False,
allow_agent=False
)
com='1'
stdin, stdout, stderr = client.exec_command(com)
data = stdout.read() + stderr.read()
print(data.decode('utf-8'))
I get message:
Invalid target.
My shell on the jump server looks like this:
Your jump server probably shows the selection in an interactive shell session only. So you will have to use SSHClient.invoke_shell, what is otherwise not good thing to do when automating a connection.
See also What is the difference between exec_command and send with invoke_shell() on Paramiko?

How can I get live established UDP connections with python using psutil

I am trying to get the destination IP (remote host) addresses connected to my machine that are using UDP protocol but i get Null results using psutil
the script I wrote
import psutil
def GetServerIP():
PROCNAME = "theprocessname.exe"
for proc in psutil.process_iter():
if proc.name() == PROCNAME:
pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])
pidnumber = pinfo["pid"]
print("Process is runnging on number: %r" % (pidnumber))
for connection in psutil.net_connections(kind='udp4'):
if pidnumber in connection:
print(connection.raddr)
GetServerIP()
The script works for TCP connections but gives nothing on UDP connections that are established on my local machine.
I read through psutil documentation however still cant figure out why it gives no results back on UDP
I can verify there are established UDP packets being sent and received using wireshark
if psutil does not work well with UDP is there an alternative solution
It seems like psutil was not the right solution and path to take, turned out UDP connections are not established like TCP, so instead I switched to python scapy to capture UDP packets and it helped me resolve the destination IP address

Get command result from SSH/Telnet via another SSH jump server in Python

I am writing a Python code to SSH or Telnet remote hosts, execute some commands and get the result output. Here we have a jump server, so the code must be "connected" with this server and from it, Telnet or SSH the remote hosts.
All my approaches work fine within the jump server, for example I can get the output of commands inside it. The problem is when I try to remote connect to hosts from it.
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('IP', 22, username="user", password="pass")
stdin, stdout, stderr = client.exec_command("command")
for line in stdout:
print('... ' + line.strip('\n'))
client.close()
Using the library jumpssh get same results, and I am not will able to Telnet hosts. I tried the follow approach, but get the error
Administratively prohibited.
from jumpssh import SSHSession
gateway_session = SSHSession('jumpserver','user', password='pass').open()
remote_session = gateway_session.get_remote_session('IP',password='pass')
print(gateway_session.get_cmd_output('command'))
In the last company i worked we had a license from an SSH client that supports Python scripts, and worked fine in a more "textual" treatment.
There is any way of accomplish same task natively in Python?
SSHSession is trying to open direct-tcpip port forwarding channel over the gateway_session.
"administratively prohibited" is OpenSSH sshd server message indicating that direct-tcpip port forwarding is disabled.
To enable port forwarding, set AllowTcpForwarding and DisableForwarding directives in sshd_config appropriately.
If you cannot enable the port forwarding on the server, you cannot use jumpssh library.
If you have a shell access to the server, you can use ProxyCommand-like approach instead.
See Paramiko: nest ssh session to another machine while preserving paramiko functionality (ProxyCommand).

Pairing bluetooth devices with Passkey/Password in python - RFCOMM (Linux)

I am working on a Python script to search for bluetooth devices and connect them using RFCOMM. This devices has Passkey/Password. I am using PyBlueZ and, as far as I know, this library cannot handle Passkey/Password connections (Python PyBluez connecting to passkey protected device).
I am able to discover the devices and retrieve their names and addresses:
nearby_devices = bluetooth.discover_devices(duration=4,lookup_names=True,
flush_cache=True, lookup_class=False)
But if tried to connect to a specific device using:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((addr,port))
I get an error 'Device or resource busy (16)'.
I tried some bash commands using the hcitool and bluetooth-agent, but I need to do the connection programmatically. I was able to connect to my device using the steps described here: How to pair a bluetooth device from command line on Linux.
I want to ask if someone has connected to a bluetooth device with Passkey/Password using Python. I am thinking about to use the bash commands in Python using subprocess.call(), but I am not sure if it is a good idea.
Thanks for any help.
Finally I am able to connect to a device using PyBlueZ. I hope this answer will help others in the future. I tried the following:
First, import the modules and discover the devices.
import bluetooth, subprocess
nearby_devices = bluetooth.discover_devices(duration=4,lookup_names=True,
flush_cache=True, lookup_class=False)
When you discover the device you want to connect, you need to know port, the address and passkey. With that information do the next:
name = name # Device name
addr = addr # Device Address
port = 1 # RFCOMM port
passkey = "1111" # passkey of the device you want to connect
# kill any "bluetooth-agent" process that is already running
subprocess.call("kill -9 `pidof bluetooth-agent`",shell=True)
# Start a new "bluetooth-agent" process where XXXX is the passkey
status = subprocess.call("bluetooth-agent " + passkey + " &",shell=True)
# Now, connect in the same way as always with PyBlueZ
try:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((addr,port))
except bluetooth.btcommon.BluetoothError as err:
# Error handler
pass
Now, you are connected!! You can use your socket for the task you need:
s.recv(1024) # Buffer size
s.send("Hello World!")
Official PyBlueZ documentation is available here
Is there a way to connect two phones via Bluetooth , the script should be running on a Linux host. Any suggestions of using pybluez or any other APIs?
I have seen some examples where a Linux host is used as Client and is connect a phone (which is a server), but here I'm want to use Linux host as just a device to run the script and make two phones connect via Bluetooth.

Categories