SOLVED Shutdown RasPi by Phone

Kennybell

Member
Joined
Aug 22, 2011
Messages
67
Reaction score
4
Is there a way to safely shutdown Ras Pi by dialing an extension? I installed latest version on Pi3+. I want to shutdown by dialing an extension, say 5455 when I am finished using it. I don't have access to kbd & mon when I am at alt locations. So far pulling the plug hasn't done any harm. Just don't want to push my luck by doing it this way though. Can someone help with script?
 
So far pulling the plug hasn't done any harm.
I would advise against pulling the plug. You will eventually corrupt the Pi's disk.
Look for a way to execute an operating system command from inside asterisk.

Alternatively set up a VPN and connect via SSH and shut down the OS.
 
From the asterisk CLI
Code:
rasterisk
you can type linux commands by prefixing the commant with the exclamation character '!' so that at the asterisk CLI> prompt
Code:
pbx*CLI> !/usr/local/sbin/reboot
# OR
pbx*CLI> !reboot
would run the reboot script [ /usr/local/sbin/reboot ]. Given that I'm sure you can write a dialplan for that then dial 'KILL' . It might not be a good idea if you have a grandchild (with autism) that likes trying to dial random.

I found a link here Running shell script from within the DialPlan which may give you some clues.

That said if you still 'want' it I remember @wardmundy had something about dialing in from remote, using port knocker and doing a reboor from there; though that might have been via ssh. If you use ssh it requires a secure password for access; but from a feature code perhaps you also need a 6 digit 'pin' to be safe.

There are various ssh apps for your phone. One called 'Raspberry SSH Lite allows you to program buttons on that app
Button settings:
IP/Host: 192.168.xx.xxx ## using externalIP or fqdn and router port 22 directed to your privateIP ( ip route | grep default |cut -d" " -f7 ) and a Portknocker to whitelist your connection.
Port: 22
User Name: root
Password: thisisveryhardtodo
option use ssh key (openssh format)
Btn Text: raspi
Btn Cmd: /usr/local/sbin/reboot
You can use the app free limited to only 2 buttons
Regards
 
Last edited:
will eventually corrupt the Pi's disk
Thats correct the Pi sets thge 'Dirty' bit and lets you bugger about scratching your head wondering why.
Its worth remembering for partition checks
Code:
## where x is your mSD card
fsck.fat -a -w -v /dev/sdx1
## AND
e2fsck -f -y -v -C 0 /dev/sdx2
 
Last edited:
@Kennybell: Did we mention this is VERY DANGEROUS. You can also use it to reboot your server by replacing halt with reboot in line 4.

1. Add the following to the bottom of /etc/sudoers:
Code:
asterisk ALL = NOPASSWD: /usr/local/sbin/reboot
asterisk ALL = NOPASSWD: /usr/local/sbin/halt

2. Add the following to /etc/asterisk/extensions_custom.conf just below
[from-internal-custom] with very secure PIN in third line replacing 12345:
Code:
exten => 5455,1,Answer
exten => 5455,n,Wait(1)
exten => 5455,n,Authenticate(12345)
exten => 5455,n,System(sudo /usr/local/sbin/halt)
exten => 5455,n,Wait(10)
exten => 5455,n,Hangup

3. Reload Asterisk dialplan: asterisk -rx "dialplan reload"

p.s. NEVER USE OTHER COMMANDS TO SHUTDOWN OR REBOOT YOUR SERVER!! ONLY OUR 2 SCRIPTS (halt & reboot) GRACEFULLY CLOSE ASTERISK AND MYSQL.
 
Last edited:
Not sure why you'd ever halt your server remotely, but maybe I'm missing something obvious. :idea:
 
That's an interesting topic! With all the good will and optimism going on these days (well, for many/some of us) there still are security breaches of sorts.
I believe that having access to the PBX is important, both locally and remotely (VPN, SSH, whatever etc.) The obvious (while not only) reason for halting a PBX remotely would be as simple as it was compromised.
There are many reliable providers out there who still offer DIDs for around or a bit more than $1 / year. You dial an extension from a certain number and a courteous voice asks you if you want a reboot, halt or anything else (scripting implied...)
Would that work for a plan B in an otherwise unpredictable world?
 
Stopping asterisk without rebooting the machine is about as blocking as possible (and to me obvious) against getting a big phone bill. Your asterisk user (asterisk) likely already has access to fwconsole and (r)asterisk itself, but not it's path so



exten => 5455,n,TrySystem(/usr/sbin/fwconsole stop)

or more brutally

exten => 5455,n,TrySystem(/usr/sbin/rasterisk -x 'core stop now')

would allow both stopping VOIP calls yet leave you still access to the box for post-mortem diagnosis and the ability to restart it when you fix the problem, (neither will it possibly kill your sd card) ( Never use a hammer when a loupe is a better tool ;-) )
 
Last edited:
Well, indeed, no need for tools at all (hammers, loupes etc.) As for the post-mortem part I would do just what the phrase stands for...
 
TrySystem
TrySystem returns a fail code - but I reckon you won't get a fail code returned once fwconsole command is passed; so the application System() it seems to me should be be just as good?
 
Either will work, watch the CLI for the differences, TrySystem is more verbose
 

Members online

Forum statistics

Threads
26,709
Messages
174,552
Members
20,277
Latest member
bikerlady
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Back
Top