Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading