使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:40:54  来源:igfitidea点击:

Start and stop openconnect using Bash

linuxbashshell

提问by user1867459

I am trying to achieve the following:

我正在努力实现以下目标:

  • ./vpnconnect.sh startshould establish a VPN connection to a server.
  • ./vpnconnect.sh stopshould 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 VAR2and PIDOCNvariables 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, evalwill try to execute the output of sudo ps -aef | grep openconnectcommand. 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 sudocommand pipeline to VAR2variable. However, you can't use VAR2as an argument to killbecause 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 openconnectto kill any existing openconnectprocesses instead of finding out the PID and issuing a killagainst it. pgrepand pkillare quite handy for start/stop/restart script like yours.

您可以使用pkill openconnect杀死任何现有openconnect进程,而不是找出 PID 并kill针对它发出一个。pgrep并且pkill对于像您这样的启动/停止/重启脚本非常方便。