esllin esllin - 2 years ago 83
Linux Question

Expect script on linux

I would like to make a expect script which can answer the question by using srand functuon.

For example,
I will use netcat to connect to the server,
and I will get a question from server like "please enter 0 or 1";
then I hope my script can answer the question automatically by using the code like above.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

void main(){
int num;

If you can provide me some sample, it will be very helpful.
Thank you very much.

Answer Source

You want to launch netcat, recognize that text, then return a random value in (0,1), yes?

#!/usr/bin/env expect
expr {srand([clock seconds])}    ;# initialize RNG
spawn netcat
expect "please enter 0 or 1"
send "[expr {int(rand() * 2)}]\r"
expect eof

See documentation: and

Tcl's expr command gets some flak for being a standalone mini-language inside Tcl. The syntax can be ... cleaned up somewhat.

#!/usr/bin/env expect
namespace import ::tcl::mathfunc::*
namespace import ::tcl::mathop::\*
srand [clock seconds]    ;# initialize RNG
spawn netcat
expect "please enter 0 or 1"
send "[int [* 2 [rand]]]\r"
expect eof
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download