I'm trying to write a magic script that will execute a command (for example, ssh) and monitor user input. If it sees "@@" it will accept the rest of the input up the <RETURN> as the filename of a script. For instance, after entering "@@test", a single <RETURN> is entered and the script, "test" begins to execute. Every output of "test" script goes to the "ssh" command as if the original person had typed them in. It runs until the owner kills it. Here's what I have:
if ($input[0] eq "ssh"){ # SSH session my $exp = new Expect; $exp->raw_pty(1); $exp = Expect->spawn("$cmd\r"); $exp->expect(1, '-ex', "$_"); system("stty -echo"); $exp->expect (1, '-ex', "$_"); $password = <STDIN>; system("stty echo"); chomp $password; $exp->send ("$password\r"); $exp->expect (1, '-ex', "$login[1]:~#"); my $input = <STDIN>; chomp $input; if ($input =~ "@@"){ $input =~ s/@@//; my $script = "/root/Desktop/$input"; open(SCRIPT, "./$input|"); while (<SCRIPT>){ $exp->send ("$_\r"); } close (SCRIPT); } else {$exp->send ("$input\r");} } else{ # Telnet session my $exp = new Expect; $exp->raw_pty(1); $exp = Expect->spawn("$cmd\r"); $exp->expect(1, '-ex', "Password:"); system("stty -echo"); $password = <STDIN>; chomp $password; $exp->send ("$password\r"); system("stty echo"); $exp->expect(1, '-ex', "$_"); # User mode $command = <STDIN>; chomp $command; $exp->send ("$command\r"); $exp->expect (1, '-ex', "$_"); # Privileged mode system("stty -echo"); $password = <STDIN>; chomp $password; system("stty echo"); $exp->send( "$password\r"); $exp->expect (1, '-ex', "$_"); my $input = <STDIN>; chomp $input; if ($input =~ "@@"){ $input =~ s/@@//; my $script = "/$dir/$input"; open(SCRIPT, "./$input|"); while (<SCRIPT>){ $exp->send ("$_\r"); } else {$exp->send ("$input\r");} }
When I telnet, it will read “test” and execute the commands. But when I ssh, for some reason it does execute the commands. Any help is greatly appreciated, I’m not really sure how this “magic” thing works.

In reply to Help Using Magic Script by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.