How to automate password entry?

后端 未结 5 2245
攒了一身酷
攒了一身酷 2020-12-16 06:46

I want to install a software library (SWIG) on a list of computers (Jenkins nodes). I\'m using the following script to automate this somewhat:

NODES=\"10.8.2         


        
相关标签:
5条回答
  • 2020-12-16 07:18

    With SSH the right way to do it is to use keys instead.

    # ssh-keygen
    

    and then copy the *~/.ssh/id_rsa.pub* file to the remote machine (root@$node) into the remote user's .ssh/authorized_keys file.

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

    You can perform the task using empty, a small utility from sourceforge. It's similar to expect but probably more convenient in this case. Once you have installed it, your first scp will be accomplished by following two commands:

    ./empty -f scp InstallSWIG.sh root@$node:/root/InstallSWIG.sh
    echo YOUR_SECRET_PASSWORD | ./empty -s -c
    

    The first one starts your command in the background, tricking it into thinking it's running in interactive mode on a terminal. The other one sends it data from stdin. Of course, putting your password anywhere on command line is risky due to shell history being preserved, users being able to see it in ps results etc. Not secure either, but a bit better thing would be to store the password in a file and redirect the second command's input from that file instead of using echo and a pipe.

    After copying to the server, you can run the script in a similar manner:

    ./empty -f ssh root@$node sh InstallSWIG.sh
    echo YOUR_SECRET_PASSWORD | ./empty -s -c
    
    0 讨论(0)
  • 2020-12-16 07:23

    Here’s an expect example that sshs in to Stripe’s Capture The Flag server and enters the password automatically.

    expect <<< 'spawn ssh level01@ctf.stri.pe; expect "password:"; send "e9gx26YEb2\r";'
    
    0 讨论(0)
  • 2020-12-16 07:25

    Wes' answer is the correct one but if you're keen on something dirty and slow, you can use expect to automate this.

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

    You could look into setting up passwordless ssh keys for that. Establishing Batch Mode Connections between OpenSSH and SSH2 is a starting point, you'll find lots of information on this topic on the web.

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