Cron Jobs

Post your tips and tricks here
w1tcc
Posts: 4
Joined: Sun May 31, 2020 8:39 pm

Cron Jobs

Post by w1tcc »

Cron jobs in V4.1.2 are working well now that Andy has added all the scripts necessary to connect in all the modes.
The simplest d-star example would be to go to the telnet console and type in:
sudo pistar-link ref030_c
to unlink type: sudo pistar-link unlink
for YSF it's: sudo pistar-ysflink ysf21080
for BM it's: sudo pistar-bmapi addtg 3100 0
for TGIF it's: sudo pistar-tgifapi link 31665 0

Type in the script name alone to see what all the options are.
Other modes are available such as NXDN and P25.

Automatically connecting to weekly nets makes these command line options useful. First backup your pistar config and if possible your SD card just in case. Next go to the pistar dashboard -> Configuration -> Expert -> System Cron What you will see is the existing system wide cron job. After the last line which currently ends with "/etc/cron.monthly" add and edit the following:

Code: Select all

# Connect to BM 3100 default timeslot at 16:15 then delete at 16:20 on Friday the 5 day of the week
# The hash is a comment within the file.  Command line start with: MIN HOUR DOM MON DOW USER COMMAND
15 16 * * 5   root  pistar-bmapi    addtg 3100 0
20 16 * * 5   root  pistar-bmapi    deltg 3100 0
Followed by a CR then press -> Apply Changes Go back to the admin dashboard to watch and listen.

Next is another example for auto starting a Net at 10pm eastern local time

Code: Select all

#Nebraska D-Star Net on Thurs.
00 22 * * 4   root pistar-link        ref073_c 
59 22 * * 4   root pistar-link        unlink
Third example:

Code: Select all

#Test America link on YSF Sunday night 8pm for 15 min
00 20 * * 0   root pistar-ysflink     ysf21080  
15 20 * * 0   root pistar-ysflink     unlink
For duplex hotspots BM and TGIF the timeslot will be 1 or 2 not 0 and the connections will be static.
Have fun
Tim C
VK2FMET
Posts: 77
Joined: Fri Dec 20, 2019 1:19 am

Re: Cron Jobs

Post by VK2FMET »

Code: Select all

pi-star@piccolo(ro):~$ sudo pistar-bmapi addtg 31012 0
 Request to BrandMesiter API: Add TG 31012 to TimeSlot 0 for DMR ID 5050766            
Traceback (most recent call last):                                                     
  File "<string>", line 1, in <module>                                                 
  File "/usr/lib/python3.7/json/__init__.py", line 296, in load                        
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)          
  File "/usr/lib/python3.7/json/__init__.py", line 348, in loads                       
    return _default_decoder.decode(s)                                                  
  File "/usr/lib/python3.7/json/decoder.py", line 337, in decode                       
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())                                  
  File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode                   
    raise JSONDecodeError("Expecting value", s, err.value) from None                   
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)                
Answer from BrandMesiter API:                                                          
                                                                                       
pi-star@piccolo(ro):~$ sudo pistar-tgifapi link 31665 0                                
 Request to TGIF API: Set TimeSlot 0 to TG 31665 for DMR ID                            
Answer from TGIF API: Somthing went wrong with this request                            
                                                                                       
pi-star@piccolo(ro):~$ sudo pistar-tgifapi link 31665 2                                
 Request to TGIF API: Set TimeSlot 2 to TG 31665 for DMR ID                            
Answer from TGIF API: Somthing went wrong with this request                            
                                                                                       
pi-star@piccolo(ro):~$ 
Can you give me a hint as to what's wrong here?
Alan
VK1AO
VK2FMET
Posts: 77
Joined: Fri Dec 20, 2019 1:19 am

Re: Cron Jobs

Post by VK2FMET »

OK - to answer:
Q1: The error is this exception: raise JSONDecodeError("Expecting value", s, err.value) from None
and the TG does not connect.
Q2: Yes I do have a key installed and I do see the Manager on teh dashboard.
Q3: I run my HS 24/7 it's 4.1.2 Dashboard 20200813 but I have just done update and upgrade and nothing was updated/upgraded - it says nothing to update and I am already running latest version.

What will re-image do since I am on the latest?

My other identical (except it's DStar/YSF and not DMR) hotspot does work fine with the command line -ysflink and -link commands for ysf and dstar reflectors.
Alan
VK1AO
VK2FMET
Posts: 77
Joined: Fri Dec 20, 2019 1:19 am

Re: Cron Jobs

Post by VK2FMET »

OK - why do I need to set an ESSID when I have one radio, one DMR hotspot?
Alan
VK1AO
AF6VN
Posts: 821
Joined: Fri Jul 20, 2018 1:15 am

Re: Cron Jobs

Post by AF6VN »

KE7FNS wrote: Mon Sep 07, 2020 2:05 am
VK2FMET wrote: Mon Sep 07, 2020 1:15 am Can you give me a hint as to what's wrong here?
Do you have a BM API key installed and can you see the BM manager in the ADMIN section of the dashboard?
Given that the script checks for the (a) key before parsing the command options, and isn't issuing

Code: Select all

else
        echo "Unable to find your BrandMesiter API Key, no API commands available."
        exit 0
I'd conclude there is /something/ stored as the API key.

{I'm more amazed at the many mis-spelled "BrandMeister" in these messages!}

This mess, however, ...

Code: Select all

RESULT=$(wget -q -O - --user-agent="Pi-Star cli tool for: ${DMRID}" --header="Content-Type: application/x-www-form-urlencoded" --header="Authorization: Basic ${APIKeyB64}:"  --post-data="${APIDATA}" "${APIURL}" | \
        python3 -c "import sys, json; print(json.load(sys.stdin)['message'])")
echo "Answer from BrandMesiter API: ${RESULT}"
... appears to run a Python instance with everything provided as a single command line, taking the output from wget piped into stdin, loading it as JSON, and extracting the "message" field which gets sent to stdout. I'm not skilled in Linux shell scripts -- that seems to imply that the stdout from Python is getting fed back to the RESULT variable. There isn't much that can go wrong with that python invocation.

The TGIF API is just looking for an HTTP status code

Code: Select all

RESULT=$(wget -q -O - --user-agent="Pi-Star cli tool for: ${DMRID}" --header="Content-Type: application/x-www-form-urlencoded" "${APIURL}")
if [[ ${RESULT} == 200 ]]; then
        RESULT="OK"
else
        RESULT="Somthing went wrong with this request"
fi
echo "Answer from TGIF API: ${RESULT}"
echo ""
exit 0
Might be worth removing/commenting out the |\ and python3 lines, and seeing what the raw return data looks like. And maybe see what the command line is returning for apikey and dmrid (yes, two undocumented commands)

Code: Select all

pi-star@pi-star-3b(ro):~$ sudo pistar-bmapi apikey
Your BrandMeister API Key is: ***suppressed***
pi-star@pi-star-3b(ro):~$ sudo pistar-bmapi dmrid
Your DMR ID is: 1126104
pi-star@pi-star-3b(ro):~$ sudo pistar-bmapi addtg 3100 0
 Request to BrandMesiter API: Add TG 3100 to TimeSlot 0 for DMR ID 1126104
Answer from BrandMesiter API: Talkgroup 3100 added on slot 0.

pi-star@pi-star-3b(ro):~$ sudo pistar-bmapi deltg 3100 0
 Request to BrandMesiter API: Delete TG 3100 from TimeSlot 0 for DMR ID 1126104
Answer from BrandMesiter API: Talkgroup 3100 remove from slot 0.


--
AF6VN
Dennis L Bieber
AF6VN
Posts: 821
Joined: Fri Jul 20, 2018 1:15 am

Re: Cron Jobs

Post by AF6VN »

KE7FNS wrote: Mon Sep 07, 2020 8:02 pm I didn't devote any time digging into the actual bmapi script written by Andy at all. I just looked at the error codes and noticed that they are not a script related to Pi-star at all. They are scripts that belong to the python installation itself.
The imported modules are standard Python library routines.
I'm pretty sure the failure is at the

Code: Select all

import sys, json;
It appears there is something wrong with his json scripts in the python installation (/usr/lib/python3.7/json/__init__.py) as those are not anything modified or maintained by Andy. There should be no reason those scripts are failing.
The error message indicates that what is being /read/ from sys.stdin does not match what is expected for JSON format.

Code: Select all

  File "/usr/lib/python3.7/json/decoder.py", line 337, in decode                       
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())                                  
  File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode                   
    raise JSONDecodeError("Expecting value", s, err.value) from None                   
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)  

Code: Select all

    def raw_decode(self, s, idx=0):
        """Decode a JSON document from ``s`` (a ``str`` beginning with
        a JSON document) and return a 2-tuple of the Python
        representation and the index in ``s`` where the document ended.

        This can be used to decode a JSON document from a string that may
        have extraneous data at the end.

        """
        try:
            obj, end = self.scan_once(s, idx)
        except StopIteration as err:
            raise JSONDecodeError("Expecting value", s, err.value) from None
        return obj, end
The error states that the first character (char 0) does not match a valid JSON object (which likely means it is not something like {, [, or ( ). Unfortunately, the exception formatting does not /print/ "s" to show what it found (it is passed to the exception only for use in computing the line/column values).
Thats why I suggested he just reimage and start over with a fresh image that will work out of the box. I've already spent more time typing this one post up than the time it would take for him to reimage. Its just the easier route to get back to a fully working hotspot.
While the shell script is finding /something/ for the API Key, I think I'd go on Brandmeister, invalidate the existing key, and generate a new key to be cut&pasted into Pi-Star's BM configuration page. Possible a key is there, but it is corrupted.

--
AF6VN
Dennis L Bieber
VK2FMET
Posts: 77
Joined: Fri Dec 20, 2019 1:19 am

Re: Cron Jobs

Post by VK2FMET »

Well it's not the image or the scripts being corrupted.
I re-imaged and re-built my hotspot.
I can confirm that the scripts work up until the point I fully populate my gateway script with re-write rules.
I tested the addtg command before I finished the rebuild and it worked so it looked promising. Then I tried the deltg command and it fell over again.
Now that I am finished - the result is as per my original post.
I have compared the current scripts with my previous ones and there is no difference.

It has to be the script logic not liking my stanza content or the number of servers I use.

I have the following enabled:
XLX
Brandmeister
IPSC2-Quadnet
IPSC2-VKHOTSPOTS
DMR2YSF
TGIF

My BM Key is fine since I have the manager and it works fine.
Alan
VK1AO
AF6VN
Posts: 821
Joined: Fri Jul 20, 2018 1:15 am

Re: Cron Jobs

Post by AF6VN »

KE7FNS wrote: Tue Sep 08, 2020 7:29 pm Is there a reason why you repeat to me what I just said in my previous post?
Because the imported modules are NOT SCRIPTS per se. They are in essence function libraries.

The only "script" is the command line string itself which is doing the import.
BM API keys don't use any of those characters, so the scripts shouldn't be expecting one, ESPECIALLY at the start.
But it isn't the key that is being processed -- the Python portion has nothing to do with BM API keys; it is attempting to parse a JSON encoded result string returned by the server.

--
AF6VN
Dennis L Bieber
IV3BVK
Posts: 7
Joined: Sat Feb 22, 2020 1:12 pm
Location: Trieste
Contact:

Re: Cron Jobs

Post by IV3BVK »

VK2FMET wrote: Wed Sep 09, 2020 1:38 am Well it's not the image or the scripts being corrupted.
I re-imaged and re-built my hotspot.
I can confirm that the scripts work up until the point I fully populate my gateway script with re-write rules.
I tested the addtg command before I finished the rebuild and it worked so it looked promising. Then I tried the deltg command and it fell over again.
Now that I am finished - the result is as per my original post.
I have compared the current scripts with my previous ones and there is no difference.

It has to be the script logic not liking my stanza content or the number of servers I use.

I have the following enabled:
XLX
Brandmeister
IPSC2-Quadnet
IPSC2-VKHOTSPOTS
DMR2YSF
TGIF

My BM Key is fine since I have the manager and it works fine.
Hi All,

today I wanted to use these commands to insert in the crontab of my pistar, only the commands dedicated to the DMR do not work yet.

My working conditions:

DMR Gateway with four networks and two streams (DMR BM-2222, IPSC2-IT-MLINK, TGIF, FreeDMR, XLX and DMR2YSF).

I use the BM v2 key, successfully from the Admin dashboard.

The pistar harware is a Pi 0 W MMDVM Simplex - Version is: 4.1.6 Dashboard: 22020904


Sending the command from the SSH terminal connected to the pistar:

sudo pistar-bmapi addtg 2237 0

the answer is as follows:
/usr/local/sbin/pistar-bmapi: line 108: [[: 1.0: syntax error: invalid arithmetic operator (error token is ".0")
Request to BrandMesiter API: Add TG 2237 to TimeSlot 0 for DMR ID 222448316
/usr/local/sbin/pistar-bmapi: line 224: [[: 1.0: syntax error: invalid arithmetic operator (error token is ".0")
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable
Answer from BrandMesiter API:

and the pistar does not add the static TG.

What and how can I do to solve the problem?

Do these pistar-bmapi commands work with BM's API v2 key?

'73 de Paolo IV3BVK
-
Paolo IV3BVK

WW LOC JN65GX - 33074 Fontanafredda (PN)
web: www.paolettopn.it
AF6VN
Posts: 821
Joined: Fri Jul 20, 2018 1:15 am

Re: Cron Jobs

Post by AF6VN »

Looking at the script, the fault may be in the section

Code: Select all

# Get the stored BM API Key
if [ -f ${APIKeyFile} ]; then
        if [[ $(wc -c ${APIKeyFile} | awk '{print $1}') > 200 ]]; then
                echo -e "Using BM API v2"
                APIURL="https://api.brandmeister.network/v2/device/"
                APIKey=$(grep -m 1 'apikey=' ${APIKeyFile} | awk -F "=" '/apikey/ {print $2}')
                APIVer="2"
        else
                echo -e "Using BM API v1.0"
                APIURL="https://api.brandmeister.network/v1.0/repeater/"
                APIKey=$(grep -m 1 'apikey=' ${APIKeyFile} | awk -F "=" '/apikey/ {print $2}')
                APIKeyB64=$(echo -n "${APIKey}:" | base64 | sed 's/=$*//g' | sed ':a;N;$!ba;s/\n
/ /g' | sed 's/ //g')
                APIVer="1.0"
        fi
Note that v2 gets "APIVer="2", but v1 is getting "APIVer="1.0". The error lines you show appear to be comparing APIVer against a value of 2, and choking when it sees 1.0 (as I recall, shell scripts don't do floating point numbers).

--
AF6VN
Dennis L Bieber
Post Reply