-1

Sorry for the cross-posting across the fora. I am not sure if my original post was in the correct forum.


I am aware that what I would like to do is not safe...

I would like to open a window in my byobu session with root prompt. (i.e. with sudo su -.) Once I get the root prompt, I would like to execute my script (/root/command.sh)(as root) and then give me the root prompt again.

I can achieve the first part by putting the following line in my window.tmux:

new-window -n 'root' sudo su -;

Using send-keys, I can get the root prompt. However it is not executing the command.

new-window -n root sudo su -;
send-keys mypassword C-m;
send-keys source ~/command.sh C-m;

P.S.

For some reasons it is not possible to use visudo on these commands.

closed as off-topic by Rui F Ribeiro, muru, ctrl-alt-delor, user34720, slm Apr 4 at 3:31

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – Rui F Ribeiro, muru, ctrl-alt-delor, Community, slm

  • You don't need to use sudo and su together. sudo bash, or sudo -i will do it. – ctrl-alt-delor Apr 3 at 7:49
0

Su will flush the tty input buffer before waiting for the password, so you will need a delay before the send-keys or the password (which is likely to already be in the buffer by then) will be gone. Try adding "run 'sleep 1'" before the first send-keys.

  • it seems run 'sleep 1' command needs running tmux server. If I include this line in my tmux.conf then I am get the following error: > no server running on /tmp/tmux-1000/default – Mike V.D.C. Apr 3 at 10:28
  • How do you expect new-window and send-keys to work without a running tmux server? – Nicholas Marriott Apr 3 at 10:35
  • I mean run command doesnt seems to be acceptable in windows.tmux. The first list in the file is (before the lines mentioned in the post): new-session -AD -s 'root'; However when I add run-shell 'sleep 5'; before the first send-keys, it throws an error. – Mike V.D.C. Apr 3 at 10:44
  • 1
    This works fine for me: nicholas@macbook-air:~$ cat x new-window -n root cat; run "sleep 1"; send-keys abc; send-keys def; nicholas@macbook-air:~$ tmux -Ltest new\; source x – Nicholas Marriott Apr 3 at 10:50
  • Your code seems to be working for me too, however only if I source the file. The default way (using windows.tmux) is still a no-go for me. – Mike V.D.C. Apr 3 at 12:25

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