使用python的telnetlib

时间:2017-04-14 05:15:14

标签: python sed text-processing telnetlib

我有一个运行telnetd守护程序的远程设备。我想执行一些sed命令来更新/etc/securetty文件,并通过telnet连接将pts/0, pts/1...pts/n添加到此设备上的此文件中。我使用python编写了以下telnetlib函数来执行sed命令:

def add_terminals(self):
    print "Adding enough terminals for telnet...."
    self.telnet.write("sed -i '/pts\/[0-9]/d' /etc/securetty")
    self.telnet.write("\n")
    time.sleep(1)
    self.telnet.write("sed -i '$a \ ' /etc/securetty")
    self.telnet.write("\n")
    time.sleep(1)
    self.telnet.write("sed -i '$a \# Local X displays added by me' /etc/securetty")
    self.telnet.write("\n")
    time.sleep(1)
    self.telnet.write("sed -i '$a pts/0\\npts/1\\npts/2\\npts/3\\npts/4\\npts/5\\npts/6\\npts/7\\npts/8\\npts/9' /etc/securetty")
    self.telnet.write("\n")
    time.sleep(1)

以上产生如下输出:

# Local X displays added by me                              
pts/0\npts/1\npts/2\npts/3\npts/4\npts/5\npts/6\npts/7\npts/8\npts/9

虽然我想要的输出是:

# Local X displays added by me
pts/0
pts/1
pts/2
pts/3
pts/4
pts/5
pts/6
pts/7
pts/8
pts/9

我认为sed命令本身是错误的。如何获得所需的输出?

PS:我使用的是来自poky linux的{​​{1}},我的yocto project版本被称为sed

0 个答案:

没有答案