传递键盘传递的脚本中的参数

时间:2013-09-06 07:25:01

标签: bash shell

我需要编写一个shell脚本,帮助我在执行此脚本后自动连接到vpn

vpnc程序需要以下输入

root@xmpp3:/home/test/Desktop/ScriptTovpnc# vpnc
Enter IPSec gateway address: 
Enter IPSec ID for : 
Enter IPSec secret for @: 
Enter username for : 
Enter password for @: 
vpnc: unknown host `'

我无法编写脚本,我将如何在该脚本中传递所有这些参数。

1 个答案:

答案 0 :(得分:2)

anishsane's comment是对的。使用配置文件!

但是,以防这里是expect脚本自动输入您的数据:

#!/usr/bin/expect

spawn vpnc

expect "Enter IPSec gateway address;"
send "yourdata\r";

expect "Enter IPSec ID for"
send "yourdata\r";

expect "Enter IPSec secret for"
send "yourdata\r";

expect "Enter username for"
send "yourdata\r";

expect "Enter password for"
send "yourdata\r";

如果将大部分数据作为命令行参数传递为suggested by Jonathan,则可以使其更小:

#!/usr/bin/expect

spawn vpnc --gateway yourgateway --id yourid --username yourusername

expect "Enter IPSec secret for"
send "yourdata\r";

expect "Enter password for"
send "yourdata\r";

但正如已经提到的,它不是要走的路。请改用配置文件。