#!/usr/bin/perl

# ruft ssh auf, aber spuckt ein Passwort aus

use utf8;
use strict;
use warnings;
use IO::Pty;
#use IO::Stty;
use POSIX qw(WTERMSIG WIFSIGNALED WEXITSTATUS);

die "Usage: echo \$PASS | $0 ssh-args...\n" unless @ARGV;

my $pty = IO::Pty->new;
my $s = $pty->slave(),

$pty->set_raw();
$s->set_raw();

my $pid = fork();
die "no fork: $!\n" unless defined $pid;
unless($pid) {
	close(STDIN);
	#close(STDOUT);
	open(STDIN,"<&".($s->fileno));
	#open(STDOUT,">&".($s->fileno));
	$pty->make_slave_controlling_terminal();
	
	unshift(@ARGV,"/usr/bin/ssh","-Tax");
	exec { $ARGV[0] } @ARGV or exit(99);
}

while(<STDIN>) {
	print $pty $_;
}
# $pty->close();

my $res = waitpid($pid,0);
kill WTERMSIG($?),$$ if WIFSIGNALED($?);
exit WEXITSTATUS($?);
