admin scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.2 KiB

  1. #!/usr/bin/ksh
  2. #
  3. # PCP (PID con Port)
  4. # v1.09 30/05/2009 Sam Nelson sam @ unix.ms
  5. #
  6. # If you have a Solaris 8, 9 or 10 box and you can't
  7. # install lsof, try this. It maps PIDS to ports and vice versa.
  8. # It also shows you which peers are connected on which port.
  9. # Wildcards are accepted for -p and -P options.
  10. #
  11. # Many thanks Daniel Trinkle trinkle @ cs.purdue.edu
  12. # for the help, much appreciated.
  13. i=0
  14. while getopts :p:P:a opt
  15. do
  16. case "${opt}" in
  17. p ) port="${OPTARG}";i=3;;
  18. P ) pid="${OPTARG}";i=3;;
  19. a ) all=all;i=2;;
  20. esac
  21. done
  22. if [ $OPTIND != $i ]
  23. then
  24. echo >&2 "usage: $0 [-p PORT] [-P PID] [-a] (Wildcards OK) "
  25. exit 1
  26. fi
  27. shift `expr $OPTIND - 1`
  28. if [ "$port" ]
  29. then
  30. # Enter the port number, get the PID
  31. #
  32. port=${OPTARG}
  33. echo "PID\tProcess Name and Port"
  34. echo "_________________________________________________________"
  35. for proc in $(ptree -a | awk '/ptree/ {next} {print $1};')
  36. do
  37. result=$(pfiles $proc 2> /dev/null| egrep "port: $port$")
  38. if [ ! -z "$result" ]
  39. then
  40. program=$(ps -fo comm= -p $proc)
  41. echo "$proc\t$program\t$port\n$result"
  42. echo "_________________________________________________________"
  43. fi
  44. done
  45. elif [ "$pid" ]
  46. then
  47. # Enter the PID, get the port
  48. #
  49. pid=$OPTARG
  50. # Print out the information
  51. echo "PID\tProcess Name and Port"
  52. echo "_________________________________________________________"
  53. for proc in $(ptree -a | awk '/ptree/ {next} $1 ~ /^'"$pid"'$/ {print $1};')
  54. do
  55. result=$(pfiles $proc 2> /dev/null| egrep "port:")
  56. if [ ! -z "$result" ]
  57. then
  58. program=$(ps -fo comm= -p $proc)
  59. echo "$proc\t$program\n$result"
  60. echo "_________________________________________________________"
  61. fi
  62. done
  63. elif [ $all ]
  64. then
  65. # Show all PIDs, Ports and Peers
  66. #
  67. echo "PID\tProcess Name and Port"
  68. echo "_________________________________________________________"
  69. for proc in `ptree -a | sort -n | awk '/ptree/ {next} {print $1};'`
  70. do
  71. out=$(pfiles $proc 2>/dev/null| egrep "port:")
  72. if [ ! -z "$out" ]
  73. then
  74. name=$(ps -fo comm= -p $proc)
  75. echo "$proc\t$name\n$out"
  76. echo "_________________________________________________________"
  77. fi
  78. done
  79. fi
  80. exit 0