Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions json_validator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# JSON File Validator
---
A simple Python script to validate JSON files and report syntax errors with line numbers.

## Introduction
---
This python script helps to quickly check if a JSON file is valid. If the file is invalid, it shows the exact line and column where the error occurs, making debugging easier.

## Features
---
- Validates JSON syntax.
- Reports exact line and column of errors.
- Handles file not found and other exceptions gracefully.


## Usage
---
### Prerequisites

Before using this script, ensure you have the following:
- Python 3.x installed on the system.

### Running the Script

1. Save the script as `json_validator.py`.
2. Open a terminal and navigate to the directory where the script is saved.
3. Run the script with the path to the JSON file:

```bash
python json_validator.py path/to/file.json
```

### Output
The script will print a statment displaying whether the File, inputed by user is a valid JSON or not.
In case the File is an invalid JSON file, the script will help in debugging by mentioning the that File is an invalid JSON with the line and probable suggestion to help fix the issue.

### License
This script is released under the **MIT License**. Feel free to use, modify, and distribute it as needed.
26 changes: 26 additions & 0 deletions json_validator/json_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json
import sys

def validate_json_file(file_path):
try:
with open(file_path, 'r') as file:
content = file.read()
json.loads(content)
print(f" The file '{file_path}' is valid JSON.")
return True
except json.JSONDecodeError as e:
print(f" The file '{file_path}' is NOT valid JSON.")
print(f"Error: {e.msg}")
print(f"Line: {e.lineno}, Column: {e.colno}")
return False
except FileNotFoundError:
print(f"File '{file_path}' not found.")
return False
except Exception as e:
return False

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python json_validator.py <path_to_json_file>")
else:
validate_json_file(sys.argv[1])
51 changes: 51 additions & 0 deletions ssh_connect/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Interactive SSH Shell

A Python script to securely connect to a remote server via SSH and provide an interactive shell session.

## Introduction

This script allows to connect to a server using SSH, providing an interactive shell where user can run commands just like a normal SSH session. It supports both password and key-based authentication.

## Features

- Interactive Shell: Run commands interactively after connecting.
- Password & Key Auth: Supports both password and SSH key authentication.
- Local & Remote: Works for both local (`localhost`) and remote servers.
- Graceful Error Handling: Handles common SSH errors and connection issues.
- No Hardcoded Passwords: Prompts for password if not provided.

## Usage

### Prerequisites

- Python 3.x
- `paramiko` library (install with `pip install paramiko`)

### Running the Script

1. Save the script as `ssh_shell.py`.
2. Run the script:
- For password-based auth:
```bash
python ssh_shell.py <Server IP> username
```
- For key-based auth:
```bash
python ssh_shell.py <Server IP> username --key ~/.ssh/id_rsa
```
- For custom port:
```bash
python ssh_shell.py <Server IP> username --port 2222
```

### Output

- On successful connection, user will see 'Connected to localhost. Starting interactive shell. Type "exit" to quit.'
- On Failure, user will see 'SSH connection failed: {Exception}'
- On Socket connection Failure, user will see 'Socket error: {Error}'

## License

This project is licensed under the **MIT License**. Feel free to use, modify, and distribute it as needed.


66 changes: 66 additions & 0 deletions ssh_connect/ssh_connector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import paramiko
import getpass
import socket

def ssh_interactive_shell(hostname, username, password=None, key_filepath=None, port=22):
"""
Start an interactive SSH shell session.

Args:
hostname (str): Server IP or domain (use 'localhost' for same PC).
username (str): SSH username.
password (str, optional): SSH password. If not provided, will prompt.
key_filepath (str, optional): Path to private key file for key-based auth.
port (int, optional): SSH port. Defaults to 22.
"""
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
if key_filepath:
# Key-based authentication
private_key = paramiko.RSAKey.from_private_key_file(key_filepath)
client.connect(hostname, port=port, username=username, pkey=private_key)
else:
# Password-based authentication
if not password:
password = getpass.getpass(f"Enter SSH password for {username}@{hostname}: ")
client.connect(hostname, port=port, username=username, password=password)

print(f"Connected to {hostname}. Starting interactive shell. Type 'exit' to quit.")

# Start an interactive shell session
shell = client.invoke_shell()
while True:
command = input("$ ")
if command.lower().strip() in ["exit", "quit"]:
break
shell.send(command + "\n")
while not shell.recv_ready():
pass
output = shell.recv(4096).decode()
print(output, end="")

except paramiko.AuthenticationException:
print("Authentication failed. Check credentials.")
except paramiko.SSHException as e:
print(f"SSH connection failed: {e}")
except socket.error as e:
print(f"Socket error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
finally:
client.close()
print("🔌 Connection closed.")

if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Interactive SSH Shell")
parser.add_argument("hostname", help="Server IP or domain (use 'localhost' for same PC)")
parser.add_argument("username", help="SSH username")
parser.add_argument("--key", help="Path to private key file", default=None)
parser.add_argument("--port", type=int, help="SSH port", default=22)
args = parser.parse_args()

ssh_interactive_shell(args.hostname, args.username, key_filepath=args.key, port=args.port)