通过 python paramiko ssh 会话执行时 wine 命令失败

Wine command fails when executed through python paramiko ssh session

我正在尝试通过 ssh 在我的机器上使用文本转语音程序 balabolka 及其命令行版本 Balcon 运行ning ubuntu 20.04.1 LTS。 首先我从 http://www.cross-plus-a.com/fr/bconsole.htm, Then after some research I managed to get it to work, this required installing Wine, winetricks, Microsoft speechPlatformRuntime, speechsdk and msxml6. I also installed some SAPI 5 TTS voices. I followed the steps described in https://askubuntu.com/questions/1189046/wine-how-to-use-sapi-5-voices-for-tts-application-balabolka

下载了 balcon

我在测试的时候使用这个命令行:WINEPREFIX="$HOME/prefix32" wine "$HOME/prefix32/drive_c/Program Files/balcon/balcon/balcon.exe" -f text.txt -n Daniel_Full_22kHz -w audiooutput.wav

当 运行 直接在主机上时,此命令完美运行,但我的目标是从另一台机器通过 ssh 运行 此命令。出于测试目的,我尝试直接从 windows 命令行并使用 python + paramiko.

进行 ssh 连接

这是奇怪的部分:重新启动 Ubuntu 机器后,第一次尝试使用任何提到的方法都会成功,但后续尝试总是失败。

使用直接 ssh 连接时出现以下错误:

~$ WINPREFIX="$HOME/prefix32" wine "$HOME/prefix32/drive_c/Program Files/balcon/balcon/balcon.exe " -f text.txt -n Daniel_Full_22kHz -w audiooutput.wav 0009:err:winediag:nodrv_CreateWindow Application tried to create a window, but no driver could be loaded. 0009:err:winediag:nodrv_CreateWindow Make sure that your X server is running and that $DISPLAY is set correctly. 0009:err:ole:CoGetClassObject class {d941651c-44e6-4c17-badf-c36826fc3424} not registered 0009:err:ole:create_server class {d941651c-44e6-4c17-badf-c36826fc3424} not registered 0009:err:ole:CoGetClassObject no class object {d941651c-44e6-4c17-badf-c36826fc3424} could be created for context 0x5 0009:err:ole:CoGetClassObject class {cb96b400-c743-11cd-80e5-00aa003e4b50} not registered 0009:err:ole:CoGetClassObject class {cb96b400-c743-11cd-80e5-00aa003e4b50} not registered 0009:err:ole:create_server class {cb96b400-c743-11cd-80e5-00aa003e4b50} not registered 0009:err:ole:CoGetClassObject no class object {cb96b400-c743-11cd-80e5-00aa003e4b50} could be created for context 0x7 Error: voice not selected

该错误表明 $DISPLAY 环境变量存在问题。使用 echo $DISPLAY 我发现该变量未在 ssh 控制台中设置,因此我使用 export DISPLAY=:0 设置它 值 :0 是通过 运行ning echo $DISPLAY 本地获取的ubuntu ssh 服务器。修改后所有后续尝试都正常。

我尝试使用 python+paramiko 做同样的事情,这是我的脚本:

import os
import shell
import paramiko

def connectSSH(key,host,user):
    c = paramiko.SSHClient()
    c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    print("connecting")
    c.connect( hostname = host, username = user, pkey = k )
    ftp_client = c.open_sftp()
    print("connected")
    filet = open("./text.txt","w")
    filet.write("Hi this is a text to speech test with daniel voice")
    filet.close()
    ftp_client.put("./text.txt","./text.txt")
    env_dict = {"DISPLAY":":0"}
    cmd = "WINPREFIX=\"$HOME/prefix32\" wine \"$HOME/prefix32/drive_c/Program Files/balcon/balcon/balcon.exe \" -f %s -n %s -w %s" % ("kesra2.txt", "Daniel_Full_22kHz","audiooutput.wav")
    print(cmd)
    stdin,stdout,stderr=c.exec_command(cmd,environment=env_dict)
    print(stdout.readlines())
    print(stderr.readlines())
    ftp_client.get("audiooutput.wav","audiooutput.wav")
    ftp_client.close()

起初我得到了同样的 $DISPLAY 错误,所以我使用 env_dict = {"DISPLAY":":0"} 添加它并允许在服务器 sshd 配置上修改这个变量,但现在我总是得到错误:

0009:err:ole:CoGetClassObject class {d941651c-44e6-4c17-badf-c36826fc3424} not registered 0009:err:ole:create_server class {d941651c-44e6-4c17-badf-c36826fc3424} not registered 0009:err:ole:CoGetClassObject no class object {d941651c-44e6-4c17-badf-c36826fc3424} could be created for context 0x5 0009:err:ole:CoGetClassObject class {cb96b400-c743-11cd-80e5-00aa003e4b50} not registered 0009:err:ole:CoGetClassObject class {cb96b400-c743-11cd-80e5-00aa003e4b50} not registered 0009:err:ole:create_server class {cb96b400-c743-11cd-80e5-00aa003e4b50} not registered 0009:err:ole:CoGetClassObject no class object {cb96b400-c743-11cd-80e5-00aa003e4b50} could be created for context 0x7 Error: voice not selected

错误的最后一部分 Error: voice not selected 并不重要,因为在其他情况下使用相同的 cmd,其他原因失败但我找不到它。此外,我不明白为什么无论使用何种方法(ssh 控制台或 python + paramiko)

,重新启动服务器后的第一次尝试都可以正常工作

我尝试使用 paramiko 的 invokeshell() 得到相同的结果

任何帮助将不胜感激

经过进一步研究,问题似乎与“WINEPREFIX”变量有关。通过将其移入 env_dict 并使用绝对路径,问题不再重现

env_dict = {"DISPLAY:":0", "WINEPREFIX":"absolute/path/to/prefix"}