日期:2014-05-16  浏览次数:20737 次

关于expect shell 与rsh
#!/bin/bash
ip=1

while [ $ip -lt 255 ]
do
  echo "#####################################---$ip">>statusinfo

  echo "------------------username:">>statusinfo
  echo "hadoop">>statusinfo
  echo "------------------ip:">>statusinfo
  echo "192.168.150.$ip">>statusinfo
  echo "------------------hostname:">>statusinfo

  expect -c " 
    set timeout 25
    expect *
    spawn rsh 192.168.150.$ip -l hadoop hostname>>statusinfo
    expect "password"
    send "hadoop\r"
  "
  sleep 2
  ip=`expr $ip + 1`

done


我的目的是通过一次循环,获取150段内所有机器的主机名和对应的ip地址,通过rsh远程机器,然后hostname命令结果重定向到本机的statusinfo文件中,可是每当shell运行到输入密码后,然后直接进入下一次循环,根本没有执行hostname命令,这是为什么 啊?大神?
都一天了,还是个这样子!
linux shell rsh hostname 重定向

------解决方案--------------------
引用:
Quote: 引用:

expect中是不一样的,参考例子:

主要是set results $expect_out(buffer)

   # Send the prebuilt command, and then wait for another shell prompt.
    send "$my_command\r"
    expect "%"
    # Capture the results of the command into a variable. This can be displayed, or written to disk.
    set results $expect_out(buffer)

我把rhs那一行改成了 let temp [spawn rsh 192.168.152.$ip -l hadoop hostname],结果效果仍然是temp获得的似乎是spawn的进程号码。
有其他的办法吗?
这个rsh是怎么回事啊?循环重定向就不管用了?


set results $expect_out(buffer)