summaryrefslogtreecommitdiff
path: root/nfs-ssh-fw
blob: 5a8e7a21ec6c2f96366b589340a5396ccc780a81 (plain)
  1. #!/usr/bin/perl -w
  2. # Found here:
  3. # http://www.samag.com/documents/s=4072/sam0203d/sam0203d.htm
  4. use strict;
  5. use File::Basename;
  6. ## CONFIGURATION
  7. my $nfs_server = "nfs1"; # the nfs server to connect to
  8. my $nfs_server_user = "james"; # a valid username on the nfs server
  9. my $use_version = "2"; # nfs-user-server uses 2, otherwise 3
  10. # would be better. Check output of
  11. # 'rpcinfo -p <servername>'
  12. my $nfsd_client_port = "2818"; # we will port forward nfsd here
  13. my $mountd_client_port = "3045"; # we will port forward mountd here
  14. my $sleep_length = "86400"; # how long to sleep before restarting
  15. # 86400 secs is one day. Note
  16. # this is overridden if a command is
  17. # specified in the server's
  18. # authorized_keys2 file
  19. # need to keep '-f', can also specify encryption algorithm, the ssh version
  20. # and the id key
  21. my $ssh_opts = "-f -c blowfish -2 -i /home/james/.ssh/id_dsa_nfs";
  22. my %rpcinfo_col = ( # change as per output of rpcinfo -p
  23. 'program' => '0',
  24. 'version' => '1',
  25. 'protocol' => '2',
  26. 'port' => '3',
  27. 'daemon' => '4'
  28. );
  29. ## END CONFIGURATION
  30. # not much should need to change below here
  31. my $prog_name = basename($0);
  32. my $nfsd_server_port = "";
  33. my $mountd_server_port = "";
  34. # for signals
  35. $SIG{INT} = sub { die "$0 interrupted and dying (does not kill ssh)\n" };
  36. my $first_time = 1;
  37. while (1) {
  38. if ($first_time) {
  39. print "$prog_name: Starting ssh/nfs forwarding&#151;\n";
  40. $first_time = 0;
  41. } else {
  42. print "$prog_name: Restarting ssh/nfs forwarding&#151;\n";
  43. }
  44. # first, get the rpcinfo
  45. my @rpcinfo = `rpcinfo -p $nfs_server`;
  46. print "My rpcinfo =\n @rpcinfo";
  47. # now get the nfsd and mountd port numbers
  48. foreach (@rpcinfo) {
  49. my @line = split;
  50. if ($line[$rpcinfo_col{"version"}] eq $use_version &&
  51. $line[$rpcinfo_col{"daemon"}] eq "nfs" &&
  52. $line[$rpcinfo_col{"protocol"}] eq "tcp") {
  53. $nfsd_server_port = $line[$rpcinfo_col{"port"}];
  54. print (" nfsd port = $nfsd_server_port");
  55. } elsif ($line[$rpcinfo_col{"version"}] eq $use_version &&
  56. $line[$rpcinfo_col{"daemon"}] eq "mountd" &&
  57. $line[$rpcinfo_col{"protocol"}] eq "tcp") {
  58. $mountd_server_port = $line[$rpcinfo_col{"port"}];
  59. print (", mountd port = $mountd_server_port\n");
  60. }
  61. }
  62. # now run ssh (if this fails, we get the error message and
  63. # retry). This should run all the time. This also won't die
  64. # unless the nfs mount is done.
  65. `/usr/bin/ssh $ssh_opts -L \
  66. $nfsd_client_port:$nfs_server:$nfsd_server_port -L \
  67. $mountd_client_port:$nfs_server:$mountd_server_port -l \
  68. $nfs_server_user $nfs_server /bin/sleep $sleep_length`;