Exec and pipes

Functionality and usage of the OpenNethome Server
Post Reply
TommyS
Posts: 8
Joined: Mon Oct 31, 2016 7:55 am

Exec and pipes

Post by TommyS »

Hi!
I'm trying to use pipes with exec but it looks like it is not working.
Have anyone else tried this?

Here is what I'm trying:
exec,/bin/echo -n -e '\x57\x02\x00\x04\x04\x04\x04\x00\' | netcat -i1 -w1 192.168.10.21 20000

/Tommy
stefangsbb
Site Admin
Posts: 313
Joined: Sun Nov 30, 2014 2:16 pm

Re: Exec and pipes

Post by stefangsbb »

Hi

I am not sure what you are exactly trying to do here, but one thing to notice is that the exec-command does currently not return any result values, so doing an exec with a echo does not do anything. Also you have to start with an echo if you want to pipe into netcat. Here is one example that turns on a lamp in my apartment:

Code: Select all

echo 'call,Lamp1,on' | netcat 192.168.1.130 8005
In this case I have a TCPCommandPort on port 8005

Hope this helps
/Stefan
TommyS
Posts: 8
Joined: Mon Oct 31, 2016 7:55 am

Re: Exec and pipes

Post by TommyS »

Hi Stefan,

Well, I echo a string of control characters into netcat. This lights up a lamp.
This works fine if I run it from command line, without the exec, of course.

So what you say is that ONH will not handle the echo pipe to netcat?

Well, that make sense because I tried to echo to a file (exec,echo > testfile.txt) and I could not get that to work either.

Do you have any other succession how I can sent a TCP command from ONH?

Thanks,
Tommy
stefangsbb
Site Admin
Posts: 313
Joined: Sun Nov 30, 2014 2:16 pm

Re: Exec and pipes

Post by stefangsbb »

Well, there are a few complications here. First of all, the TCPCommandPort does not allow the "exec" command. This is simple way to reduce the risk of malicious use of remote access.

You can go around this by first create a Scene-Item where the first action is the exec you want to do. Then you can call this scene via the TCPCommandPort:

Code: Select all

echo 'call,TestScene,Action' | netcat 192.168.1.130 8005
The second problem is that special characters like > often gets mangled in the process. The only really safe command to make is to call a specific script which does what you want to do. You should specify the absolute path to the script and make sure that the "nethome" user has execution rights on that script file.

Hope this helps
TommyS
Posts: 8
Joined: Mon Oct 31, 2016 7:55 am

Re: Exec and pipes

Post by TommyS »

Okey,
I think I have been a little unclear about what I want to accomplish :D
I want a CustomLamp item where i using exec to send shell commands.
Then I use the item as usual.
When then CustomLamp activates I want the command described before to be executed. And the same when inactivate.

So, at moment, i have no plan to change status on the lamp from outside ONH.

I running exec from a CustomLamp item and exec,netcat xxxxxx and that works fine. I can see that the TCP sesion is established.
The problem seems to be with the echo right now.

Should it be possible to pipe or redirect output to file?

I have considered the possibility to use scrips but it would be better if I have to. Otherwise I need three scripts for every lamp.

Thanks,
Tommy
stefangsbb
Site Admin
Posts: 313
Joined: Sun Nov 30, 2014 2:16 pm

Re: Exec and pipes

Post by stefangsbb »

Ok, now I am finally understanding what you are trying to do. The problem is still that all these special characters mess things up when Java tries to execute shell commands. Try to build a script that takes parameters without special characters. say for example that the script looks like this:

Code: Select all

/bin/echo -n -e '\x57\x02\x00\x$1\x$2\x04\x04\x00\' | /bin/netcat -i1 -w1 192.168.10.21 20000
Now you can call this script with parameters from the actions in the CustomLamp:

Code: Select all

exec,/home/pi/doit 04 04
I am not sure which parameters change between the lamps, but I hope you get the general idea
TommyS
Posts: 8
Joined: Mon Oct 31, 2016 7:55 am

Re: Exec and pipes

Post by TommyS »

Thank you!

Yes, I understand the script idea.
I will try that, it should work.

The thing was that I like it simple and it would be nice to do everything inside ONH :)

But one can't have everything.. :)

Thanks again!
TommyS
Posts: 8
Joined: Mon Oct 31, 2016 7:55 am

Re: Exec and pipes

Post by TommyS »

Hi!

Finally I finished this.

I was forced to write a script to make it work. I could not use passed in values to set values for the control characters in the script. In stead, I had to write at select/case rutin.
I call the script from ONH with CustomLamp. The OnCommand=exec,/home/nethome/LampTower.sh red blink and OffCommand=exec,/home/nethome/LampTower.sh red off

Tips! If you want to send a text string with exec, use single quotes ('), not dubbel quotes (")

The lamp I have is a Q-light tower with four colors and with Ethernet connection. Those of you that have been at Ikea or ICA have seen them at the self scanning :)

To give the knowledge for others, I include the script:

Code: Select all

Color=$1
Status=$2

case $Color in 
	red|Red)
    	case $Status in
        	on|On)
    			/bin/echo -n -e '\x57\x02\x01\x04\x04\x04\x04\x00' | netcat -i1 -w1 192.168.10.21 20000;;
            	blink|Blink)
            		/bin/echo -n -e '\x57\x02\x02\x04\x04\x04\x04\x00' | netcat -i1 -w1 192.168.10.21 20000;;
            	off|Off)
            		/bin/echo -n -e '\x57\x02\x00\x04\x04\x04\x04\x00' | netcat -i1 -w1 192.168.10.21 20000;;
        esac;;
     yellow|Yellow)
    	case $Status in
        	on|On)
    			/bin/echo -n -e '\x57\x02\x04\x01\x04\x04\x04\x00' | netcat -i1 -w1 192.168.10.21 20000;;
            	blink|Blink)
            		/bin/echo -n -e '\x57\x02\x04\x02\x04\x04\x04\x00' | netcat -i1 -w1 192.168.10.21 20000;;
            	off|Off)
            		/bin/echo -n -e '\x57\x02\x04\x00\x04\x04\x04\x00' | netcat -i1 -w1 192.168.10.21 20000;;
        esac;;
     green|Green)
    	case $Status in
        	on|On)
    			/bin/echo -n -e '\x57\x02\x04\x04\x01\x04\x04\x00' | netcat -i1 -w1 192.168.10.21 20000;;
           	 blink|Blink)
            		/bin/echo -n -e '\x57\x02\x04\x04\x02\x04\x04\x00' | netcat -i1 -w1 192.168.10.21 20000;;
           	off|Off)
            		/bin/echo -n -e '\x57\x02\x04\x04\x00\x04\x04\x00' | netcat -i1 -w1 192.168.10.21 20000;;
        esac;;
	blue|Blue)
    	case $Status in
        	on|On)
    			/bin/echo -n -e '\x57\x02\x04\x04\x04\x01\x04\x00' | netcat -i1 -w1 192.168.10.21 20000;;
            	blink|Blink)
            		/bin/echo -n -e '\x57\x02\x04\x04\x04\x02\x04\x00' | netcat -i1 -w1 192.168.10.21 20000;;
            	off|Off)
            		/bin/echo -n -e '\x57\x02\x04\x04\x04\x00\x04\x00' | netcat -i1 -w1 192.168.10.21 20000;;
        esac;;  
esac
/T
Last edited by TommyS on Thu Sep 14, 2017 6:37 pm, edited 1 time in total.
stefangsbb
Site Admin
Posts: 313
Joined: Sun Nov 30, 2014 2:16 pm

Re: Exec and pipes

Post by stefangsbb »

Ok, really fun to hear that you got it working!
And thanks a lot for sharing the solution, it can save a lot of time for others that want to do similar things.

Maybe it is not a bad thing that you collect all knowledge on how to control the lamps in one single script, otherwise that information would have been spread in a lot of different attributes all over ONH.
Post Reply