0

I have a remote device where telnetd daemon is running. I would like to execute a few sed commands to update the /etc/securetty file and add pts/0, pts/1...pts/n to this file on this device through a telnet connection. I wrote the following python function using telnetlib to execute the sed commands:

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)

The above produces the output as follows:

# 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

while the output I wanted was:

# 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

I believe that the sed command itself is wrong. How can I get the desired output?

PS : I'm using poky linux from yocto project and my sed version is mentioned as This is not GNU sed version 4.0.

skrowten_hermit
  • 437
  • 3
  • 11
  • 28
  • what is the output if you use `pts/0\npts` instead of `pts/0\\npts`? – Sundeep Apr 14 '17 at 05:34
  • When I replace `\\n` with `\n`, it gives `pts/0npts/1npts/2npts/3npts/4npts/5npts/6npts/7npts/8npts/9`. – skrowten_hermit Apr 14 '17 at 05:38
  • not sure, but python string formatting is causing trouble? in addition to earlier suggestion, try using `r""` – Sundeep Apr 14 '17 at 05:43
  • I just verified, the plain `sed` command itself is flawed. Executing it on terminal gives the same output. So, `python` is not the culprit I guess. – skrowten_hermit Apr 14 '17 at 05:46
  • you'll have to add sed version and OS you are using... `seq 5 | sed '$a pts/0\npts/1\npts/2\npts/3\npts/4\npts/5\npts/6\npts/7\npts/8\npts/9'` works as expected – Sundeep Apr 14 '17 at 06:24
  • see if this is helpful: https://stackoverflow.com/documentation/sed/9436/bsd-macos-sed-vs-gnu-sed-vs-the-posix-sed-specification/29246/append-literal-text-to-a-line-with-function-a#t=201704140643380944689 – Sundeep Apr 14 '17 at 06:44

0 Answers0