Bash communication over tcp/upd

Moderator: Mattk

Post Reply
User avatar
Hathor27
Expert Boarder
Expert Boarder
Posts: 126
Joined: Thu May 05, 2011 9:33 pm
Location: Switzerland
Contact:

Bash communication over tcp/upd

Post by Hathor27 »

Hi there,

these days I try to communicate with a tcp device: I need to write a command to a specific port and then read its answer from the same port to get its data - how can I perform that?

I read about the command line redirector

Code: Select all

echo ThisCommand > /dev/tcp/ServerIP/ServerPort
but this kind of redirector won't work on my ALIX - any idea why, or how I could get it run?

Or are there any other standard tcp comminication commands to be used in bash scripts?

Thanks a lot in advance
Best Regards
Hathor27

------------------------------------------------------------
Image
http://untersiggenthal.meteodata.ch
wfpost
Platinum Boarder
Platinum Boarder
Posts: 591
Joined: Thu Jun 12, 2008 2:24 pm
Location: HONSOLGEN
Contact:

Re: Bash communication over tcp/upd

Post by wfpost »

netcat
http://de.wikipedia.org/wiki/Netcat
http://linux.die.net/man/1/nc

e.g. this reads and writes the actual meteohub sensordata into a local file

nc localhost 5558 > sensordata.txt
User avatar
Hathor27
Expert Boarder
Expert Boarder
Posts: 126
Joined: Thu May 05, 2011 9:33 pm
Location: Switzerland
Contact:

Re: Bash communication over tcp/upd

Post by Hathor27 »

wfpost wrote:nc localhost 5558 > sensordata.txt
Thank you wfpost, but how do I have to proceed if I first have to write a command like getadc 1 to my device?
Like

Code: Select all

Put MyDevice 50290 "getadc 1"
nc MyDevice 50290 > value.txt
Best Regards
Hathor27

------------------------------------------------------------
Image
http://untersiggenthal.meteodata.ch
wfpost
Platinum Boarder
Platinum Boarder
Posts: 591
Joined: Thu Jun 12, 2008 2:24 pm
Location: HONSOLGEN
Contact:

Re: Bash communication over tcp/upd

Post by wfpost »

^^^^ Don´t know - maybe like that
What values are you trying to get? From a solar-panel?

Code: Select all

echo getadc 1|nc xxx.xxx.xxx.xxx 50290 -w 1
User avatar
Hathor27
Expert Boarder
Expert Boarder
Posts: 126
Joined: Thu May 05, 2011 9:33 pm
Location: Switzerland
Contact:

Re: Bash communication over tcp/upd

Post by Hathor27 »

wfpost wrote:^^^^ Don´t know - maybe like that
What values are you trying to get? From a solar-panel?

Code: Select all

echo getadc 1|nc xxx.xxx.xxx.xxx 50290 -w 1
Wow! That works in one go - YMMD :D

I got an AVR NET-IO to read analog values over tcp: http://www.mikrocontroller.net/articles ... von_Pollin (sorry for it's only in German). With this I want to read and log all relevant temperatures of my heating system.

Thanks a lot so far
Best Regards
Hathor27

------------------------------------------------------------
Image
http://untersiggenthal.meteodata.ch
User avatar
Hathor27
Expert Boarder
Expert Boarder
Posts: 126
Joined: Thu May 05, 2011 9:33 pm
Location: Switzerland
Contact:

Re: Bash communication over tcp/upd

Post by Hathor27 »

It's me again: As I found now, this works fine from command line, but doesn't work as plug-in...

Code: Select all

#! /bin/sh
#
# runs as /home/meteohub/plugin_raw-net-io.sh
#
while :
do
  # reads raw data of net-io.php
  echo getadc 1 | nc MyDevice 50290 -w 1| gawk 'BEGIN {FS=" "} {printf "t1 %d\n", $1 }'
  echo getadc 2 | nc MyDevice 50290 -w 1| gawk 'BEGIN {FS=" "} {printf "t2 %d\n", $1 }'
  
  # make sure output gets flushed 
  sync
  # wait 1 minutes for next read
  sleep 60
done
For I get CRLF from my device, I enriched the command line with sed, removing CR:

Code: Select all

  echo getadc 1 | nc MyDevice 50290 -w 1 | sed s/'\r'//g | gawk 'BEGIN {FS=" "} {printf "t1 %d\n", $1 }'
At the command line I get a perfect output, like

Code: Select all

t1 688
t2 568
but calling this script as plug-in, I get no sensor data... any idea why?
Best Regards
Hathor27

------------------------------------------------------------
Image
http://untersiggenthal.meteodata.ch
wfpost
Platinum Boarder
Platinum Boarder
Posts: 591
Joined: Thu Jun 12, 2008 2:24 pm
Location: HONSOLGEN
Contact:

Re: Bash communication over tcp/upd

Post by wfpost »

what´s happens if you start the shell script manually from the commandline ???
Does this display an output?

sh shellscript
User avatar
Hathor27
Expert Boarder
Expert Boarder
Posts: 126
Joined: Thu May 05, 2011 9:33 pm
Location: Switzerland
Contact:

Re: Bash communication over tcp/upd

Post by Hathor27 »

as I mentioned below
Hathor27 wrote:At the command line I get a perfect output, like

Code: Select all

t1 688
t2 568
but calling this script as plug-in, I get no sensor data...
Best Regards
Hathor27

------------------------------------------------------------
Image
http://untersiggenthal.meteodata.ch
wfpost
Platinum Boarder
Platinum Boarder
Posts: 591
Joined: Thu Jun 12, 2008 2:24 pm
Location: HONSOLGEN
Contact:

Re: Bash communication over tcp/upd

Post by wfpost »

hmm, --- dann bin ich mit meinem Latein am Ende :roll: ---
it´s strange, because that means that the commands are working also when called with the script.
I don´t know how exactly a plug-in runs the script ...
Maybe Boris has an idea why this is not working as plug-in??
wfpost
Platinum Boarder
Platinum Boarder
Posts: 591
Joined: Thu Jun 12, 2008 2:24 pm
Location: HONSOLGEN
Contact:

Re: Bash communication over tcp/upd

Post by wfpost »

could it be that stdout is missing for the plugin ???

--------
#! /bin/sh
#
# runs as /home/meteohub/plugin_raw-net-io.sh
#
while :
do
# reads raw data of net-io.php
echo getadc 1 | nc MyDevice 50290 -w 1 /dev/stdout 2>/dev/null | gawk 'BEGIN {FS=" "} {printf "t1 %d\n", $1 }'
echo getadc 2 | nc MyDevice 50290 -w 1 /dev/stdout 2>/dev/null | gawk 'BEGIN {FS=" "} {printf "t2 %d\n", $1 }'

# make sure output gets flushed
sync
# wait 1 minutes for next read
sleep 60
done
--------------
User avatar
Hathor27
Expert Boarder
Expert Boarder
Posts: 126
Joined: Thu May 05, 2011 9:33 pm
Location: Switzerland
Contact:

Re: Bash communication over tcp/upd

Post by Hathor27 »

wfpost wrote:could it be that stdout is missing for the plugin ???
Negative! - this doesn't change anything: The output at the command line is still OK, but it won't show any output as Plug-In
Best Regards
Hathor27

------------------------------------------------------------
Image
http://untersiggenthal.meteodata.ch
User avatar
Hathor27
Expert Boarder
Expert Boarder
Posts: 126
Joined: Thu May 05, 2011 9:33 pm
Location: Switzerland
Contact:

Re: Bash communication over tcp/upd

Post by Hathor27 »

I found a work-around with a PHP-Script, which runs on an other server (uah!!!) and fetches the values from my NET-IO:

Code: Select all

<?php 
  /* import raw data from NET-I/O and 
      print raw_data to stdout
  */
  $commands = array("getadc 1", "getadc 2", "getadc 3", "getadc 4");
  $raw_data = "";
  $fp = fsockopen('MyDevice', 50290);          // opens connection to MyDevice
  foreach ($commands as $key => $command)      // loop over all commands
  { fwrite($fp, $command);                     // request data
    $reading = intval(fgets($fp, 4096));       // read from device
    $raw_data = $raw_data."t$key $reading\n";  // forms rawdata: "t1 234\n"...
  }
  fclose($fp);
  print $raw_data;                             // rawdata to stdout
?>
It does the task that the "echo... | nc... | sed... | gawk..."-command should do.

If I request this PHP-Script from my Bash-Script, using WGET command, it also works as Plug-In - but it's not really a smart solution...
Best Regards
Hathor27

------------------------------------------------------------
Image
http://untersiggenthal.meteodata.ch
Post Reply