Embedding the Password in the Bash Script

前端 未结 3 1822
闹比i
闹比i 2020-12-22 11:28

I am running a test script where files needs to be copied to the target embedded system.But when this command of copying the files to remote target system is run from the sc

3条回答
  •  借酒劲吻你
    2020-12-22 12:00

    You can use sshpass to pass the password to the scp. Something like

    sshpass -p passw0rd scp test.file1 :/home/bot21/test/
    

    But as already mentioned, using the keys is recommended. I added the following notes to SO Documentation before it was retired.


    Connecting from script using password

    When you really need to script ssh connection, piping the password into the ssh command does not work (echo passw0rd | ssh host). It is because the password is not read from standard input, but directly from TTY (teleprinter, teletypewriter, Teletype for historical reasons).

    But there is sshpass tool which works around this problem. It can read the password from parameter, file or environment variable. But note that none of these options does not satisfy the security requirements for a passwords!

    $ sshpass -p passw0rd ssh host
    $ sshpass -f /secret/filename ssh host
    $ SSHPASS=passw0rd sshpass -e ssh host
    

    The command line options can be seen by other users in ps (during runtime it is masked, but not during start time and you can't rely on it):

    ... 23624  6216 pts/5    Ss   Aug30   0:00  \_ /bin/bash
    ... 12812  1988 pts/5    S+   08:50   0:00  |   \_ sshpass -p passw0rd ssh host
    ... 45008  5796 pts/15   Ss+  08:50   0:00  |       \_ ssh host
    

    Note, that environemnet variables of a process are also accessible by other processes on the system using /proc/PID/environ file.

    Finally, storing the password in the file might look like the best possible idea, but still using keys as described in the other examples is preferred way to use ssh.

提交回复
热议问题