0

Is it possible to use she-bang in oneliners?

I am trying to run this script from https://stackoverflow.com/questions/16928004/how-to-enter-ssh-password-using-bash, in one liner.

If I put this in a file, and run it with ./file it works as expected, but how would I be able to run that as oneliner?

This works as a script without any issues.

#!/usr/bin/expect -f
spawn ssh user@my.server.com
expect "assword:"
send "mypassword\r"
interact

How would I be able to run that from oneliner?

If I try to run

spawn ssh user@my.server.com; expect "assword:"; send "mypassword\r"; interact

it returns:

bash: spawn: command not found

If I try to run

#!/usr/bin/expect -f; spawn ssh user@my.server.com; expect "assword:"; send "mypassword\r"; interact

then the whole thing is treated as a comment and nothing happens.

EDIT:

tried running stuff suggested in answers:

expect -c `spawn ssh user@server "ls -lh /some/file"; expect "assword:"; send "mypassword\r"; interact`

it returned

bash: spawn: command not found
couldn't read file "assword:": no such file or directory
bash: send: command not found
bash: interact: command not found
expect: option requires an argument -- c

When running:

$ cat << 'EOF' | /usr/bin/expect -
> spawn ssh user@server "ls -lh file"
> expect "assword:"
> send "password\r"
> interact
> EOF

It seems to SSH to server, but doesn't run the command, or it doesn't output any result of it:

it returns:

spawn ssh user@server ls -lh file
user@server password

When I run it from script:

./test
spawn ssh user@server ls -lh file
user@server's password:
-rw-rw-r-- 1 user user 467G Jan  2 00:46 /file

EDIT2:

Issue with first command was with backtick instead of single quote, following command works as expected

expect -c 'spawn ssh user@server "ls -lh file"; expect "assword:"; send "mypassword\r"; interact'

:

  • 1
    In your expect -c command, you have used backticks in place of single quotes. You also appear to have an extra hyphen i.e. --c in place of -c. And what is the "ls -lh file" doing in there? You don't mention it in your original question. – steeldriver Jan 2 '16 at 0:50
  • 1
    I tried the extra hypen when one returned expect: option requires an argument -- c, it was the backticks, when I used single quotes it works as expected thanks. – rAlen Jan 2 '16 at 0:54
3

As a one-liner, you'd pass the script as the argument to expect -c

expect -c 'spawn ssh user@my.server.com; expect "assword:"; send "mypassword\r"; interact'

Note that the shell's quotes are '...' and "...", and expect's corresponding quotes are {...} and "...", so there's some flexibility for passing shell vars and so forth.

But that quickly becomes unreadable and unmaintainable.

  • doesn't work for me; returns bash: spawn: command not found couldn't read file "assword:": no such file or directory bash: send: command not found bash: interact: command not found expect: option requires an argument -- c tried with -f also – rAlen Jan 2 '16 at 0:36
1

expect can read its script from the standard input:

cat << 'EOF' | /usr/bin/expect -
spawn ssh user@my.server.com
expect "assword:"
send "mypassword\r"
interact
EOF
  • doesn't work as expected, when doing this it doesn't execute the command I have in ssh, for instance ls $ cat << 'EOF' | /usr/bin/expect - > spawn ssh user@server "ls -lh file" > expect "assword:" > send "password\r" > interact > EOF spawn ssh user@server ls -lh file user@server password: – rAlen Jan 2 '16 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.