【问题标题】:Expect script on linuxlinux上的expect脚本
【发布时间】:2016-06-10 07:57:52
【问题描述】:

我想制作一个可以使用 srand 函数回答问题的期望脚本。

例如, 我将使用netcat连接到服务器, 我会从服务器收到一个问题,例如“请输入 0 或 1”; 那么我希望我的脚本可以使用上面的代码自动回答这个问题。

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

  void main(){
  int num;
  srand(time(NULL));
  num=(rand()%2);
  printf("%d\n",num);}

如果你能提供一些样本,那将非常有帮助。 非常感谢。

【问题讨论】:

    标签: linux expect srand


    【解决方案1】:

    您想启动 netcat,识别该文本,然后在 (0,1) 中返回一个随机值,是吗?

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

    请参阅文档:http://tcl.tk/man/tcl8.6/TclCmd/expr.htmhttp://tcl.tk/man/tcl8.6/TclCmd/mathfunc.htm

    Tcl 的expr 命令因为是 Tcl 内部的独立迷你语言而受到批评。语法可以......稍微清理一下。

    #!/usr/bin/env expect
    namespace import ::tcl::mathfunc::*
    namespace import ::tcl::mathop::\*
    srand [clock seconds]    ;# initialize RNG
    spawn netcat 127.0.0.1
    expect "please enter 0 or 1"
    send "[int [* 2 [rand]]]\r"
    expect eof
    

    【讨论】:

    • 您好,感谢您的回复。我使用你的示例代码,它是工作!如果我想使用 SRAND 方法,可以吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    相关资源
    最近更新 更多