简体   繁体   English

通过Python连接CISCO Anyconnect VPN

[英]Connect CISCO Anyconnect VPN via Python

I am trying to connect to CISCO Anyconnect VPN via python function. 我正在尝试通过python函数连接到CISCO Anyconnect VPN。 I have seen the ways given here: Connect CISCO Anyconnect VPN via bash 我已经看到了这里给出的方法: 通过bash连接CISCO Anyconnect VPN

But first I have a couple of problems, the first one being new to Python and second being reluctant to use any/very-few third-party modules/libraries other than https://github.com/cernekee/stoken . 但是首先我有两个问题,第一个是Python的新问题,第二个是不愿意使用https://github.com/cernekee/stoken以外的任何/很少的第三方模块/库。

Here, stoken gives me the RSA SecureID token for authentication when I run: 在这里,当我运行时, stoken为我提供了用于认证的RSA SecureID令牌:

$ stoken tokencode

My password consists of a 4-digits pin say, WXYZ which is concatenated with the RSA token for the passcode input. 我的密码包含一个4位数的密码,即WXYZ ,该密码与用于输入密码的RSA令牌串联。

I came up with the following one-liner bash command to connect which works so far from my OSX: 我想出了以下单线bash命令进行连接,该命令到目前为止与OSX无关:

'(echo "user.name";echo "WXYZ$(stoken tokencode)"; echo y )> vps.ad ; /opt/cisco/anyconnect/bin/vpn -s connect "vpn.domain" < vps.ad'

Now, in my python script, I tried using both os and subprocess modules to do the same but failed spectacularly. 现在,在我的python脚本中,我尝试同时使用os子进程模块来执行相同操作,但是失败了。

I have several VPN domains and two different pins(like, WXYZ) which I am trying to put together to replicate the above bash command. 我有几个VPN域和两个不同的引脚(例如WXYZ),我试图将它们放在一起以复制上述bash命令。 The function now looks like this: 该函数现在如下所示:

def __auth_cisco_vpn__(username, pin, domain):
try:
    token = str(os.popen('stoken tokencode', 'r'))

    pre_prcs = '(echo "' + username + '";echo "' + pin + '$(stoken tokencode)"; ' + 'echo y )> vps.ad ; /opt/cisco/anyconnect/bin/vpn -s connect "' + domain + '" < vps.ad'

    subprocess.Popen(pre_prcs, shell=True, executable="/bin/bash", stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE).communicate()
    return True

except KeyboardInterrupt:
    return False

I managed to get the following error when I try to get the token separately as below: 当我尝试分别获取令牌时,出现以下错误:

pre_prcs = '(echo "'+username+'";echo "'+pin+'$'+token+'"; ' + 'echo y )> vps.ad ; /opt/cisco/anyconnect/bin/vpn -s connect "'+domain+'" < vps.ad'

subprocess.Popen(pre_prcs, shell=True, executable="/bin/bash", stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

TypeError: cannot concatenate 'str' and 'file' objects TypeError:无法连接“ str”和“ file”对象

What should I do? 我该怎么办? Many thanks in advance. 提前谢谢了。

After a little tweaking, I found out the problem and come up with the following solution. 稍作调整后,我发现了问题并提出了以下解决方案。 Notice the error with the quotations in # Assign cmd section below: 请注意以下“ #分配cmd”部分中的引号引起的错误:

import subprocess

def __auth_cisco_vpn__(username, pin, domain):

    # Grab Token
    proc = subprocess.Popen(['stoken', 'tokencode'],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    store = list(proc.stdout)
    token = store[0].strip()

    # Assign cmd
    credentials = "printf '" + username + "\\n" + pin + token + "\\ny'"
    vpn_cmd = "/opt/cisco/anyconnect/bin/vpn -s connect '" + domain + "'"
    cmd = credentials + " | " + vpn_cmd

    # Command Execution
    print("Executing Command: \n" + cmd)
    subprocess.Popen(cmd,
                     shell=True,
                     executable="/bin/bash",
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE).communicate()

__auth_cisco_vpn__('user.name', 'WXYZ', 'domain')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM