1

I'm trying to make a script connecting via an SSH connection to a server and executing some commands. The first part works:

#!/usr/bin/expect -f
spawn ssh address
expect "password:"
send "password\r"
interact

but after that I want to execute some more commands, e.g cd to directory, launch some more scripts etc. Is there any way to implement these things ?

2 Answers 2

1

try following:

#!/usr/bin/expect
set login "any_user"
set addr "some_address"
set pw "any_pwd"

spawn ssh -t $login@$addr
expect "$login@$addr\'s password:"
send "$pw\r"
expect "~" ; # put here string from your server prompt
send "mkdir some_dir\r"
interact

This is one of the command, you could try other commands like cd, any other scripts too in it and let us know if any queries.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, this works, thanks a lot! As I understand, I need to set expect command each time I want to send anything
0

There are 2 easier approaches than using spawn, expect, interact, etc.:

  1. The easiest approach is to execute a command with ssh. For example listing directories of your remote with ls:
# Either enter the password in a prompt...
ssh [email protected] ls

# ...or use sshpass to avoid the password prompt
SSHPASS='XXXXX' sshpass -e ssh [email protected] ls
  1. Create a SSH tunnel. There are a lot of tutorials in the web on how to do this in different ways.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.