Esempio n. 1
0
        else:
            try:
                packet = send[packetName]
            except PacketNotFound:
                pass
        if packet is None:
            print("Unknown packet name: %r" % packetName)
            continue
        packets.append([packet.__class__, data, poll])

    if options.verbose:
        print("Connecting to %(host)s:%(port)s" % {
            'host': options.host,
            'port': options.port
        })
    connection = AdminClient()
    connection.packet_recv += formatter_recv
    connection.packet_send += formatter_send
    connection.configure(password=options.password,
                         host=options.host,
                         port=options.port)
    if not connection.connect():
        print("Unable to connect to %(host)s:%(port)s" % {
            'host': options.host,
            'port': options.port
        })
        sys.exit()

    connection.poll(0.2)
    connection.poll(0.2)
        packet = None
        if isinstance(packetName, basestring):
            packet = send.get_by_name(packetName)
        else:
            try:
                packet = send[packetName]
            except PacketNotFound:
                pass
        if packet is None:
            print("Unknown packet name: %r" % packetName)
            continue
        packets.append([packet.__class__, data, poll])

    if options.verbose:
        print("Connecting to %(host)s:%(port)s" % {'host': options.host, 'port': options.port})
    connection = AdminClient()
    connection.packet_recv += formatter_recv
    connection.packet_send += formatter_send
    connection.configure(password=options.password, host=options.host, port=options.port)
    if not connection.connect():
        print("Unable to connect to %(host)s:%(port)s" % {'host': options.host, 'port': options.port})
        sys.exit()

    connection.poll(0.2)
    connection.poll(0.2)

    for packet, data, poll in packets:
        connection.send_packet(packet, **data)
        if not isinstance(poll, (list, tuple)):
            if isinstance(poll, float) and poll > 0:
                poll = [poll,]
                  default=True)

if __name__ == "__main__":
    options, args = parser.parse_args()
    if options.password is None:
        parser.error("--password is required")
    if len(args) < 1:
        parser.error("At least one command is required")

    if options.verbose:
        print("Connecting to %(host)s:%(port)s" % {
            'host': options.host,
            'port': options.port
        })

    connection = AdminClient()
    connection.settimeout(0.4)
    connection.configure(password=options.password,
                         host=options.host,
                         port=options.port)
    connection.connect()
    failed = False
    try:
        protocol_response = connection.recv_packet()
        welcome_response = connection.recv_packet()
    except socket.error:
        failed = True

    if protocol_response is None or welcome_response is None:
        failed = True
                  help="Output responses only (use in combination with -q)", default=False)
parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
                  help="be verbose (default)", default=True)


if __name__ == "__main__":
    options, args = parser.parse_args()
    if options.password is None:
        parser.error("--password is required")
    if len(args) < 1:
        parser.error("At least one command is required")

    if options.verbose:
        print("Connecting to %(host)s:%(port)s" % {'host': options.host, 'port': options.port})

    connection = AdminClient()
    connection.settimeout(0.4)
    connection.configure(password=options.password, host=options.host, port=options.port)
    connection.connect()
    failed = False
    try:
        protocol_response = connection.recv_packet()
        welcome_response = connection.recv_packet()
    except socket.error:
        failed = True

    if protocol_response is None or welcome_response is None:
        failed = True

    if failed:
        print("Unable to connect to %(host)s:%(port)s" % {'host': options.host, 'port': options.port})
Esempio n. 5
0
def rcon(command):
    connection = AdminClient()
    connection.settimeout(0.4)
    connection.configure(password=RCON_PASSWORD,
                         host=SERVER_IP,
                         port=ADMIN_PORT)
    connection.connect()
    failed = False
    try:
        protocol_response = connection.recv_packet()
        welcome_response = connection.recv_packet()
    except socket.error:
        failed = True

    if protocol_response is None or welcome_response is None:
        failed = True

    result = []
    if failed:
        pass
    else:
        connection.send_packet(AdminRcon, command=command)

        cont = True
        while cont:
            packets = connection.poll()
            if packets is None or packets is False:
                break
            cont = len(packets) > 0
            for packet, data in packets:
                if packet == ServerRcon:
                    result += [data['result']]
                elif packet == ServerRconEnd:
                    cont = False
                    break
                else:
                    pass
    connection.disconnect()

    return result