Use Expect in a Bash script to provide a password to an SSH command

前端 未结 9 688
轮回少年
轮回少年 2020-11-22 06:23

I\'m trying to use Expect in a Bash script to provide the SSH password. Providing the password works, but I don\'t end up in the SSH session as I should. It goes back strait

相关标签:
9条回答
  • 2020-11-22 06:38

    sshpass is broken if you try to use it inside a Sublime Text build target, inside a Makefile. Instead of sshpass, you can use passh: https://github.com/clarkwang/passh

    With sshpass you would do:

    sshpass -p pa$$word ssh user@host
    

    With passh you would do:

    passh -p pa$$word ssh user@host
    

    Note: Do not forget to use -o StrictHostKeyChecking=no. Otherwise, the connection will hang on the first time you use it. For example:

    passh -p pa$$word ssh -o StrictHostKeyChecking=no user@host
    

    References:

    1. Send command for password doesn't work using Expect script in SSH connection
    2. How can I disable strict host key checking in ssh?
    3. How to disable SSH host key checking
    4. scp without known_hosts check
    5. pam_mount and sshfs with password authentication
    0 讨论(0)
  • 2020-11-22 06:44

    The easiest way is to use sshpass. This is available in Ubuntu/Debian repositories and you don't have to deal with integrating expect with Bash.

    An example:

    sshpass -p<password> ssh <arguments>
    sshpass -ptest1324 ssh user@192.168.1.200 ls -l /tmp
    

    The above command can be easily integrated with a Bash script.

    Note: Please read the Security Considerations section in man sshpass for a full understanding of the security implications.

    0 讨论(0)
  • 2020-11-22 06:45

    Another way that I found useful to use a small Expect script from a Bash script is as follows.

    ...
    Bash script start
    Bash commands
    ...
    expect - <<EOF
    spawn your-command-here
    expect "some-pattern"
    send "some-command"
    ...
    ...
    EOF
    ...
    More Bash commands
    ...
    

    This works because ...If the string "-" is supplied as a filename, standard input is read instead...

    0 讨论(0)
  • 2020-11-22 06:49

    Mixing Bash and Expect is not a good way to achieve the desired effect. I'd try to use only Expect:

    #!/usr/bin/expect
    eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com
    
    # Use the correct prompt
    set prompt ":|#|\\\$"
    interact -o -nobuffer -re $prompt return
    send "my_password\r"
    interact -o -nobuffer -re $prompt return
    send "my_command1\r"
    interact -o -nobuffer -re $prompt return
    send "my_command2\r"
    interact
    

    Sample solution for bash could be:

    #!/bin/bash
    /usr/bin/expect -c 'expect "\n" { eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com; interact }'
    

    This will wait for Enter and then return to (for a moment) the interactive session.

    0 讨论(0)
  • 2020-11-22 06:53

    A simple Expect script:

    File Remotelogin.exp

        #!/usr/bin/expect
        set user [lindex $argv 1]
        set ip [lindex $argv 0]
        set password [lindex $argv 2]
        spawn ssh $user@$ip
        expect "password"
        send "$password\r"
        interact
    

    Example:

    ./Remotelogin.exp <ip> <user name> <password>
    
    0 讨论(0)
  • 2020-11-22 06:54

    Use the helper tool fd0ssh (from hxtools, not pmt). It works without having to expect a particular prompt from the ssh program.

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