문제

I'm writing a bash script which calls a lot of other scripts.
Several scripts have to be executed as user_1 but several ones as user_2.
The scripts should be called in strict sequence. I start my script as user_1 and use su many times to become user_2. These times su requires a password so I have to retype it many times. I'd like to avoid that, but su has no password parameter.
sudo and expect are not installed.

도움이 되었습니까?

해결책 4

I have solved the problem using ssh. I have generated the authentication key for user_1 and published to user2. Password is not needed any more.

ssh -l user_1 hostname  "command_1; command_2"

다른 팁

  • Make a fork (using '&') for user_1 and for user_2. They should communicate (similar as client-serwer) using 'lock file' (semaphore). For example user_1 doing something and user_2 checking is 'lock file' exist in while ...sleep loop

  • Maybe that is stupid, but you can use ssh user@localhost end keys

If you happen to have zsh, I think you can do this easily - something like:

#!/bin/zsh
coproc user1_script
su user2
do_stuff
echo "your_turn" >&p
read MY_TURN
do_more_stuff
echo "your_turn" >&p
read MY_TURN
...

user1_script:

#!/bin/zsh
read MY_TURN
do_stuff
echo your_turn
read MY_TURN
do_more_stuff
...

If you're stuck, and have zsh, it's worth a try anyway.

You could run as root. Sounds risky? Not if you are careful and precede each command with su and add the username like so:

su -c 'script1' user_1 && su -c 'script2' user_2

This will (assuming you start as root) change the user to user_1 before running script1 then back to root then change to user_2 to run script2.. all without asking for multiple passwords.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top