Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameters ignored in call()

I am trying to call ssh from a Python program but it seems to be ignoring the arguments.

This is the Python program:

#!/usr/bin/python

from subprocess import Popen, PIPE, call

vm_name = 'vmName with-space'
vm_host = '[email protected]'

def ssh_prefix_list(host=None):
    if host:
        # return ["ssh", "-v", "-v", "-v", host]
        return ["scripts/ssh_wrapper", "-v", "-v", "-v", host]
    else:
        return []

def start(vm_name, vm_host=None):  # vm_host defaults to None
    print "vm_host = ", vm_host
    vbm_args = ssh_prefix_list(vm_host) + ["VBoxManage", "startvm", vm_name]
    print vbm_args
    return call(vbm_args, shell=True)

start(vm_name, vm_host)

The wrapper prints the number of arguments, their values, and calls ssh.

#!/bin/bash

echo Number of arguments: $#
echo ssh arguments: "$@"
ssh "$@"

This is the output.

$ scripts/vm_test.py
vm_host =  [email protected]
['scripts/ssh_wrapper', '-v', '-v', '-v', '[email protected]', 'VBoxManage', 'startvm', 'atp-systest Clone']
Number of arguments: 0
ssh arguments:
usage: ssh [-1246AaCfgKkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-e escape_char] [-F configfile]
           [-i identity_file] [-L [bind_address:]port:host:hostport]
           [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
           [-R [bind_address:]port:host:hostport] [-S ctl_path]
           [-w local_tun[:remote_tun]] [user@]hostname [command]

This is on Python 2.5.

like image 906
Stephen Rasku Avatar asked Jan 21 '26 08:01

Stephen Rasku


1 Answers

When you are using shell=True , you need to pass in a string, not a list of arguments. Try -

return call(' '.join(vbm_args), shell=True)

Also, you should consider constructing the string from start, rather than list.

When you pass a list to call() or Popen() with shell=True , only the first element in the list is actually called , and that is the reason you are seeing the wrapper called with 0 arguments.

You should also try first without using shell=True , since its a security hazard, as clearly stated in the documentation of subprocess -

Using shell=True can be a security hazard. See the warning under Frequently Used Arguments for details.

like image 113
Anand S Kumar Avatar answered Jan 23 '26 22:01

Anand S Kumar