import subprocess
import os
def get_open_fds():
'''
return the number of open file descriptors for current process
.. warning: will only work on UNIX-like os-es.
'''
pid = os.getpid()
procs = subprocess.check_output(["lsof", '-w', '-Ff', "-p", str(pid)])
nprocs = len(list(filter(lambda s: s and s[0] == 'f' and s[1:].isdigit(), procs.decode().split('\n'))))
return nprocs
def f1(s='111'):
f=open('test', 'a+')
f.write(s)
f1()
print(get_open_fds())
f1()
print(get_open_fds())
f1()
print(get_open_fds())
f1()
print(get_open_fds())
import asyncio
from aiogram import Bot, Dispatcher, types, executor
import cfg
bot = Bot(token=cfg.token)
dp = Dispatcher(bot)
async def func(n):
a=0.0
for _ in range(n):
await asyncio.sleep(0.0000000000001)
a += 0.01
return f'f1 done\n\n{a}'
@dp.message_handler(commands=['cmd1',])
async def send_welcome(message: types.Message):
result = await func(10000000000000)
await message.reply(result)
@dp.message_handler(commands=['cmd2',])
async def send_welcome2(message: types.Message):
a=0.0
for _ in range(5000):
a += 0.1
result = f'f2 done\n\n{a}'
await message.reply(result)
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)
import cfg
import telebot
import threading
import time
import random
bot = telebot.TeleBot(cfg.token)
@bot.message_handler(commands=["cmd1"])
def task(message):
bot.send_message(message.chat.id, "Хорошо, я начинаю выполнять задание.")
thread = threading.Thread(target=do_task, args=(message.chat.id,))
thread.start()
def do_task(chat_id):
t = random.randint(1,5)
time.sleep(t)
bot.send_message(chat_id, f"Я закончил задание за {t} секунд.")
bot.polling()
import paramiko
# создаем объект SSH клиента
ssh = paramiko.SSHClient()
# автоматически добавляем хост в список известных хостов
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# подключаемся к серверу
ssh.connect('192.168.0.210', username='root', password='')
# выполнение команды
stdin, stdout, stderr = ssh.exec_command('uname -a')
# вывод результатов
print(stdout.read().decode())
# закрываем соединение
ssh.close()
Linux NAS 5.10.0-23-amd64 #1 SMP Debian 5.10.179-1 (2023-05-12) x86_64 GNU/Linux
например так
sudo su - user1
или так
sudo bash
su - user1
sudo ls /home/user итп тоже сработает
можно разрешить судо юзеру только определенные команды. например можно разрешить запускать только один скрипт который он не может изменять