#!/bin/sh
#
# (2002) David Lefevre & Cyriac Remy
#
# Start/stop/restart the secure shell server:
#

sshd_start() {
  # Create host keys if needed.
  if [ ! -r /usr/local/etc/ssh_host_key ]; then
    /usr/local/bin/ssh-keygen -t rsa1 -f /usr/local/etc/ssh_host_key -N ''
  fi
  if [ ! -f /usr/local/etc/ssh_host_dsa_key ]; then
    /usr/local/bin/ssh-keygen -t dsa -f /usr/local/etc/ssh_host_dsa_key -N ''
  fi
  if [ ! -f /usr/local/etc/ssh_host_rsa_key ]; then
    /usr/local/bin/ssh-keygen -t rsa -f /usr/local/etc/ssh_host_rsa_key -N ''
  fi
  # to be sure sshd will answer even if high load cpu
  /usr/bin/nice -n -5 /usr/local/sbin/sshd
}


sshd_stop() {
  killall sshd
}

sshd_restart() {
  sshd_stop
  sleep 1
  sshd_start
}


sshd_status() {
  echo "Daemon : "
  ps -ef | grep sshd | grep -v status | grep -v grep | head -1
  echo "port :"
  netstat -an | grep "*.22" | grep LISTEN | head -1
  echo "done."
}


case "$1" in
'start')
  sshd_start
  ;;
'stop')
  sshd_stop
  ;;
'restart')
  sshd_restart
  ;;
'status')
  sshd_status
  ;;
*)
  echo "usage $0 start|stop|restart"
esac
