Monday, January 2, 2017

let's imagine we are beeing "bugged" by the security server Black Box Network Services, LES8164A Dual 10/100/1000 Secure Console Server, 16-Port

Embedded Freaks..

August 13, 2008

Reading from serial port (using rxtx)

Filed under: java — Tags:  — kunilkuda @ 9:29 am
Once you’ve got the serial InputStream, reading would be easy. Here’s an example of code to read the serial port using InputStream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * Buffer to hold the reading
 */
private byte[] readBuffer = new byte[400];
private void readSerial() {
    try {
        int availableBytes = inStream.available();
        if (availableBytes > 0) {
            // Read the serial port
            inStream.read(readBuffer, 0, availableBytes);
            // Print it out
            System.out.println(
                    new String(readBuffer, 0, availableBytes));
        }
    } catch (IOException e) {
    }
}
But the real problem comes from the place where you should place the code.

You can put inside the SerialPortEvent.DATA_AVAILABLE: event, like this:
1
2
3
4
5
6
7
8
9
private class SerialEventHandler implements SerialPortEventListener {
    public void serialEvent(SerialPortEvent event) {
        switch (event.getEventType()) {
            case SerialPortEvent.DATA_AVAILABLE:
                readSerial();
                break;
        }
    }
}
Don’t forget to register the event handler, before you run the application
1
2
3
4
5
6
7
8
9
10
11
12
/**
 * Set the serial event handler
 */
private void setSerialEventHandler(SerialPort serialPort) {
    try {
        // Add the serial port event listener
        serialPort.addEventListener(new SerialEventHandler());
        serialPort.notifyOnDataAvailable(true);
    } catch (TooManyListenersException ex) {
        System.err.println(ex.getMessage());
    }
}
The result is the read procedure is only called when new data is accepted.
The other way to read the serial port data continously is by using other thread to read:
1
2
3
4
5
6
7
private class ReadThread implements Runnable {
    public void run() {
        while(true) {
            readSerial();
        }
    }
}
Here’s how you start the thread:
1
2
3
public void setSerialListener() {
    new Thread(new ReadThread()).start();
}

Conclusion

So, which one that is better, put the reading inside different thread or in the event handler ? Well..I don’t know about it yet. I’ll post it once I found the answer.

https://embeddedfreak.wordpress.com/2008/08/13/reading-from-serial-port-using-rxtx/

So I'll explain..SCADA systems, or multicast configurations on CISCO routers, have not more by default ways to go over and over routing packets...but I want more than that solution..what I want is to READ all vrf ip PID's...but more important of course than reaching to PLC's circuits, is to have total control on the OBJECT_TYPE...meaning the logger keys.

eAPI Python script to look at ARP entries per VRF

I needed to see all the different ARP entries in each VRF, so I wrote up this little script to do just that. The ‘show vrf’ command in eAPI has not yet been converted to JSON, so I had to do some text parsing to get the VRF names, then use those names to grab the ARP entries. On line 4 you’ll see that I use the ‘text’ option for the output of the JSON reply. That allows me to run a command that hasn’t been converted yet and get the raw text output:
response = switch.runCmds( 1, ["show vrf"], "text" )
The output looks like this:
"output": "   Vrf         RD            Protocols       State         Interfaces \n----------- ------------- --------------- ---------------- ---------- \n   test        100:100       ipv4            no routing               \n   test2       101:101       ipv4            no routing               \n   test3       102:102       ipv4            no routing               \n\n"
Or in a more familiar format:
   Vrf         RD            Protocols       State         Interfaces
----------- ------------- --------------- ---------------- ----------
   test        100:100       ipv4            no routing
   test2       101:101       ipv4            no routing
   test3       102:102       ipv4            no routing
Then I take the output and use splitlines() to take each line (separated by newline) and insert them into a list:
lines = response[0]['output'].splitlines()
Now I iterate through each entry of the ‘show ip vrf’ output and issue a ‘show ip arp vrf’ with the VRF name. I use the range() function, starting at the 3rd line (since the first two are just header lines), and go through the end of the list. Then I use the split() method to split each line on whitespace, taking the first entry which corresponds to the VRF name. Finally, I can use that VRF name in my command.
for i in range(2, len(lines) - 1):
  vrfname = lines[i].split()[0]
  command = "show ip arp vrf " + vrfname
Here’s the script in its entirety:
from jsonrpclib import Server
switch = Server( "https://admin:admin@leaf1/command-api" )
response = switch.runCmds( 1, ["show vrf"], "text" )
lines = response[0]['output'].splitlines()
for line in lines:
print line
for i in range(2, len(lines) - 1):
vrfname = lines[i].split()[0]
command = "show ip arp vrf " + vrfname
response = switch.runCmds( 1, [command] )
print vrfname
print response[0]