2

Need to know if one can send a string of commands to a program.

The program is called Dynamips,

After I launch the program it comes up with "->"

The command I want to send to the terminal is "start R1" and "start R2" you get the picture.

Also want to know if it can sleep for 5 sec and then send the second string.

I did try to echo it but the echo only comes trough when u exit the program.

Any ideas

Regards

l0c0b0x
  • 11,867
  • 7
  • 47
  • 76

2 Answers2

3

You can use the expect program.

Here is an example script:

spawn /path/to/program/Dynamips
expect -- "->"
send "start R1\r"
expect -- "->" {
  sleep 5
  send "start R2\r"
}
expect eof

You can invoke this with:

expect scriptname
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • Important TCL note: The comments are first class language elements. So you can't just comment out any random line of code! If you don't know this, dealing with TCL is a very frustrating experience. – Michael Kohne Aug 26 '09 at 17:22
1

Expect is also available as a Perl module, if you are more comfortable in Perl than TCL.

A simple snippet using the Perl Expect module might look like this:

my $bob = Expect->new("bob_program");
$bob->expect(1, "Hullo!");
$bob->send("Hi!\r");
$bob->expect(1, "Bye!");
$bob->send("Later!\r");
$bob->soft_close();

I've worked both with expect/TCL and perl, and I have to say that I prefer the Perl. If you don't already know it, TCL is kind of annoying to pick up.

Michael Kohne
  • 2,334
  • 1
  • 16
  • 29