Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Shutdown a second raspberry pi via SSH.

snowman159
2020-06-13
2020-06-24
  • snowman159 - 2020-06-13

    I have 2 Raspberry pi boards:
    - pi@PLC with the codesys project
    - pi@raspberrypi (the other one)

    I already made two buttons to shutdown and restart pi@PLC without problems.

    Then I made a 3rd button to shutdown pi@raspberrypi with the following code:

    PROGRAM test
    VAR
        _xDoExecuteShell : BOOL;
        _Retval: DINT;
        _pResult : Systypes.RTS_IEC_RESULT;
        _sCommand : STRING;
    END_VAR
    
    IF _xDoExecuteShell THEN
        _xDoExecuteShell := FALSE;  
        _sCommand := 'ssh pi@raspberrypi -t sudo shutdown -h -P now';
        _RetVal := Sysprocess.SysProcessExecuteCommand(pszComand:= _sCommand, pResult:= ADR(_pResult));
    END_IF
    

    Both RP boards share a private generated key. When I use Putty to log in via SSH to pi@PLC and execute: ssh pi@raspberrypi -t sudo shutdown -h -P now, it shuts down perfectly without asking for passwords or anything. Just as intended.

    However it does not work via Codesys.

    I use the same syntax above to shutdown(sudo shutdown -h now & disown) and restart(sudo reboot) pi@PLC without problems, however the code above does not seem to do anything regarding pi@raspberrypi.

    Anyone have any idea what I am doing wrong?

    Thanks in advance.

     
  • eschwellinger

    eschwellinger - 2020-06-14

    Hi,
    I would give this a try:
    generate a script for example ssh-shutdown.sh with your command:
    ssh pi@raspberrypi -t sudo shutdown -h -P now
    and just execute then ssh-shutdown.sh by the codesys command.

    Best Regards
    Edwin

     
  • valentin-sl - 2020-06-14

    Hello, look to the side.

    PROGRAM POU_ST
    VAR
    pRes: POINTER TO SysProcess.SysTypes.RTS_IEC_RESULT;
    StrName_Shutdown: STRING(255):= 'sudo shutdown now ';
    END_VAR

    SysProcessExecuteCommand((StrName_Shutdown) , pRes );

     
  • nothinrandom - 2020-06-21

    If you haven't done so, maybe add ssh to /etc/CODESYSControl_User.cfg:

    [SysProcess]
    Command.0=shutdown
    Command.1=ssh

    shutdown is default. There's another option to allow all via Command=AllowAll, but this is way too permissive.

     
  • snowman159 - 2020-06-24

    Thank you all for the feedback.

    I used Edwin's suggestion and created a python script.
    This seems to do the trick.

    Kind regards

     

Log in to post a comment.