如何从命令行获取linux屏幕标题

时间:2012-11-10 12:55:51

标签: python gnu-screen

如何从命令行获取screen会话的标题?

1 个答案:

答案 0 :(得分:1)

我想出了一个非常小而简单的python脚本pexpect来完成它。

在多用户环境中很方便,其中保留了一些主机并且用户将状态写入屏幕标题。 它对我有用,随意让它变得更好。 要获取特定会话标题,您需要修改脚本并调用正确的会话。

如果您通过远程连接作为本地脚本运行(例如通过SSH),请记住在执行前设置export TERM=xterm

try:
    import pexpect
    import sys
    child=pexpect.spawn('screen -x')
    child.sendcontrol('a');
    child.send('A');
    i = child.expect('Set window.*')
    child.sendcontrol('c');
    child.sendcontrol('a');
    child.send('d');
    TITLE=str(child.after)
    TITLE_P=TITLE.split('7m')
    if str(TITLE_P[-1]) == '':
        print 'Title not found'
    else:
        print str(TITLE_P[-1])
except:
    print 'Could not check screen Title'