bashsftpsshpass

sshpass want to use parameter of sftp


Hi i created following script to initialize my storage box to use rsync without password later. Last year it works if i remember correct...

cat .ssh/id_rsa.pub >> .ssh/storagebox_authorized_keys
echo -e "mkdir .ssh \n chmod 700 .ssh \n put $.ssh/storagebox_authorized_keys" \
    ".ssh/authorized_keys \n chmod 600 .ssh/authorized_keys" | sshpass -p ${storage_password} \
    sftp -P ${storage_port} -i .ssh/id_rsa ${storage_user}@${storage_address}

today I get following error:

sshpass: invalid option -- 'i'

but the parameter -i belongs to sftp and not sshpass - is there an possibility to parse the parameters in the correct way?

edit: i switched the position of

-i .ssh/id_rsa ${storage_user}@${storage_address}

and get this error

sshpass: Failed to run command: No such file or directory

edit: it seems like an sftp problem...


Solution

  • after discussion, updating answer to properly support automation

    step 1:

    create an sftp "batch file" e.g: ~/.ssh/storage-box_setup.sftp

    mkdir .ssh
    chmod 700 .ssh
    put /path/to/authorized_keys_file ".ssh/authorized_keys
    chmod 600 .ssh/authorized_keys
    

    /path/to/authorized_keys_file is a file containing public keys of ONLY the keys that should have access to your storage box (.ssh/storagebox_authorized_keys)

    step 2:

    update automation script command to

    sshpass -p <password> -- sftp -P <port> -b ~/.ssh/storage-box_setup.sftp user@host
    

    the -b flag was the answer you needed. refer: man sftp

    -b batchfile

    Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication.


    --

    sshpass -p ${storage_password} -- \
        sftp -P ${storage_port} -i .ssh/id_rsa \ 
        ${storage_user}@${storage_address}
    

    the -- before sftp is a way to tell sshpass (and most other programs) to stop parsing arguments.

    everything after -- is assumed as the last argument, which in the case of sshpass is the command to be executed ssh -i ~/.id_rsa ...


    in case you're wondering switching the position of -i tells sshpass to execute -i as a program and hence fails with command not found