Search code examples
bashsshexpect

How to hand over many commands into expect command?


I have to connect with ssh to server and run a lot of commands. Solution as I know is script like this

expect -c 'spawn -noecho ssh '"admin"@"server"' "sudo bash -c \"command1\"";
      expect "*assword:*"; send "'${user_password}'\r";
      expect eof'

repeated several times. But I don't want to connect every time.

Maybe you know how to run command1 command2 ... in one connection?

Thank you


Solution

  • From the man ssh, you can do

    ssh [user@]hostname [command]
    

    you can also pipe commands and use multiple commands like this:

    ssh user@hostname "command1; command2; command3"
    

    @SMA is also correct, you can create a script on your server and use:

    ssh user@hostname 'bash myscript.sh'