-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
585 additions
and
344 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import commune as c | ||
import streamlit as st | ||
from typing import * | ||
import json | ||
import paramiko | ||
|
||
class SSH(c.Module): | ||
@classmethod | ||
def cmd(cls, *cmd_args, | ||
host:str= None, | ||
cwd:str=None, | ||
verbose=False, | ||
sudo=False, | ||
key=None, | ||
timeout=10, | ||
generator=True, | ||
**kwargs ): | ||
"""s | ||
Run a command on a remote server using Remote. | ||
:param host: Hostname or IP address of the remote machine. | ||
:param port: Remote port (typically 22). | ||
:param username: Remote username. | ||
:param password: Remote password. | ||
:param command: Command to be executed on the remote machine. | ||
:return: Command output. | ||
""" | ||
command = ' '.join(cmd_args).strip() | ||
|
||
if command.startswith('c '): | ||
command = command.replace('c ', cls.executable_path + ' ') | ||
|
||
if cwd != None: | ||
command = f'cd {cwd} && {command}' | ||
|
||
# Create an Remote client instance. | ||
client = paramiko.SSHClient() | ||
|
||
# Automatically add the server's host key (this is insecure and used for demonstration; | ||
# in production, you should have the remote server's public key in known_hosts) | ||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | ||
|
||
# Connect to the remote server | ||
client.connect(host['host'], | ||
port=host['port'], | ||
username=host['user'], | ||
password=host['pwd']) | ||
|
||
if sudo and host['user'] != "root": | ||
command = "sudo -S -p '' %s" % command | ||
|
||
stdin, stdout, stderr = client.exec_command(command) | ||
|
||
try: | ||
if sudo: | ||
stdin.write(host['pwd'] + "\n") | ||
stdin.flush() | ||
color = c.random_color() | ||
# Print the output of ls command | ||
outputs = {'error': '', 'output': ''} | ||
|
||
if generator: | ||
for line in stdout.readlines(): | ||
if verbose: | ||
c.print(f'[bold]{host["host"]}[/bold]', line.strip('\n'), color=color) | ||
yield line | ||
|
||
for line in stdout.readlines(): | ||
if verbose: | ||
c.print(f'[bold]{host_name}[/bold]', line.strip('\n'), color=color) | ||
outputs['output'] += line | ||
|
||
for line in stderr.readlines(): | ||
if verbose: | ||
c.print(f'[bold]{host_name}[/bold]', line.strip('\n')) | ||
outputs['error'] += line | ||
|
||
if len(outputs['error']) == 0: | ||
outputs = outputs['output'] | ||
|
||
# stdin.close() | ||
# stdout.close() | ||
# stderr.close() | ||
# client.close() | ||
except Exception as e: | ||
c.print(e) | ||
return outputs |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.