Getting Started with Termitty
Get up and running with Termitty in 5 minutes. Perfect for AI agents and automation scripts.
Installation
Install Termitty using pip:
pip install termitty
Or if you're using Poetry:
poetry add termitty
Requirements
- •Python 3.8 or higher
- •SSH access to target servers
- •No additional system dependencies
Quick Start
1. Basic Connection and Command
from termitty import TermittySession
# Connect and run a simple command
with TermittySession() as session:
session.connect('example.com', username='user', password='pass')
result = session.execute('ls -la')
print(result.output)
2. Using SSH Keys (Recommended)
from termitty import TermittySession
# More secure: use SSH keys
with TermittySession() as session:
session.connect(
'example.com',
username='deploy',
key_file='~/.ssh/id_rsa'
)
result = session.execute('systemctl status nginx')
print(f"Exit code: {result.exit_code}")
3. Smart Waiting (No More Sleep!)
from termitty import TermittySession
from termitty.conditions import OutputContains, PromptReady
with TermittySession() as session:
session.connect('example.com', username='deploy', key_file='~/.ssh/id_rsa')
# Start a long-running process
session.execute('./deploy.sh', wait=False)
# Wait for specific output instead of arbitrary sleep
session.wait_until(OutputContains('Deployment successful'), timeout=300)
# Wait for prompt to be ready
session.wait_until(PromptReady())
print("Deployment completed!")
4. AI-Friendly State Access
from termitty import TermittySession
import json
with TermittySession() as session:
session.connect('example.com', username='deploy', key_file='~/.ssh/id_rsa')
# Execute command
session.execute('htop')
# Get structured state for AI processing
state = session.state.terminal.get_structured_state()
# State includes:
# - Current screen content
# - Cursor position
# - Detected UI elements (menus, prompts, etc.)
# - Working directory and environment
print(json.dumps(state, indent=2))
Interactive Sessions
For interactive applications like vim, installers, or terminal UIs:
from termitty import TermittySession
from termitty.keys import Keys
with TermittySession() as session:
session.connect('example.com', username='user', key_file='~/.ssh/id_rsa')
# Start interactive shell
with session.interactive_shell() as shell:
# Edit a file
shell.send_line('vim config.yaml')
shell.wait_for_text('~') # Wait for vim to load
# Enter insert mode and add content
shell.send_key('i')
shell.send_keys('server_port: 8080\n')
# Save and exit
shell.send_key(Keys.ESCAPE)
shell.send_line(':wq')
# Verify the change
shell.send_line('cat config.yaml')
output = shell.get_screen_text()
print(output)
Handling Authentication
Termitty makes it easy to handle various authentication scenarios:
from termitty import TermittySession
from termitty.conditions import TextContains
with TermittySession() as session:
session.connect('example.com', username='admin', key_file='~/.ssh/id_rsa')
# Run sudo command
session.execute('sudo systemctl restart nginx', wait=False)
# Handle sudo password prompt
if session.wait_until(TextContains('[sudo] password'), timeout=5):
session.send_line('your_sudo_password', secure=True)
# Wait for completion
session.wait_until(PromptReady())
# Verify service is running
result = session.execute('systemctl status nginx')
if 'active (running)' in result.output:
print("Nginx restarted successfully!")
Error Handling
Built-in error handling for robust automation:
from termitty import TermittySession
from termitty.exceptions import ConnectionError, CommandTimeout
try:
with TermittySession() as session:
session.connect('example.com', username='deploy', timeout=30)
# Execute with timeout
result = session.execute('long_running_script.sh', timeout=600)
if result.exit_code != 0:
print(f"Command failed: {result.stderr}")
# AI can analyze the error and decide what to do
except ConnectionError as e:
print(f"Failed to connect: {e}")
# AI can try alternative servers or methods
except CommandTimeout as e:
print(f"Command timed out: {e}")
# AI can decide whether to retry or escalate
Next Steps
AI Integration Guide
Learn how to integrate Termitty with LangChain, OpenAI, and build autonomous agents.
API Reference
Explore the full API documentation with detailed examples.
Need Help?
Join our Discord community for support, or check out more examples on GitHub.