示例#1
0
def logix_remote( count, svraddr, kwargs ):
  try:
    time.sleep(.25) # Wait for server to be established
    # Confirm that a known Register encodes as expected
    data			= cpppo.dotdict()
    data.enip			= {}
    data.enip.options		= 0
    data.enip.session_handle	= 0
    data.enip.status		= 0
    data.enip.sender_context	= {}
    data.enip.sender_context.input = bytearray( [0x00] * 8 )
    data.enip.CIP		= {}
    data.enip.CIP.register 	= {}
    data.enip.CIP.register.options 		= 0
    data.enip.CIP.register.protocol_version	= 1

    data.enip.input		= bytearray( enip.CIP.produce( data.enip ))
    data.input			= bytearray( enip.enip_encode( data.enip ))
    log.normal( "Register Request: %r" % data )
    
    assert bytes( data.input ) == rss_004_request

    # Try to Register a real session, followed by commands
    timeout			= 5

    begun			= cpppo.timer()
    cli				= client.client( host=svraddr[0], port=svraddr[1] )
    assert cli.writable( timeout=timeout )
    elapsed			= cpppo.timer() - begun
    log.normal( "Client Connected in  %7.3f/%7.3fs" % ( elapsed, timeout ))

    begun			= cpppo.timer()
    with cli:
        cli.register( timeout=timeout )
        data,elapsed		= client.await_response( cli, timeout=timeout )
    log.normal( "Client Register Rcvd %7.3f/%7.3fs: %r", elapsed, timeout, data )
    assert data is not None and 'enip.CIP.register' in data, "Failed to receive Register response"
    assert data.enip.status == 0, "Register response indicates failure: %s" % data.enip.status

    # Establish the EtherNet/IP "session handle" used by all further requests
    cli.session			= data.enip.session_handle

    start			= cpppo.timer()
    with cli:
        for _ in range( count ):
            begun		= cpppo.timer()
            cli.read( path=[{'symbolic': 'SCADA'}, {'element': 12}],
                      elements=201, offset=2, timeout=timeout )
            data,elapsed	= client.await_response( cli, timeout=timeout )
            log.normal( "Client ReadFrg. Rcvd %7.3f/%7.3fs: %r", elapsed, timeout, data )

    duration			= cpppo.timer() - start
    log.warning( "Client ReadFrg. Average %7.3f TPS (%7.3fs ea)." % ( count / duration, duration / count ))

    log.normal( "Signal shutdown w/ server.control in object %s", id( kwargs['server']['control'] ))
  finally:
    kwargs['server']['control'].done= True # Signal the server to terminate
示例#2
0
def logix_remote( count, svraddr, kwargs ):
  try:
    time.sleep(.25) # Wait for server to be established
    # Confirm that a known Register encodes as expected
    data			= cpppo.dotdict()
    data.enip			= {}
    data.enip.options		= 0
    data.enip.session_handle	= 0
    data.enip.status		= 0
    data.enip.sender_context	= {}
    data.enip.sender_context.input = bytearray( [0x00] * 8 )
    data.enip.CIP		= {}
    data.enip.CIP.register 	= {}
    data.enip.CIP.register.options 		= 0
    data.enip.CIP.register.protocol_version	= 1

    data.enip.input		= bytearray( enip.CIP.produce( data.enip ))
    data.input			= bytearray( enip.enip_encode( data.enip ))
    log.normal( "Register Request: %r" % data )
    
    assert bytes( data.input ) == rss_004_request

    # Try to Register a real session, followed by commands
    timeout			= 5

    begun			= cpppo.timer()
    cli				= client.client( host=svraddr[0], port=svraddr[1] )
    assert cli.writable( timeout=timeout )
    elapsed			= cpppo.timer() - begun
    log.normal( "Client Connected in  %7.3f/%7.3fs" % ( elapsed, timeout ))

    begun			= cpppo.timer()
    with cli:
        cli.register( timeout=timeout )
        data,elapsed		= client.await( cli, timeout=timeout )
    log.normal( "Client Register Rcvd %7.3f/%7.3fs: %r", elapsed, timeout, data )
    assert data is not None and 'enip.CIP.register' in data, "Failed to receive Register response"
    assert data.enip.status == 0, "Register response indicates failure: %s" % data.enip.status

    # Establish the EtherNet/IP "session handle" used by all further requests
    cli.session			= data.enip.session_handle

    start			= cpppo.timer()
    with cli:
        for _ in range( count ):
            begun		= cpppo.timer()
            cli.read( path=[{'symbolic': 'SCADA'}, {'element': 12}],
                      elements=201, offset=2, timeout=timeout )
            data,elapsed	= client.await( cli, timeout=timeout )
            log.normal( "Client ReadFrg. Rcvd %7.3f/%7.3fs: %r", elapsed, timeout, data )

    duration			= cpppo.timer() - start
    log.warning( "Client ReadFrg. Average %7.3f TPS (%7.3fs ea)." % ( count / duration, duration / count ))

    log.normal( "Signal shutdown w/ server.control in object %s", id( kwargs['server']['control'] ))
  finally:
    kwargs['server']['control'].done= True # Signal the server to terminate
示例#3
0
 def scan(self):
     timeout = 1.0
     addr = self.sin_addr
     with client.client(host=addr, udp=True, broadcast=True) as conn:
         conn.list_identity(timeout=timeout)
         while True:
             response, elapsed = client. await (conn, timeout=timeout)
             if response:
                 self.prop = response
             else:
                 break  # No response (None) w'in timeout or EOF ({})
示例#4
0
def list_identity(address=('255.255.255.255', enip.address[1]),
                  broadcast=True,
                  timeout=1.0,
                  udp=True):
    """Yields a sequence of 0 or more List Identity responses from the target IP or Broadcast
    (address,port) address.  Defaults to UDP/IP.

    """
    with client.client(host=address[0],
                       port=address[1],
                       broadcast=broadcast,
                       udp=udp) as conn:
        begin = cpppo.timer()
        conn.list_identity(timeout=timeout)
        while True:
            used = cpppo.timer() - begin
            response, elapsed = client.await_response(conn,
                                                      timeout=max(
                                                          0, timeout - used))
            if not response:
                break  # No response (None) w'in timeout or EOF ({})
            yield response
示例#5
0
"""python -m cpppo.server.enip.list_identity_simple <hostname>

Returns any List Identity responses from the given hostname or IP address (default:
255.255.255.255), received before timeout (default: 1.0 second) expires.

"""

from __future__ import print_function

import sys

from cpppo.server import enip
from cpppo.server.enip import client

timeout			= 1.0
host			= sys.argv[1] if sys.argv[1:] else '255.255.255.255'
with client.client( host=host, udp=True, broadcast=True ) as conn:
    conn.list_identity( timeout=timeout )
    while True:
        response,elapsed= client.await( conn, timeout=timeout )
        if response:
            print( enip.enip_format( response ))
        else:
            break # No response (None) w'in timeout or EOF ({})
示例#6
0
def logix_remote( count, svraddr, kwargs ):
    time.sleep(.25)
    data			= cpppo.dotdict()
    data.enip			= {}
    data.enip.options		= 0
    data.enip.session_handle	= 0
    data.enip.status		= 0
    data.enip.sender_context	= {}
    data.enip.sender_context.input = bytearray( [0x00] * 8 )
    	#array.array( cpppo.type_bytes_array_symbol, "\x00" * 8 )
    data.enip.CIP		= {}
    data.enip.CIP.register 	= {}
    data.enip.CIP.register.options 		= 0
    data.enip.CIP.register.protocol_version	= 1

    data.enip.input		= bytearray( enip.CIP.produce( data.enip ))
    data.input			= bytearray( enip.enip_encode( data.enip ))
    log.normal( "Register Request: %r" % data )
    
    assert bytes( data.input ) == rss_004_request


    timeout			= 5

    begun			= cpppo.timer()
    cli				= client.client( host=svraddr[0], port=svraddr[1] )
    assert cli.writable( timeout=timeout )
    elapsed			= cpppo.timer() - begun
    log.normal( "Client Connected in  %7.3f/%7.3fs" % ( elapsed, timeout ))

    begun			= cpppo.timer()
    request			= cli.register( timeout=timeout )
    elapsed			= cpppo.timer() - begun
    log.normal( "Client Register Sent %7.3f/%7.3fs: %r" % ( elapsed, timeout, request ))
    for data in cli:
        elapsed			= cpppo.timer() - begun
        log.detail( "Client Register Resp %7.3f/%7.3fs: %r" % ( elapsed, timeout, data ))
        if data is None:
            if elapsed <= timeout:
                cli.readable( timeout=timeout - elapsed )
                continue
        break
    elapsed			= cpppo.timer() - begun
    log.normal( "Client Register Rcvd %7.3f/%7.3fs: %r" % ( elapsed, timeout, data ))
    assert data is not None and 'enip.CIP.register' in data, "Failed to receive Register response"
    assert data.enip.status == 0, "Register response indicates failure: %s" % data.enip.status

    cli.session			= data.enip.session_handle


    start			= cpppo.timer()
    for _ in range( count ):
        begun			= cpppo.timer()
        request			= cli.read( path=[{'symbolic': 'SCADA'}, {'element': 12}],
                                                elements=1, offset=0, timeout=timeout )
        elapsed			= cpppo.timer() - begun
        log.normal( "Client ReadFrg. Sent %7.3f/%7.3fs: %r" % ( elapsed, timeout, request ))
        for data in cli:
            elapsed		= cpppo.timer() - begun
            log.detail( "Client ReadFrg. Resp %7.3f/%7.3fs: %r" % ( elapsed, timeout, data ))
            if data is None:
                if elapsed <= timeout:
                    cli.readable( timeout=timeout - elapsed )
                    continue
            break
        elapsed			= cpppo.timer() - begun
        log.normal( "Client ReadFrg. Rcvd %7.3f/%7.3fs: %r" % ( elapsed, timeout, data ))

    duration			= cpppo.timer() - start
    log.warning( "Client ReadFrg. Average %7.3f TPS (%7.3fs ea)." % ( count / duration, duration / count ))

    kwargs['server'].control.done= True
示例#7
0
def logix_remote(count, svraddr, kwargs):
    time.sleep(.25)
    data = cpppo.dotdict()
    data.enip = {}
    data.enip.options = 0
    data.enip.session_handle = 0
    data.enip.status = 0
    data.enip.sender_context = {}
    data.enip.sender_context.input = bytearray([0x00] * 8)
    #array.array( cpppo.type_bytes_array_symbol, "\x00" * 8 )
    data.enip.CIP = {}
    data.enip.CIP.register = {}
    data.enip.CIP.register.options = 0
    data.enip.CIP.register.protocol_version = 1

    data.enip.input = bytearray(enip.CIP.produce(data.enip))
    data.input = bytearray(enip.enip_encode(data.enip))
    log.normal("Register Request: %r" % data)

    assert bytes(data.input) == rss_004_request

    timeout = 5

    begun = cpppo.timer()
    cli = client.client(host=svraddr[0], port=svraddr[1])
    assert cli.writable(timeout=timeout)
    elapsed = cpppo.timer() - begun
    log.normal("Client Connected in  %7.3f/%7.3fs" % (elapsed, timeout))

    begun = cpppo.timer()
    request = cli.register(timeout=timeout)
    elapsed = cpppo.timer() - begun
    log.normal("Client Register Sent %7.3f/%7.3fs: %r" %
               (elapsed, timeout, request))
    for data in cli:
        elapsed = cpppo.timer() - begun
        log.detail("Client Register Resp %7.3f/%7.3fs: %r" %
                   (elapsed, timeout, data))
        if data is None:
            if elapsed <= timeout:
                cli.readable(timeout=timeout - elapsed)
                continue
        break
    elapsed = cpppo.timer() - begun
    log.normal("Client Register Rcvd %7.3f/%7.3fs: %r" %
               (elapsed, timeout, data))
    assert data is not None and 'enip.CIP.register' in data, "Failed to receive Register response"
    assert data.enip.status == 0, "Register response indicates failure: %s" % data.enip.status

    cli.session = data.enip.session_handle

    start = cpppo.timer()
    for _ in range(count):
        begun = cpppo.timer()
        request = cli.read(path=[{
            'symbolic': 'SCADA'
        }, {
            'element': 12
        }],
                           elements=1,
                           offset=0,
                           timeout=timeout)
        elapsed = cpppo.timer() - begun
        log.normal("Client ReadFrg. Sent %7.3f/%7.3fs: %r" %
                   (elapsed, timeout, request))
        for data in cli:
            elapsed = cpppo.timer() - begun
            log.detail("Client ReadFrg. Resp %7.3f/%7.3fs: %r" %
                       (elapsed, timeout, data))
            if data is None:
                if elapsed <= timeout:
                    cli.readable(timeout=timeout - elapsed)
                    continue
            break
        elapsed = cpppo.timer() - begun
        log.normal("Client ReadFrg. Rcvd %7.3f/%7.3fs: %r" %
                   (elapsed, timeout, data))

    duration = cpppo.timer() - start
    log.warning("Client ReadFrg. Average %7.3f TPS (%7.3fs ea)." %
                (count / duration, duration / count))

    kwargs['server'].control.done = True