EN VI

Bash - How can I pass commands to SSH when using expect while it doesn not support using double quotes?

2024-03-10 20:00:06
Bash - How can I pass commands to SSH when using expect while it doesn not support using double quotes?

I'm automating my login process to my servers using an inline bash script, I'm using expect to automate this, but upon logging into server I also want to print a message, I didn't had any luck with echo because I read in a stackoverflow post that it's not reliable cause I want to use color coding, now I'm using printf to do this, printf works in the server perfectl but when passing it in this little script of mine I'm getting an error, I suspect that there is something wrong with the way I'm using curly braces instead of double quotes, I will be glad if someone can point out what is wrong with the way I'm putting the printf command in curly braces.

expect -c 'spawn ssh -t MYHOST@MYSERVER {printf [\33[01;32m A MESSAGE TO BE SHOWN \33[01;37m]\n; bash}; expect "Password:"; send "MY PASSWORD"; interact'

I'm getting these errors, there is absolutely something wrong with the I'm putting printf command inside curly braces:

bash: {printf: command not found bash: 32m: command not found bash: 37m]n}}: command not found

I tried using double quotes and many ways of using curly braces but didn't have any luck.

Thank you everyone.

Solution:

You need to put quotes around printf :

expect -c 'spawn ssh -t  MYHOST@MYSERVER "printf '"'"'\[\\33\[01\;32m\ \ A\ MESSAGE\ TO\ BE\ SHOWN\ \ \\33\[01\;37m\]\\n'"'"'; bash"; expect "Password:"; send "secret\r"; interact'

The command to run on the server is printf '...', as expect -c used the single quote, we need '"'"'.

If you think alternating quotes are confusing, you can use '\''

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login