|
| 1 | +# Getting Started with SSHScript |
| 2 | + |
| 3 | +SSHScript is a Python library that simplifies SSH interactions, allowing you to write shell scripts in Python with enhanced control and readability. This guide will walk you through the basics of setting up and using SSHScript. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +First, you need to install SSHScript. You can do this using pip: |
| 8 | + |
| 9 | +```bash |
| 10 | +pip install sshscript |
| 11 | +``` |
| 12 | + |
| 13 | +## Basic Usage |
| 14 | + |
| 15 | +Let's start with a simple example. We'll connect to a remote server, execute a command, and print the output. |
| 16 | + |
| 17 | +```python |
| 18 | +from sshscript import Session |
| 19 | + |
| 20 | +# Create a new SSH session |
| 21 | +# Replace 'user', 'password', and 'your_server_ip' with your actual credentials and server IP |
| 22 | +with Session('user@your_server_ip', password='your_password') as session: |
| 23 | + # Execute a command on the remote server |
| 24 | + result = session.run('ls -l /') |
| 25 | + |
| 26 | + # Print the standard output and standard error |
| 27 | + print("STDOUT:") |
| 28 | + print(result.stdout) |
| 29 | + print("STDERR:") |
| 30 | + print(result.stderr) |
| 31 | + |
| 32 | + # You can also access the exit code |
| 33 | + print(f"Exit Code: {result.exit_code}") |
| 34 | +``` |
| 35 | + |
| 36 | +### Running Multiple Commands |
| 37 | + |
| 38 | +You can run multiple commands sequentially within the same session: |
| 39 | + |
| 40 | +```python |
| 41 | +from sshscript import Session |
| 42 | + |
| 43 | +with Session('user@your_server_ip', password='your_password') as session: |
| 44 | + session.run('mkdir my_test_directory') |
| 45 | + session.run('echo "Hello from SSHScript!" > my_test_directory/hello.txt') |
| 46 | + result = session.run('cat my_test_directory/hello.txt') |
| 47 | + print(result.stdout) |
| 48 | + session.run('rm -rf my_test_directory') |
| 49 | +``` |
| 50 | + |
| 51 | +### Handling Errors |
| 52 | + |
| 53 | +SSHScript allows you to easily check for command execution errors. By default, if a command returns a non-zero exit code, an `SSHScriptError` will be raised. |
0 commit comments