Guest User

Untitled

a guest
Mar 7th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 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. #use POSIX ();
  39. #POSIX::setsid or die "setsid failed: $!";
  40.  
  41. ## Associate process with a new controlling terminal.
  42. $tty = $pty->slave;
  43. $tty_fd = $tty->fileno;
  44. close $pty;
  45.  
  46. ## Make stdio use the new controlling terminal.
  47. open STDIN, "<&$tty_fd" or die $!;
  48. open STDOUT, ">&$tty_fd" or die $!;
  49. open STDERR, ">&STDOUT" or die $!;
  50. #open STDERR, ">&$tty_fd" or die $!;
  51. close $tty;
  52.  
  53. ## Execute requested program.
  54. exec $cmd or die "problem executing $cmd\n";
  55. }
  56.  
  57. new Net::Telnet ( fhopen => $pty,
  58. telnetmode => 0,
  59. errmode => "die",
  60. cmd_remove_mode => 1,
  61. ors => "\r",
  62. timeout => 15
  63. );
  64. }
  65.  
  66. # debugging or tracing output (if global $debug is true)
  67. # example: &debug("message 1", $msg2, @other_msgs);
  68. sub debug { local $\ = "\n"; local $" = "\n"; print STDERR "@_" if $debug }
  69.  
  70.  
  71. $tmp = &mkssh("dhcpserver");
  72. $tmp->prompt($prompt_dhcp);
  73. $tmp->waitfor($tmp->prompt);
  74. $tmp->cmd(String => $nrcmd, Prompt => "/username: /");
  75. $tmp->cmd(String => $user_nrcmd, Prompt => "/password: /");
  76. $tmp->cmd($pass_nrcmd);
Add Comment
Please, Sign In to add comment