Save the following in a file named "toggleSsh.sh
"
#!/bin/bash
stat=`status ssh`
echo $stat
#stat is returned like: ssh start/running, process 1602
goal=`echo $stat|cut -f2 -d" "|cut -f1 -d/`
#cut the 2nd field afetr 1st space; then cut the 1st field before "/" to get the "goal" of the ssh job.
#ignoring the status
echo $goal
if [ "$goal" == "start" ];
then
service ssh stop
else
service ssh start
fi
Run this script as sudo toggleSsh.sh
since service
command needs root privilege.
You can finetune the above script by taking into account the status(waiting, starting, pre-start, spawned, post-start, running, pre-stop, stopping, killed or post-stop) of the ssh job, and not just the goal (start/stop.) Read man status
to understand more about how you can utilise the status
command.