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'
:
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.