Jul-17-2019, 06:43 PM
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
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