简体   繁体   English

后台进程挂起(tty 输出)

[英]Background process suspended (tty output)

I've a python script which calls a bash script to connect a vpn account.我有一个 python 脚本,它调用 bash 脚本来连接一个 vpn 帐户。 If I run python script from console, I can do 2FA and connect to my VPN account seamlessly.如果我从控制台运行 python 脚本,我可以执行 2FA 并无缝连接到我的 VPN 帐户。 However, if I run this python script as a background process (with nohup , etc.) then the python process becomes suspended ( + suspended (tty output) ) whenever I try to connect VPN and python program is not responding (looks like it is stuck in a state that expects an input).但是,如果我将此 python 脚本作为后台进程(使用nohup等)运行,那么每当我尝试连接 VPN 并且 python 程序没有响应时,python 进程就会暂停( + suspended (tty output) )卡在需要输入的 state 中)。

vpn_manager.py : vpn_manager.py

connection_command = 'sh {}'.format(os.path.join(base_path, 'scripts', 'vpn.sh'))

response = subprocess.run(connection_command, shell=True, stdout=subprocess.PIPE)
stdout = response.stdout.decode('utf-8')

if 'state: Connected' in stdout:
    update_icon(environment)

Shell script vpn.sh : Shell 脚本vpn.sh

printf "1\nUSERNAME\nPASSWORD\n2\n" | /opt/cisco/anyconnect/bin/vpn -s connect VPN_HOST

Normally, this VPN command asks for a user name and password, then waits for me to verify it from my 2FA app on my phone.通常,此 VPN 命令要求输入用户名和密码,然后等待我通过手机上的 2FA 应用程序进行验证。

How can I make this python code working as a background process and not interrupted with that VPN prompts?我怎样才能使这个 python 代码作为后台进程工作并且不会被那个 VPN 提示打断?

Using pexpect is a better approach to communicate as @CharlesDuffy suggested.正如@CharlesDuffy 建议的那样,使用pexpect是一种更好的沟通方式。

Solution with pexpect will be similar to below example.使用pexpect的解决方案将类似于以下示例。

import pexpect

failed = False
vpn = pexpect.spawn('/opt/cisco/anyconnect/bin/vpn -s connect {}'.format(host))
ret = vpn.expect([pexpect.TIMEOUT, CONNECT_SUCCESS, CONNECT_ERR_1, CONNECT_ERR_2, ...])
if ret != 1:
    failed = True

if not failed:
    vpn.sendline('1')
    ret = vpn.expect([pexpect.TIMEOUT, SELECT_GROUP_SUCCESS, SELECT_GROUP_ERR_1, SELECT_GROUP_ERR_2, ...])
    if ret != 1:
        failed = True

if not failed:
    vpn.sendline(USER_NAME)
    ret = vpn.expect([pexpect.TIMEOUT, USER_NAME_SUCCESS, USER_NAME_ERR_1, USER_NAME_ERR_2, ...])
    if ret != 1:
        failed = True

if not failed:
    vpn.sendline(PASSWORD)
    ret = vpn.expect([pexpect.TIMEOUT, PASSWORD_SUCCESS, PASSWORD_ERR_1, PASSWORD_ERR_2, ...])
    if ret != 1:
        failed = True

if not failed:
    vpn.sendline(AUTHENTICATION_METHOD)
    ret = vpn.expect([pexpect.TIMEOUT, AUTHENTICATION_SUCCESS, AUTHENTICATION_ERR_1, AUTHENTICATION_ERR_2, ...])
    if ret != 1:
        failed = True

if not failed:
    print('Connected!')
else:
    print('Failed to connect!')

Send the output to a file (or named pipe)将 output 发送到文件(或命名管道)

printf "1\nUSERNAME\nPASSWORD\n2\n" | /opt/cisco/anyconnect/bin/vpn -s connect VPN_HOST > /tmp/vpn.out

Then in you python script you can check that file content until you read the expected content.然后在您的 python 脚本中,您可以检查该文件内容,直到您阅读预期的内容。

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

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