Creating a Raspberry Pi command interface via Telegram bot

I’m working on a project to control my Raspberry Pi 2B running Arch Linux ARM using a Telegram bot. The goal is to execute shell commands remotely. I’ve got basic functionality working with Python 3.6 and Telepot, but I’m hitting some roadblocks.

My current setup uses subprocess to run commands, but it can’t handle interactive prompts or long-running processes. For example, I can’t input passwords for sudo or ssh, or stop a ping command.

Here’s a snippet of what I’ve tried:

import subprocess

def run_command(cmd):
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, error = process.communicate()
    return output.decode(), error.decode()

# Usage
result, err = run_command('ls -l')
print(result)

This works for simple commands, but falls short for interactive stuff. I’ve looked into pexpect, but I’m not sure how to use it without knowing the expected output in advance.

Any ideas on how to create a more interactive shell experience through the bot? I’d love to be able to handle password prompts, use pipes, and even run commands like ‘cat’ that wait for input.

Thanks for any suggestions!

I’ve tackled a similar project before and can offer some insights based on my experience. While subprocess works for basic commands, it falls short with interactive scenarios. Instead, consider using the ‘paramiko’ library to set up an SSH connection to your Raspberry Pi. By employing its invoke_shell() method, you can create a pseudo-terminal to send commands and capture live output, which helps you handle password prompts and long-running processes.

Remember to implement robust error handling and secure your setup by limiting command execution to authorized users.

hey sparklinggem, have u tried using the ‘fabric’ library? it’s great for running remote commands over SSH. u can set up a connection to ur Pi and execute commands easily. it handles interactive prompts too, so u can input passwords and stuff. might be worth checkin out for ur project

Having worked on a similar setup, I can tell you that using a library like ‘pexpect’ can be a game-changer for interactive shell experiences. It allows you to spawn child applications and control them as if a human were typing commands.

Here’s a basic example of how you might use pexpect:

import pexpect

def run_command(cmd):
    child = pexpect.spawn('/bin/bash', ['-c', cmd])
    child.expect(pexpect.EOF)
    return child.before.decode()

# For sudo commands
child = pexpect.spawn('sudo apt-get update')
child.expect('password')
child.sendline('your_password')
child.expect(pexpect.EOF)

This approach lets you handle password prompts and interactive commands more effectively. You can even set up pattern matching for different prompts or outputs, making your bot more versatile.

Just be cautious about security. Ensure you’re validating inputs and restricting access to sensitive commands. Also, consider implementing a timeout mechanism to prevent hanging on unexpected outputs.