You don't show any code and error messages, so it is hard to suggest any concrete fixes to your script.
I recommend to write a Perl script that runs on the machine B, and copy that to the machine B and start that through SSH.
You could make the script B output JSON (or JSON::Tiny) to transfer the resulting data back in a convenient way.
|
|---|