lsh 975 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/bin/sh
  2. # This script can be used as a "remote shell" command that is only
  3. # capable of pretending to connect to "localhost". This is useful
  4. # for testing or for running a local copy where the sender and the
  5. # receiver needs to use different options (e.g. --fake-super). If
  6. # we get a -l USER option, we try to use "sudo -u USER" to run the
  7. # command.
  8. user=''
  9. prefix=''
  10. do_cd=y # Default path is user's home dir, just like ssh.
  11. while : ; do
  12. case "$1" in
  13. -l) user="$2"; shift; shift ;;
  14. -l*) user=`echo $1 | sed 's/^-l//'`; shift ;;
  15. --no-cd) do_cd=n; shift ;;
  16. -*) shift ;;
  17. localhost) shift; break ;;
  18. *) echo "lsh: unable to connect to host $1" 1>&2; exit 1 ;;
  19. esac
  20. done
  21. if [ "$user" ]; then
  22. prefix="sudo -H -u $user"
  23. if [ $do_cd = y ]; then
  24. home=`perl -e "print((getpwnam("$user"))[7])"`
  25. # Yeah, this may fail, but attempts to get sudo to cd are harder.
  26. cd $home
  27. fi
  28. elif [ $do_cd = y ]; then
  29. cd
  30. fi
  31. eval $prefix "${@}"