Author: Matteo Lugaresi Language: bash
Description: Raw TCP/IP -> POST HTTP Timestamp: 2017-01-12 04:36:30 -0500
View raw paste Reply
  1. #!/bin/bash
  2.  
  3. # ----------------------------------------------------------------------------------------
  4. # MOTIVATION
  5. # ----------------------------------------------------------------------------------------
  6. # The current MX camera software (4.4.2.x) is not able to send POST notifications. In
  7. # order to overcome this limitation it's possible to send Raw TCP/IP messages to a gateway
  8. # which converts them into POST HTTP messages and sends them to the Web server
  9. #
  10. # This script listens to incoming TCP/IP messages and converts them into POST HTTP ones
  11. # Such POST requests will be sent by the gateway to a remote Web Server
  12. # The script can be hosted by a mini PC (e.g. RaspberryPi) with a Linux bash shell
  13. #
  14. # ----------------------------------------------------------------------------------------
  15. # Camera configuration
  16. # ----------------------------------------------------------------------------------------
  17. # From Admin Menu > IP Notify Profiles create a new profile as follows:
  18. # Type: Raw TPC/IP
  19. # Target: IP address of the RaspberryPI port 5000 (e.g. 192.168.1.100:5000)
  20. # Message: all the parameters you want to send to the server separated by a blank space
  21. # Example: output=1 status=high time=5
  22. #
  23. # This script handles up to three parameters but you can add as many as you need
  24.  
  25. port=5000                       # The Pi will be listening on this port
  26. target_proto=http               # Protocol supported by the remote Web Server (http or https)
  27. target_ip=my.server.com         # IP address of the remote Web Server
  28. target_port=80                  # Listening port of the remote Web Server
  29. cgi_path=cgi/bin                # CGI path of the remote Web Server without leading /
  30. user=admin                      # User of the remote Web Server
  31. password=root                   # Password of the remote Web Server
  32.  
  33. while true; do                  # infinite loop
  34.  
  35.         nc_out=$(nc -l $port)   # read the text sent by the camera and store it into a variable
  36.         ret=$?
  37.  
  38.         if [ $ret == 0 ]; then  # if the previous operation was successful
  39.                 (
  40.                 a=$(echo $nc_out | awk '{print $1}')            # reads param 1 (e.g. output=1)
  41.                 b=$(echo $nc_out | awk '{print $2}')            # reads param 2 (e.g. status=high)
  42.                 c=$(echo $nc_out | awk '{print $3}')            # reads param 3 (e.g. time=5)
  43.                 echo "Connecting to ${target_proto}://${target_ip}:${target_port}/${cgi_path}?${a}&${b}&${c}"
  44.                 curl -u "user:password" --data "${a}&${b}&${c}" "${target_proto}://${target_ip}:${target_port}/${cgi_path}"
  45.                 ) &
  46.         else                    # if the previous operation was unsuccessful
  47.                 echo "Error reading the text from the camera"
  48.                 sleep 2
  49.         fi
  50.  
  51. done
View raw paste Reply