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.
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.
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:
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.