I will send a string from a local machine to a remote ssh server.
The user is in a simple jail, so he can't execute commands, login, do something on the remote bash or what ever, he can only send a string/value/variable and execute the script run
on the ssh server not more.
This is the simple jail for the user
This is are the default settings for the USER to login and run a shell:
chsh -s /bin/bash [USER]
But i change the settings to this for the simple jail:
chsh -s /home/[USER]/./run [USER]
The ssh server receive the string and change the string to a new result.
I save this new result on my local machine in a file called OUTPUT
.
After trying i found this solution, call it a hack or a bug, but i don't know if this is the correct way!
- With this command i ssh to the remote server, with a ssh-key without a password.
MY SSH COMMAND ON THE LOCAL MACHINE TO CONNECT TO THE SERVER:
ssh -i [KEY] -p [XXXX] [USER]@[HOST] '123' <<< "$VALUE" > OUTPUT
- The script
run
receive the string123
from the variable$VALUE
MY SCRIPT WITH NAME run
ON THE SSH SERVER
#!/bin/bash
echo "WELCOME TO SSH"
read VALUE
echo $VALUE
This is only an example to See what i mean, the real Script generate a Hash.
- On my local machine i store/save the result in my file
OUTPUT
from the scriptrun
of the remote ssh server.
MY LOCAL FILE OUTPUT
WITH THE RESULT FROM THE SERVER
WELCOME TO SSH
123
The read VALUE
command saves the value 123
in the variable $VALUE
without a prompt and save the result on my local machine.
THE EXECUTION STEPS:
- Connect to ssh server
- Send a string from my local machine to the remote ssh server
- The remote server change this string to a new result
- Save the new result on my local machine in a file
When i start the ssh command it opens connection,
the script works, i see WELCOME TO SSH
on my terminal and
i receive the result on my local machine and the connection close.
You can easy test this example on your machine to see what i mean.
This is exactly what i need but i wonder:
Why i need the read
Why i get not a prompt.
What is an other solution for this example.
How can i send a string to a script on a server over ssh with a jailed user and save the result from the server on my local machine in a file?
UPDATE FOR SOLUTION
From @ilkkachu answer i can run this commands and send:
String and Variable:
ssh -i [KEY] -p [XXXX] [USER]@[HOST] "123" "$VALUE" > OUTPUT
String:
ssh -i [KEY] -p [XXXX] [USER]@[HOST] "123"> OUTPUT
Variable:
ssh -i [KEY] -p [XXXX] [USER]@[HOST] "$VALUE" > OUTPUT
Nothing:
ssh -i [KEY] -p [XXXX] [USER]@[HOST] > OUTPUT
#!/bin/bash
value= # empty by default
if [ "$1" = -c ]; then
value="$2"
fi
if [ -z "$value" ]; then
echo "value is empty" >&2
exit 1
fi
# do something with the value
echo "$value" | rev
SSH authorized_keys command option
From this post:
SSH authorized_keys command option: multiple commands?
No. It is not "allowed" command, but "forced" command (as ForceCommand option). The only possibility is to use different keys for different command or read parameters from stdin.
It is a good tip but for a single command, like a backup or output some informations.
I have to do more config stuff if i run a command like rsync
, if i don't wanna receive errors.
So i will stay with a simple jailed user and a given script.