0
stdout = session.exec_command('''find {} -name "{}*" -type f -exec stat -c "%n %y" {}<--**This is bash, not variable of format** + | awk "{ print $1" "$2 }" | grep -v word'''
.format(VARIABLE1, VARIABLE2))

I'm doing this code using paramiko 2.4 on Django 1.9.13, don't know how to deal with it, the error is:

KeyError at /get_xml/index/ ' print $1" "$2 '

UPDATED:

It doesn't crash right now, but doesn't returns anything, but if I do on bash, it's working, think there's missing something...

command = find {} -name '{}*.xml' -type f -exec stat -c '%n %y' {{}} + | awk '{{print $1, $2, $3}}' | grep -v 'word'

stdout = session.exec_command(command)

ANSWER

ssh.connect(hostname=VARIABLE1, username=VARIABLE2)
command = "find {} -name '{}*.xml' -type f -exec stat -c '%n %y' {{}} + | awk '{{print $1, $2, $3}}' | grep -v WHATIDONTWANTTO".format(VARIABLE3, WHATIWANTTOSEARCH)
_, stdout, _ = ssh.exec_command(command)
for item in stdout:
     #do whatever with items
     pass

finally it works, the last way was with session = ssh.get_transport().open_session() but it always retrieve 'Nonetype'

7
  • VARIABLE3 is missing. Commented Oct 5, 2018 at 7:09
  • updated, it belongs to bash command, is NOT a variable from python Commented Oct 5, 2018 at 7:12
  • awk "{ print $1,$2 }" <file_name>. where is your file name here ? Commented Oct 5, 2018 at 7:31
  • That awk won't work, due to the double quotes. Change it to awk '{print $1 " " $2}' or even simpler, awk '{print $1, $2}' Commented Oct 5, 2018 at 7:38
  • 1
    In addition to the curly brace problem, you can't use double quotes inside a double-quoted string. Your Awk script should probably be single-quoted instead. (Also, avoid the useless use of grep; or, better yet, do the output filtering in Python.) Commented Oct 5, 2018 at 7:38

1 Answer 1

2

As per documentation on .format, if you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.

So this -exec stat -c "%n %y" {} should be -exec stat -c "%n %y" {{}} and so on, if I understood your comment correctly.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.