使用 Bash 启动和停止 openconnect
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41944094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Start and stop openconnect using Bash
提问by user1867459
I am trying to achieve the following:
我正在努力实现以下目标:
./vpnconnect.sh start
should establish a VPN connection to a server../vpnconnect.sh stop
should terminate the VPN connection.
./vpnconnect.sh start
应该建立到服务器的 VPN 连接。./vpnconnect.sh stop
应终止 VPN 连接。
Here is the attempted shell script which doesn't work as expected. It gives error:
这是未按预期工作的尝试的 shell 脚本。它给出了错误:
~$ ./vpnconnect.sh stop
Stopping VPN connection:
./vpnconnect.sh: 22: ./vpnconnect.sh: root: not found
./vpnconnect.sh: 26: ./vpnconnect.sh: 14128: not found
The script:
剧本:
#!/bin/sh
#
#
#
#
PIDOCN=""
VAR2=""
# Start the VPN
start() {
echo "Starting VPN Connection"
eval $(echo 'TestVpn&!' | sudo openconnect -q -b --no-cert-check 127.0.0.1 -u myUser --passwd-on-stdin)
success $"VPN Connection established"
}
# Stop the VPN
stop() {
echo "Stopping VPN connection:"
VAR2=eval $(sudo ps -aef | grep openconnect)
echo $VAR2
eval $(sudo kill -9 $VAR2)
PIDOCN=eval $(pidof openconnect)
echo $PIDOCN
eval $(sudo kill -9 $PIDOCN)
}
### main logic ###
case "" in
start)
start
;;
stop)
stop
;;
status)
status openconnect
;;
restart|reload|condrestart)
stop
start
;;
*)
echo $"Usage: ./vpnconnect.sh: 22: ./vpnconnect.sh: root: not found
./vpnconnect.sh: 26: ./vpnconnect.sh: 14128: not found
{start|stop|restart|reload|status}"
exit 1
esac
exit 0
回答by janos
The error messages:
错误信息:
VAR2=eval $(sudo ps -aef | grep openconnect)
PIDOCN=eval $(pidof openconnect)
Come from these lines:
来自这些行:
stop() {
echo "Stopping VPN connection:"
sudo ps -aef | grep openconnect
sudo kill -9 $(pidof openconnect)
}
These lines are non-sense. The shell takes the output of the $(...)
sub-shells and tries to execute them as commands, with VAR2
and PIDOCN
variables set to "eval". This is definitely not what you wanted.
这些台词毫无意义。shell 获取$(...)
子shell 的输出并尝试将它们作为命令执行,VAR2
并将PIDOCN
变量设置为“eval”。这绝对不是你想要的。
Probably you're looking for something more like this:
可能你正在寻找更像这样的东西:
VAR2=eval $(sudo ps -aef | grep openconnect)
回答by codeforester
The issue is with eval
:
问题在于eval
:
VAR2=$(sudo ps -aef | grep openconnect)
Here, eval
will try to execute the output of sudo ps -aef | grep openconnect
command. That's the reason you are getting the errors you are seeing.
在这里,eval
将尝试执行sudo ps -aef | grep openconnect
命令的输出。这就是您收到您所看到的错误的原因。
Rewrite it as:
将其改写为:
##代码##Which will simply assign the output of the sudo
command pipeline to VAR2
variable. However, you can't use VAR2
as an argument to kill
because it contains other tokens like username along with the PID.
这将简单地将sudo
命令管道的输出分配给VAR2
变量。但是,您不能将其VAR2
用作参数,kill
因为它包含用户名和 PID 等其他标记。
In other places where you are doing eval $(command)
, all you need is command
.
在您正在做的其他地方,您只eval $(command)
需要command
.
You could use pkill openconnect
to kill any existing openconnect
processes instead of finding out the PID and issuing a kill
against it. pgrep
and pkill
are quite handy for start/stop/restart script like yours.
您可以使用pkill openconnect
杀死任何现有openconnect
进程,而不是找出 PID 并kill
针对它发出一个。pgrep
并且pkill
对于像您这样的启动/停止/重启脚本非常方便。