B4J Question PHP - some guidance required

JackKirk

Well-Known Member
Licensed User
Longtime User
I am trying to programmatically control a Netonix WISP switch ( https://www.netonix.com/wisp-switch/ws-8-150-dc.html ) at a remote location.

Specifically I want to be able to turn PoE on selected ports on and off under certain circumstances from an AWS EC2 instance.

But I am not having much luck doing it with B4J.

I've made an earlier probe on this subject in the forums here:

https://www.b4x.com/android/forum/threads/sshj-ssh-scp-sftp-for-java.88615/page-2#post-586467

But I am reluctantly coming to the conclusion that it might not be easy or even possible to do with B4J alone.

After much digging around I have found PHP code here:

https://forum.netonix.com/viewtopic.php?f=8&t=2657&p=18570&hilit=php+poe#p18579

that looks like it could be adapted to do what I want:

B4X:
#!/bin/php
<?php
function help() {
 print "\r\n** Syntax error - You must supply all of the following options:\r\n\r\n";
 print " h <host> The hostname or IP address for the Netonix\r\n";
 print " n <portnumber> The port number that you wish to alter\r\n";
 print " v <value> The value for the PoE, off, 24V, 24VH, 48V, 48VH\r\n";
 print " u <username> The username to use for logging in\r\n";
 print " p <password> The Password used to log into the switch\r\n";
 print "\r\n";
 exit(1);
}

$options = getopt("h:u:p:n:v:");
if((!isset($options['h'])) || (!isset($options['u'])) || (!isset($options['p'])) || (!isset($options['n'])) || (!isset($options['v']))) {
 help();
}else{
 $host = ($options['h']);
 $username = ($options['u']);
 $password = ($options['p']);
 $port = ($options['n']);
 $poe = ($options['v']);
}
print "Setting PoE to $poe on port $port of $host\r\n";

// Setup the SSH connection
$connection = ssh2_connect($host);
ssh2_auth_password($connection, $username, $password);

$shell=ssh2_shell($connection, 'switch');
fwrite( $shell, 'conf'.PHP_EOL);
fwrite( $shell, 'interface port '.$port.PHP_EOL);
fwrite( $shell, 'poe '.$poe.PHP_EOL);
fwrite( $shell, 'exit'.PHP_EOL);
fwrite( $shell, 'exit'.PHP_EOL);
sleep(1); // wait for the confirm prompt
fwrite( $shell, PHP_EOL); // send ENTER to confirm
fwrite( $shell, 'exit'.PHP_EOL); // exit the CLI which will terminate the SSH session

// Turn on blocking so it doesn't disconnect between multiple commands
stream_set_blocking($shell, true);

// Display any output
echo 'Output: '.stream_get_contents($shell)."\r\n";

// Close the connection
fclose($shell);

?>

My untrained eye sees statements like ssh2_shell in this PHP that I haven't seen in any other environment so maybe PHP is unavoidable?

However, I have never touched PHP in my life and would prefer not to at this point.

I would be interested in any suggestions, guidance, pointers etc of how to proceed.

Thanks in anticipation...
 

OliverA

Expert
Licensed User
Longtime User
It looks like the library wrap does not expose the startShell(), getInputStream(), getOutputStream() and getErrorStream() methods. With those in hand, you might be able to reproduce the above.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I would be interested in any suggestions, guidance, pointers etc of how to proceed.
maybe you can get using the jkSSH2 Library.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
jkSSH2 Library

That library suffers from the same issue. Looks like the project it is based on has been dormant since end of 2006 (http://www.ganymed.ethz.ch/ssh2/). It's interesting that the code base looks very similar to the newer code base of the sshj library (https://github.com/hierynomus/sshj). Another library that is build on interactive shell communication (and it's based on sshj) and may be worth wrapping is ExpectIt (https://github.com/Alexey1Gavrilov/expectit).
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Thanks for all the interest and suggestions.

Firing another rocket blind into the ether...

From my PuTTy and associated plink rummaging so far, I can reproduce, in PuTTy, this bit of the above PHP code:

B4X:
$shell=ssh2_shell($connection, 'switch');
fwrite( $shell, 'conf'.PHP_EOL);
fwrite( $shell, 'interface port '.$port.PHP_EOL);
fwrite( $shell, 'poe '.$poe.PHP_EOL);
fwrite( $shell, 'exit'.PHP_EOL);
fwrite( $shell, 'exit'.PHP_EOL);
sleep(1); // wait for the confirm prompt
fwrite( $shell, PHP_EOL); // send ENTER to confirm

the "switch" shell provides a new command line interface on which you drop "conf", "interface port .." etc

This delivers the desired result - except I can't automate it.

It appears plink does not work because it is not interactive and therefor you can't have a command line interface.

I guess this reinforces Oliver A's first post in this thread.
 
Upvote 0
Top