sed command inside paramiko python

Hi

I am trying to execute a sed command inside paramiko which finds and deletes the particular string from a file

But sed command doesnt work inside paramiko python

machine=qwe
dssh = paramiko.SSHClient()
dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
        dssh.connect( host, username='xxx', password='123', timeout=120)

except Exception as e:

    ln="SSH to machine:%s FAILED" % ( machine )
    print_log(ln)
    exit_status()

        grpRem = 'sed -i -E "/paste=used//g" /etc/gr.txt > /etc/tempFile'
        stdin, stdout, stderr = dssh.exec_command( grpRem )
        if stdout.channel.recv_exit_status() != 0:
            err="cmd=%s FAILED" % (grpRem)
            print_log(grpRem)
            dssh.close()
            exit_status()
        else:
            print "Copying temp file back to original file"
            #fileMv = 'mv /etc/tempFile /etc/gr.txt'
    else:
        print "paste=used not found.."
else:
    print "the system is inside else loop"

dssh.close()

The file is:

cat gr.txt | grep paste
        i have a paste=used so that it wont be available for anyone
    I do not have a paste=used

My expected output should be (paste=used should be deleted)

cat gr.txt
        i have a  so that it wont be available for anyone
    I do not have a 

Please help

---------- Post updated at 06:00 AM ---------- Previous update was at 05:26 AM ----------

I tried with get_pty=True even then no luck seen..

grpRem = 'sed -i -E "/xmon=on//g" /etc/grub2.cfg > /etc/tempFile,get_pty=True'

Please help

Hi.

I ran your embedded sed command:

sed -i -E "/paste=used//g" <file>

where <file> contained:

        i have a paste=used so that it wont be available for anyone
    I do not have a paste=used

and I received the error message:

sed: -e expression #1, char 13: unknown command: `/'

which, upon inspection, I expected, because the sed command looked incorrect to me.

On a system like:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30
sed (GNU sed) 4.2.2

Have you ever run your sed command outside of the python code?

Note that I have supplied the information regarding the environment in which I ran this code. We encourage you to do the same.

Best wishes ... cheers, drl

Yes, outside python code it works fine.
And inside python code I see the entire command is printed as output and the command is not getting executed as in the follwoing

sed -i -E "/paste=used//g" /etc/gr.txt > /etc/tempFile

---------- Post updated at 01:20 PM ---------- Previous update was at 01:13 PM ----------

And outside python, this format command worked fine

sed -i  -E 's/xmon=on//g' /etc/gr.txt > /etc/tempFile

I doubt it. I can confirm drl's analysis.

How about adding the s ubstitute command to your first sed script?

It worked this way finally when i used sed command inside paramiko python..

        grpRem = "sed -i -E 's/paste=used//g' /etc/gr.txt"        

Its surprising that my try worked :slight_smile: