Guest User

Untitled

a guest
Mar 7th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. ## perl sub &mkssh to create an SSH session to given host using publickey-auth
  2. # example: $ssh = mkssh("hipsxxc3");
  3. sub mkssh {
  4. use IO::Pty ();
  5. use Net::Telnet;
  6.  
  7. my $tmp = '';
  8. #chomp(my $cmd_ssh = `which ssh`); # wrong on hipsscr01 :-(
  9. my $cmd_ssh = "/usr/bin/ssh";
  10. my $host = shift;
  11. my $user = $ENV{USER};
  12. my $tmp = shift if @_;
  13. $user = $tmp if $tmp;
  14. my $port = 22;
  15. $tmp = shift if @_;
  16. $port = $1 if $tmp =~ /\d+/;
  17. my $cmd = "$cmd_ssh -l $user -p $port $host";
  18. my($pid, $pty, $tty, $tty_fd, $ssh, $telnet);
  19.  
  20. &debug("mkssh:\thost: $host", "\tuser: $user", "\tport: $port", "\tcmd: $cmd");
  21.  
  22. # new pseudo terminal
  23. $pty = new IO::Pty or die $!;
  24.  
  25. # used for killing zombies
  26. $SIG{CHLD} = 'IGNORE';
  27.  
  28. # fork process
  29. $pid = fork;
  30.  
  31. unless ($pid) { # child process
  32. die "problem spawning program: $!\n" unless defined $pid;
  33.  
  34. # reset SIG_CHLD, essentially for exec() call
  35. $SIG{CHLD} = 'DEFAULT';
  36.  
  37. ## Disassociate process from existing controlling terminal.
  38. # has no effect
  39. #use POSIX ();
  40. #POSIX::setsid or die "setsid failed: $!";
  41.  
  42. ## Associate process with a new controlling terminal.
  43. $tty = $pty->slave;
  44. $tty_fd = $tty->fileno;
  45. close $pty;
  46.  
  47. ## Make stdio use the new controlling terminal.
  48. open STDIN, "<&$tty_fd" or die $!;
  49. open STDOUT, ">&$tty_fd" or die $!;
  50. open STDERR, ">&STDOUT" or die $!;
  51. #open STDERR, ">&$tty_fd" or die $!;
  52. close $tty;
  53.  
  54. ## Execute requested program.
  55. exec $cmd or die "problem executing $cmd\n";
  56. }
  57.  
  58. new Net::Telnet ( fhopen => $pty,
  59. telnetmode => 0,
  60. errmode => "die",
  61. cmd_remove_mode => 1,
  62. ors => "\r",
  63. timeout => 15
  64. );
  65. }
  66.  
  67. ## perl sub &debug for debugging or tracing output (if global $debug is true)
  68. # example: &debug("message 1", $msg2, @other_msgs);
  69. sub debug { local $\ = "\n"; local $" = "\n"; print STDERR "@_" if $debug }
  70.  
  71. ## creation of SSH session with &mkssh
  72. $tmp = &mkssh("dhcpserver");
  73. $tmp->prompt($prompt_dhcp);
  74. $tmp->waitfor($tmp->prompt);
  75. $tmp->cmd(String => $nrcmd, Prompt => "/username: /");
  76. $tmp->cmd(String => $user_nrcmd, Prompt => "/password: /");
  77. $tmp->cmd($pass_nrcmd);
  78.  
  79. # the login works, the execution of commands, too.
  80. # but the child process /usr/bin/ssh -l .. -p .. .. keeps existing. it's a zombie.
  81. # and on the dhcp-server, there are zombies, too.
  82. # how to avoid these zombies? the "use posix" and the $SIG{CHLD} lines have no effect. :(
Add Comment
Please, Sign In to add comment