1

I need to run command as another user and write to its stdin and read from its stdout. I can run command as another user only via opening new shell: sudo su anotheruser but with new opened shell i will lost stdin and stdout of caller. I am not allowed to run sudo -u anotheruser -c command

I found out that i can use named pipes for stdin/stdout redirection, so i wrote a script:

#!/usr/bin/env bash

mkfifo pipein
mkfifo pipeout

echo '/usr/lib/openssh/sftp-server 0<pipein >pipeout' | sudo su anotheruser

cat 0<&1 >pipein & cat <pipeout

wait

But it doesn't work as desired. I want to redirect script's stdin to command's stdin and also redirect command's stdout to script's stdout - so i could seamlessly use the script as original command.

2

You seem to have some editing errors in your post. There is a "&" missing for the sudo line, and you are using different names for your pipes later in the script. Here is something that works for me:

#!/usr/bin/env bash
mkfifo pipein
mkfifo pipeout
echo '/usr/lib/openssh/sftp-server <pipein >pipeout' | sudo su anotheruser &
cat <pipeout &
cat >pipein
wait
rm -f pipein pipeout
  • 1
    works great with little tweak: mkfifo pipeout --mode=0777, depends on relation between user running script and anotheruser – isevcik Aug 18 '15 at 21:08
0

I'm not sure I understand your question completely but I think you are after something like this:

sudo -u $USER sh -c "ls -la /home" | grep $USER

Or if you want the pipe as the other user

sudo -u $USER sh -c "ls -la /home | grep $USER"
  • i can't use sudo -u -c, i am allowed only to run "sudo su anotherdeveloper" – isevcik Aug 18 '15 at 15: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.