Python Forum

Full Version: Long command with characters not working in Python on Oracle Linux 7
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have below python script which connects to a host, runs a command and prints the output.

This script is working fine and printing the output of df command.

But when I replace the df command (inside cmd = list2cmdline) with long find command as follows, the script fails.

find /OVS -wholename '*VirtualMachines/*/vm.cfg' -exec grep -H 'simple_' {} \; 2>/dev/null | awk {'print $3'} | tr -d "'"

Above command lists the VM names in OVM Manager deployments. After putting above line the python script fails with below syntax error.

cmd = list2cmdline(shlex.split("""find /OVS -wholename '*VirtualMachines/*/vm.cfg' -exec grep -H 'simple_' {} \; 2>/dev/null | awk {'print $3'} | tr -d "'""""))
^
SyntaxError: EOL while scanning string literal


Can someone let me know what changes are needed for making the python script to work?




import paramiko
from subprocess import list2cmdline
import shlex

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='<hostname>', username='<username>', password='<password>')

cmd = list2cmdline(shlex.split("""df -h"""))

stdin, stdout, stderr = ssh.exec_command(cmd)

for line in stdout.read().splitlines():
print(line)

ssh.close()



- Iaas.Infra
Well, you start the string with three double-quotes (""") and end it with six. So out of the six, the first three end the string, and the next three confuse Python. Do you need the three at the end of the command? If so, escape them ('\"\"\"') to keep them from ending the string.
I removed all the double quotes except beginning one and ending one.

I still see the syntax error as follows:

[root@tboyella-python tboyella-scripts]# python ./test.py
File "./test.py", line 9
cmd = list2cmdline(shlex.split("find /OVS -wholename '*VirtualMachines/*/vm.cfg' -exec grep -H 'simple_' {} \; 2>/dev/null | awk {'print $3'} | tr -d "'""))


Below is the actual command I want to put in double quotes.

find /OVS -wholename '*VirtualMachines/*/vm.cfg' -exec grep -H 'simple_' {} \; 2>/dev/null | awk {'print $3'} | tr -d "'"

Above command works fine. But when I put in python script inside double quotes it fails.

Let me know if there is any changes we have to make to have above command work in python script.

Iaas.Infra
Try this:

cmd = list2cmdline(shlex.split("find /OVS -wholename '*VirtualMachines/*/vm.cfg' -exec grep -H 'simple_' {} \; 2>/dev/null | awk {'print $3'} | tr -d \"'\""))
thanks I tried to change command as you suggested.

When I run python I do not get any errors. But the output is not shown.

printing the command via python I see that the command is as follows:

find /OVS -wholename *VirtualMachines/*/vm.cfg -exec grep -H simple_ {} ; 2>/dev/null | awk "{print $3}" | tr -d '

Original command is below

find /OVS -wholename '*VirtualMachines/*/vm.cfg' -exec grep -H 'simple_' {} \; 2>/dev/null | awk {'print $3'} | tr -d "'"

As we can see the \ is removing the double quotes. Without double quotes above command will not work.

Is there a way we can modify the command so that it is parsed correctly by Python and executed on remote server?
I don't know what's going on. This is what I get with the string on my machine:

Output:
>>> text = "find /OVS -wholename '*VirtualMachines/*/vm.cfg' -exec grep -H 'simple_' {} \; 2>/dev/null | awk {'print $3'} | tr -d \"'\"" >>> text 'find /OVS -wholename \'*VirtualMachines/*/vm.cfg\' -exec grep -H \'simple_\' {} \\; 2>/dev/null | awk {\'print $3\'} | tr -d "\'"'
Now, the representation of the string that Python spits out is flipped: the single-quotes are escaped and the double-quotes are not. But the two double-quotes are clearly there at the end of the string.
Below is the script I have with the command you suggested.

Output:
import paramiko from subprocess import list2cmdline import shlex client=paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) cmd = list2cmdline(shlex.split("find /OVS -wholename '*VirtualMachines/*/vm.cfg' -exec grep -H 'simple_' {} \; 2>/dev/null | awk {'print $3'} | tr -d \"'\"")) client.connect('XX.XX.XX.XX',22, username='XXXX', password='XXXX') output="" stdin, stdout, stderr = client.exec_command(cmd) print "ssh succuessful. Closing connection" stdout=stdout.readlines() client.close() print "Connection closed" print stdout print cmd for line in stdout: output=output+line if output!="": print output else: print "There was no output for this command"


So when I run the python as you can see there is no error. But the command did not return any output. And if you look at the command being printed its different than what is desired.

Error:
ssh succuessful. Closing connection Connection closed [] find /OVS -wholename *VirtualMachines/*/vm.cfg -exec grep -H simple_ {} ; 2>/dev/null | awk "{print $3}" | tr -d ' There was no output for this command
Guys any pointers on this?
I looked at it again. The string is fine. Using shlex.split() on the string retains the part you say is missing from the final command. So the problem must be in list2cmdline. If that's a function you wrote, we need to see the code.
shlex.split()is not the function I have written. I removed that function from my script. This is how the line looks now.

cmd = list2cmdline("find /OVS -wholename '*VirtualMachines/*/vm.cfg' -exec grep -H 'simple_' {} \; 2>/dev/null | aw
k {'print $3'} | tr -d \"'\"")
This is the output I get now.

Error:
python test.py ssh succuessful. Closing connection Connection closed [] f i n d " " / O V S " " - w h o l e n a m e " " ' * V i r t u a l M a c h i n e s / * / v m . c f g ' " " - e x e c " " g r e p " " - H " " ' s i m p l e _ ' " " { } " " \ ; " " 2 > / d e v / n u l l " " | " " a w k " " { ' p r i n t " " $ 3 ' } " " | " " t r " " - d " " \" ' \" There was no output for this command
Pages: 1 2