Visit Our UNIX and Linux User Community

sed command inside paramiko python

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command inside paramiko python
# 1  
Old 01-29-2017
sed command inside paramiko python


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

dssh = paramiko.SSHClient()
        dssh.connect( host, username='xxx', password='123', timeout=120)

except Exception as e:

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

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


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
# 2  
Old 01-29-2017

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
# 3  
Old 01-29-2017
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

# 4  
Old 01-29-2017
Originally Posted by Priya Amaresh
Yes, outside python code it works fine.
I doubt it. I can confirm drl's analysis.
sed -i  -E 's/xmon=on//g' /etc/gr.txt > /etc/tempFile

How about adding the s ubstitute command to your first sed script?
# 5  
Old 01-29-2017
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 Smilie

Last edited by Priya Amaresh; 01-29-2017 at 03:56 PM.. Reason: Code seems messy

Previous Thread | Next Thread
Test Your Knowledge in Computers #630
Difficulty: Medium
According to Lifewire, as of November 1, 2018, a total of more than 3.2 billion iPhones had been sold.
True or False?

9 More Discussions You Might Find Interesting

1. Programming

Python Paramiko multi threading to capture all output for command applied in loop

My issue : I am getting only last command output data in ouput file. Though comamnd "print(output)" displays data for all 3rd column values but the data saved in file is not what required it hs to be the same which is being printed by command"print(output)". Could you please help me to fix this,... (0 Replies)
Discussion started by: as7951
0 Replies

2. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

3. Programming

Search or find a element inside list python

I have a list as follows: From this i need to grep the element using keyword as "primary" and return output as 12:13-internet-wifi-primary i used as follows if (i <= (len(system_info))): ss = system_info print... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

4. Shell Programming and Scripting

Executing sed command inside a bash script

I want to run commands inside a bash script. An example is I want to pass the command in a string as regexp as an argument to the script, then run sed on the bash variable regexp "-i \"s/<p>//g\"" then call sed "$regexp" $fl (3 Replies)
Discussion started by: Kangol
3 Replies

5. Shell Programming and Scripting

Paramiko Script

Hi Geeks, I have the following python script. #!/usr/bin/python #Script to pull KPI values from MMEs. import paramiko import sys import os host = sys.argv user = sys.argv password = sys.argv ssh = paramiko.SSHClient() (8 Replies)
Discussion started by: infinitydon
8 Replies

6. Shell Programming and Scripting

[Python] Search a file with paramiko

I need to compare the output files in a directory for sftp, looking through a mask. Return the full file name. Eg. I have a file named locally: test.txt I must check through sftp, if a file with the following name: test_F060514_H173148.TXT My idea is for the filename to a... (0 Replies)
Discussion started by: Jomeaide
0 Replies

7. Shell Programming and Scripting

sed command not working inside ksh script but works fine outside

Hi, I am a bit confused ,why would a sed command work fine outside of ksh script but not inside. e.g I want to replace all the characters which end with a value and have space at end of it. so my command for it is : sed -i "s/$SEPARATOR /$SEPARATOR/g" file_name This is working fine in... (8 Replies)
Discussion started by: vital_parsley
8 Replies

8. Shell Programming and Scripting

Need help in sed command ( Replacing a pattern inside a file with a variable value )

Hello, The following sed command is giving error sed: -e expression #1, char 13: unknown option to `s' The sed command is echo "//-----" | sed "s/\/\/---*/$parChk/g" where parChk="//---ee-" How can i print the variable value from sed command ? And is it possible to replace a... (2 Replies)
Discussion started by: frozensmilz
2 Replies

9. Shell Programming and Scripting

How to use refrence values inside sed command

I am using script S_db=`grep SRC_DB= ${login_txt} | cut -d= -f2` T_db=`grep TGT_DB= ${login_txt} | cut -d= -f2` today=`date +%Y%m%d` sed 's/USER_WORK/RAR_WORK_D1/g' < ${my_path}/files/${T_target}_${today}_DDL.txt > ${my_path}/files/temp9 But when I am trying to remove hardcode values... (1 Reply)
Discussion started by: scorp_rahul23
1 Replies

Featured Tech Videos