Bash: controlling SSH

后端 未结 8 2087
执念已碎
执念已碎 2020-12-03 15:26

I have this bash file, which asks for IP, password, etc. for OpenSSH to a device.

Now, if i use ssh root@ip, i have to enter the password. This is reall

相关标签:
8条回答
  • 2020-12-03 16:02

    The proper way to do this without storing passwords in plaintext on your machine is with ssh. First run:

    ssh-keygen

    This will generate a new SSH key in ~/.ssh/id_rsa.pub. After that simply run:

    ssh-copy-id user@my.server.com

    If you're on OS X or another machine that does not have "ssh-copy-id" there are one-line alternatives such as this one:

    cat ~/.ssh/id_rsa.pub | ssh user@machine "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"

    Ultimately you just need to append the contents of ~/.ssh/id_rsa.pub on your local machine to ~/.ssh/authorized_keys on the remote server. How you do that is up to you, the above are just quick shortcuts to do that.

    0 讨论(0)
  • 2020-12-03 16:07

    Even if I would use pem keys for this and this is an old topic, I also wanted to quote sshpass

    0 讨论(0)
  • 2020-12-03 16:10

    Expect is the usual tool for automating interactive sessions.

    0 讨论(0)
  • 2020-12-03 16:18

    Have you considered Paramiko? It's a Python-library for interacting with SSH.

    0 讨论(0)
  • 2020-12-03 16:19

    You can use this script: https://github.com/aprey10/ssh-authorizer

    It also allows to skip ssh keys passphrase request.

    0 讨论(0)
  • 2020-12-03 16:20

    The proper way to go is to copy the keys as has been said here. To add something to the conversation, there are cases where sshpass can be handy.

    The question asks specifically about scripting in a system with SSH. If it is the development of an embedded system, it can be useful to combine sshpass with command line options, as it reads on this post

    sshpass -p raspberry ssh pi@192.168.0.145
    

    this can be combined with

    ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no pi@192.168.0.145
    

    to avoid confirmation questions that prevent scripting from happening.

    Again, only use this in development systems where different machines share an IP and security is not important.

    https://ownyourbits.com/2017/02/22/easy-passwordless-ssh-with-sshh/

    0 讨论(0)
提交回复
热议问题