Compare commits

...
8 Commits
Author SHA1 Message Date
marc 360364c80d master 2022-05-21 23:54:55 +02:00
marc b0c0511126 ssh 2022-05-21 23:46:59 +02:00
marc 02e5ff1a14 pipfile.lock 3.8.13 2022-05-21 23:23:55 +02:00
marc e66e94f094 bye brew 2022-05-21 23:07:40 +02:00
marc b4d83a255f ok 2022-05-21 23:06:39 +02:00
marc c74b028d82 Merge branch 'master' into ssh 2022-04-02 17:49:18 +02:00
marc f02603bc20 ssh start 2022-04-02 17:42:13 +02:00
marc b06e8dcdc0 adding pxssh 2022-04-02 17:06:10 +02:00
3 changed files with 82 additions and 6 deletions
-4
View File
@@ -19,7 +19,3 @@ setup:
pipenv install
recreate:
docker-compose -f docker-compose.yml up --force-recreate dev
fixwithbrew:
brew install python@3.8
sudo ln -sf /home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.12_1/bin/python3.8 /usr/bin/python3.8
Generated
+1 -2
View File
@@ -745,7 +745,6 @@
"sha256:ea6841bc3a76fa4942ce00f3bda7d436fda21e2d91602b9e21b7ca9ecab8f3ff",
"sha256:f8851ab9041756003119368c1e6cd0b9c631f46d686b3904b18c0139f4419f80"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
"version": "==1.4.0"
},
"pyopenssl": {
@@ -1004,7 +1003,7 @@
"sha256:44ece4d53fb1706f667c9bd1c648f5469a2ec925fcf3a776667042d645472c14",
"sha256:aabaf16477806a5e1dd19aa41f8c2b7950dd3c746362d7e3223dbe6de6ac448e"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'",
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4.0'",
"version": "==1.26.9"
},
"wikipedia": {
+81
View File
@@ -0,0 +1,81 @@
#!/usr/bin/env python
"""Control ssh sessions."""
import typing
import discord
from pexpect import pxssh
from discord.ext import commands
from discord.ext.commands.core import has_role
import pexpect
SSH_ROLE = "Ssh"
class Ssh(commands.Cog):
"""Control ssh sessions."""
def __init__(self, bot: commands.Bot):
"""Initialize the cog with the bot."""
self.bot = bot
self.color: discord.Color = discord.Color.purple()
self.thumbnail: str = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/GNOME_Terminal_icon_2019.svg/1200px-GNOME_Terminal_icon_2019.svg.png"
self.timeout = 3600
self.shells: typing.Dict[int, pexpect.pty_spawn.spawn] = {}
@has_role(SSH_ROLE)
@commands.command()
async def ssh(self, ctx: commands.Context, hostname: str, port: int=0):
"""Run a command in a ssh session"""
username=""
await create_role_if_missing(ctx, SSH_ROLE, self.color)
embed = discord.Embed(color=self.color)
embed.set_thumbnail(url=self.thumbnail)
embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar)
embed.description = self.shell_path
shell = pxssh.pxssh()
shell.login(hostname, username, password)
self.shells[ctx.author.id] = shell
shell.sendline("echo $HOSTNAME")
hostname = shell.before.decode("utf-8")
embed.title = f"Connected to {hostname}"
await ctx.send(embed=embed)
@commands.command(name="shells")
async def list_shells(self, ctx: commands.Context):
"""List ."""
if len(self.shells) == 0:
return await ctx.send("> No active ssh connexion.")
embed = discord.Embed(title="Active ssh connexions:", color=self.color)
embed.set_thumbnail(url=self.thumbnail)
shell_users = []
for shell_user in map(self.bot.fetch_user, self.shells.keys()):
shell_users.append(f"- {await shell_user}")
embed.description = "\n".join(shell_users)
await ctx.send(embed=embed)
@commands.Cog.listener(name="on_message")
async def on_message(self, msg: discord.Message):
"""Read all messages"""
if msg.author == self.bot.user:
return
if msg.content.startswith(self.bot.command_prefix):
return
if msg.author.id in self.shells:
shell = self.shells[msg.author.id]
shell.sendline(msg.content)
shell.prompt()
output = shell.before.decode("utf-8")
print(output)
await msg.channel.send(output)
# async def cog_common_error(self, ctx, error):
# """Close all shell sessions."""
# for shell in self.shells.values():
# shell.close()
def setup(bot):
"""Setup the Shell cog."""
bot.add_cog(Ssh(bot))