---
# System prepended metadata

title: Query 207

---

To remotely check the version of Python installed on an AWS EC2 instance, you can use the paramiko library for SSH communication [1][2]. Here's a Python script to achieve this:
```python
import paramiko

# EC2 instance details
instance_ip = 'YOUR_INSTANCE_IP'  # Replace with your EC2 instance's public IP address or DNS name
ssh_username = 'YOUR_SSH_USERNAME'  # Replace with your SSH username
ssh_key_file = 'your-ssh-key.pem'  # Replace with the path to your private SSH key file

# SSH connection setup
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Connect to the EC2 instance
try:
    key = paramiko.RSAKey.from_private_key_file(ssh_key_file)
    ssh.connect(instance_ip, username=ssh_username, pkey=key)

    # Run a command to check the Python version
    stdin, stdout, stderr = ssh.exec_command('python --version')

    # Read the output
    python_version = stdout.read().decode('utf-8')

    # Display the Python version
    print("Python version on the EC2 instance:")
    print(python_version)

except paramiko.AuthenticationException:
    print("Authentication failed. Please check your SSH key and username.")
except paramiko.SSHException as e:
    print(f"Unable to establish an SSH connection: {str(e)}")
except Exception as e:
    print(f"An error occurred: {str(e)}")
finally:
    ssh.close()
```
Before running this script, make sure you have the paramiko library installed. You can install it using pip:
```
pip install paramiko
```
Here's what the script does:
* Replace `YOUR_INSTANCE_IP, YOUR_SSH_USERNAME, and your-ssh-key.pem` with the public IP or DNS name of your EC2 instance, your SSH username, and the path to your private SSH key file.
* The script establishes an SSH connection to the EC2 instance using the provided credentials.
* It runs the python --version command on the remote server using SSH.
* The Python version output is captured, displayed, and printed to the console.

Make sure that your EC2 instance is reachable over SSH, and you have the appropriate permissions and key pair for SSH access to the instance.