写一个很简脚本的,问题出在哪里,各位来看看吧

sh/bash/dash/ksh/zsh等Shell脚本
回复
always.zhang
帖子: 7
注册时间: 2006-11-14 18:08

写一个很简脚本的,问题出在哪里,各位来看看吧

#1

帖子 always.zhang » 2006-12-08 9:58

我是写一个自动重启网卡的脚本
#!/bin/bash
sudo ifdown eth0
12345678>stdin //想让它自动输入密码,我的密码是12345678
sudo ifup eth0

但是执行的时候,它却要让我输入密码。这个怎么回事呢?
头像
sigma
帖子: 439
注册时间: 2006-06-01 22:45
来自: Fujian

#2

帖子 sigma » 2006-12-18 20:57

这样肯定是不对的
应该要用到一个叫expect的东西

用这个脚本试试 :

代码: 全选

#!/usr/bin/expect -f
spawn sudo ifdown eth0
expect {
Password: { send "12345678\r";exp_continue }
}
exit
被时间羁绊,被邪恶包围……空气稀薄
always.zhang
帖子: 7
注册时间: 2006-11-14 18:08

#3

帖子 always.zhang » 2006-12-20 11:23

还是出错
netrestart2: 2: spawn: not found
netrestart2: 3: expect: not found
netrestart2: 4: Password:: not found
netrestart2: 4: exp_continue: not found
netrestart2: 5: Syntax error: "}" unexpected


恩,还是谢谢前辈啦。继续研究中
moonchild
帖子: 116
注册时间: 2005-12-25 20:44

#4

帖子 moonchild » 2006-12-20 15:32

这个用here文档行么?

你试试
iamdxlwh
帖子: 26
注册时间: 2006-02-07 15:05

#5

帖子 iamdxlwh » 2007-05-06 22:39

echo 12345678 | sudo ifdown eth0
激水之疾,至于漂石者,势也;鸷鸟之疾,至于毁折者,节也。故善战者,其势险,其节短。
头像
5451vs5451
帖子: 345
注册时间: 2006-07-14 18:56
来自: Apple Valley, Planet Tux, Linux System

#6

帖子 5451vs5451 » 2007-05-06 23:46

代码: 全选

man sudo
...
       -S  The -S (stdin) option causes sudo to read the password from the
           standard input instead of the terminal device.
...

代码: 全选

echo 12345678 | sudo -S ifdown eth0
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#7

帖子 eexpress » 2007-05-07 0:53

man
学习。
没用过。
● 鸣学
Robinho
帖子: 22
注册时间: 2007-03-01 15:22
来自: 广州

#8

帖子 Robinho » 2007-05-12 8:41

还是出错
netrestart2: 2: spawn: not found
netrestart2: 3: expect: not found
netrestart2: 4: Password:: not found
netrestart2: 4: exp_continue: not found
netrestart2: 5: Syntax error: "}" unexpected

lz要先安装expect包[/quote]
回复