Вопрос пользователя:
Ответ нейросети:
import subprocess
import hashlib
import os
import time
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
stat_file = 'stat.txt'
ssh_user = config['SSH']['user']
ssh_host = config['SSH']['host']
ssh_key = config['SSH']['key']
def execute_ssh_command(command):
ssh_command = f'ssh -i {ssh_key} {ssh_user}@{ssh_host} "{command}"'
result = subprocess.run(ssh_command, shell=True, stdout=subprocess.PIPE)
return result.stdout.decode('utf-8')
def write_stat_to_file():
with open(stat_file, 'a') as file:
current_time = time.strftime('%Y-%m-%d %H:%M:%S')
file.write(f'{current_time}, {config["Files"]["file_count"]}, {config["Files"]["file_size"]}, {execute_ssh_command("cat /proc/loadavg")}\n')
def calculate_hash(file_path):
hash_object = hashlib.md5()
with open(file_path, "rb") as file:
for chunk in iter(lambda: file.read(4096), b""):
hash_object.update(chunk)
return hash_object.hexdigest()
def process_command(command, text):
if command == 'l':
result = execute_ssh_command('ls')
print(result)
write_stat_to_file()
return True
elif command == 'x':
# Add code to extract archive with paths
write_stat_to_file()
return True
elif command == 'h':
hash_value = calculate_hash(text)
crc32_value = execute_ssh_command(f'crc32 {text}').strip()
print(f'Hash: {hash_value}, CRC32: {crc32_value}')
write_stat_to_file()
return hash_value == crc32_value
else:
return False
# Tests
def test_list_files():
assert process_command('l', '') == True
def test_extract_archive():
assert process_command('x', '') == True
def test_calculate_hash():
file_path = 'test_file.txt'
with open(file_path, 'w') as file:
file.write('Test data')
assert process_command('h', file_path) == True
os.remove(file_path)
# Run tests
test_list_files()
test_extract_archive()
test_calculate_hash()
Примечание:
Напишите нам, если в вопросе есть ваши персональные данные ([email protected])
Обратите внимание: ответы, предоставляемые искусственным интеллектом, могут не всегда быть точными. Не рассчитывайте на них в критически важных областях, таких как медицина, юриспруденция, финансы или в вопросах, связанных с безопасностью. Для важных решений всегда обращайтесь к квалифицированным специалистам. Администрация сайта не несет ответственности за контент, сгенерированный автоматически.