Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## perl sub &mkssh to create an SSH session to given host using publickey-auth
- # example: $ssh = mkssh("hipsxxc3");
- sub mkssh {
- use IO::Pty ();
- use Net::Telnet;
- my $tmp = '';
- #chomp(my $cmd_ssh = `which ssh`); # wrong on hipsscr01 :-(
- my $cmd_ssh = "/usr/bin/ssh";
- my $host = shift;
- my $user = $ENV{USER};
- my $tmp = shift if @_;
- $user = $tmp if $tmp;
- my $port = 22;
- $tmp = shift if @_;
- $port = $1 if $tmp =~ /\d+/;
- my $cmd = "$cmd_ssh -l $user -p $port $host";
- my($pid, $pty, $tty, $tty_fd, $ssh, $telnet);
- &debug("mkssh:\thost: $host", "\tuser: $user", "\tport: $port", "\tcmd: $cmd");
- # new pseudo terminal
- $pty = new IO::Pty or die $!;
- # used for killing zombies
- $SIG{CHLD} = 'IGNORE';
- # fork process
- $pid = fork;
- unless ($pid) { # child process
- die "problem spawning program: $!\n" unless defined $pid;
- # reset SIG_CHLD, essentially for exec() call
- $SIG{CHLD} = 'DEFAULT';
- ## Disassociate process from existing controlling terminal.
- # has no effect
- #use POSIX ();
- #POSIX::setsid or die "setsid failed: $!";
- ## Associate process with a new controlling terminal.
- $tty = $pty->slave;
- $tty_fd = $tty->fileno;
- close $pty;
- ## Make stdio use the new controlling terminal.
- open STDIN, "<&$tty_fd" or die $!;
- open STDOUT, ">&$tty_fd" or die $!;
- open STDERR, ">&STDOUT" or die $!;
- #open STDERR, ">&$tty_fd" or die $!;
- close $tty;
- ## Execute requested program.
- exec $cmd or die "problem executing $cmd\n";
- }
- new Net::Telnet ( fhopen => $pty,
- telnetmode => 0,
- errmode => "die",
- cmd_remove_mode => 1,
- ors => "\r",
- timeout => 15
- );
- }
- ## perl sub &debug for debugging or tracing output (if global $debug is true)
- # example: &debug("message 1", $msg2, @other_msgs);
- sub debug { local $\ = "\n"; local $" = "\n"; print STDERR "@_" if $debug }
- ## creation of SSH session with &mkssh
- $tmp = &mkssh("dhcpserver");
- $tmp->prompt($prompt_dhcp);
- $tmp->waitfor($tmp->prompt);
- $tmp->cmd(String => $nrcmd, Prompt => "/username: /");
- $tmp->cmd(String => $user_nrcmd, Prompt => "/password: /");
- $tmp->cmd($pass_nrcmd);
- # the login works, the execution of commands, too.
- # but the child process /usr/bin/ssh -l .. -p .. .. keeps existing. it's a zombie.
- # and on the dhcp-server, there are zombies, too.
- # how to avoid these zombies? the "use posix" and the $SIG{CHLD} lines have no effect. :(
Add Comment
Please, Sign In to add comment