Daniel
Daniel

Reputation: 673

Passing arguments from Python to bash doesn't work as expected

I'm writing a script that should create a public RSA key and push it to the authorized_keys.

Here is the function that I use in Python:

def push_key():
    auth_keys_contents = None
    with open('/tmp/{}/public.pub'.format(args.username), 'r') as f:
        auth_keys_contents = f.read()
    print(auth_keys_contents) #just for testing
    os.system('ssh -l root server2 -i ~/.ssh/id_rsa "bash -s" < /home/scripts/script.sh {} {}'.format(args.username, auth_keys_contents))

The bash script that you see me running with the ssh line is simple. Here's a short version of it:

ssh_dir="/home/${1}/.ssh"
auth_keys_file="/home/${1}/.ssh/authorized_keys"
su $1 -c 'bash -s' <<EOL
...
...
...
echo "$2" > $auth_keys_file
EOL

For some reason, when it gets to this line: echo "$2" > $auth_keys_file the output is not as I expect it to be:

user@server2 ~/.ssh [54]> cat authorized_keys
ssh-rsa
user@server2 ~/.ssh [55]>

I don't understand why it's only getting the ssh-rsa instead of the full public key. I tried different switches for echo, I tried printf but the result is the same.


Seems like @Thomas solved it. Another approach that solved it was replacing this:

os.system(' ssh -l root server2 -i ~/.ssh/sid_rsa "bash -s" < /home/scripts/script.sh {} {}'.format(args.username, auth_keys_contents))

With this:

ssh_cmd = "bash -s {} {}".format(shlex.quote(args.username), shlex.quote(auth_key_contents)) os.system('ssh -l root server2 -i ~/.ssh/sid_rsa {} < /home/scripts/script.sh'.format(shlex.quote(ssh_cmd)))

Issue was that I have 2 nested shells and that I needed to quote twice as well.

Upvotes: 0

Views: 103

Answers (1)

Thomas
Thomas

Reputation: 17412

The problem is that there is a space character after ssh-rsa in a public key, so when you pass it unquoted to a shell script, it will split the different space-separated parts into separate parameters.

Try replacing your current line

echo "$2" > $auth_keys_file

with

echo "${@:2}" > $auth_keys_file

This is not ideal because multiple consecutive whitespace characters will get collapsed into a single space, but I believe that is no problem in your specific case.

Upvotes: 2

Related Questions