Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Creating Magic Script 1

Status
Not open for further replies.

ITadvice

IS-IT--Management
Jul 22, 2008
38
US
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:

Code:
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 doesn't execute the commands.

Any help is greatly appreciated, I’m not really sure how this “magic” thing works.
 
You need to escape the @'s. If you're expecting the user to actually type in the @ character, your script is checking for an array named @ (because @ is the array identifier). If you want to check for if the user actually typed in "@@", you need to check for \@\@, and escape both of them with backslashes. The same is true if you were expecting $ (scalars) or % (hashes).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top