How to Launch Linux Subshell for Ssh_Askpass to Work in Linux

ssh with SSH_ASKPASS always fails

You need to export the environment variables to apply for your ssh command:

export DISPLAY
export SSH_ASKPASS

Running the ssh with -vvv would tell you that the SSH_ASKPASS command is not used.

How to use tee with sshpass in shell script

How about

sudo -S sh -c 'cat /tmp/hosts >> /etc/hosts' <<< "password"

It's best to contain redirections for sudo within a subshell so that the elevated permissions are applied to opening the destination file.

ref: See https://stackoverflow.com/a/4327123/7552

How the make command invokes other subshells for recipe execution

You don't see calls to fork because the actual system call is clone. You'll see this if you inspect the output of strace. I like to save the strace output in a file and look at it afterwards:

strace -o trace make all

If I have a Makefile that looks like this:

three: one two
cat one two > three

one:
date > one

two:
date > two

Then after running the strace command above, I have:

$ grep clone trace
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f6a3570ce50) = 29836
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f6a3570ce50) = 29838
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f6a3570ce50) = 29840

From the fork man page:

   C library/kernel differences
Since version 2.3.3, rather than invoking the kernel's fork() system
call, the glibc fork() wrapper that is provided as part of the NPTL
threading implementation invokes clone(2) with flags that provide the
same effect as the traditional system call. (A call to fork() is
equivalent to a call to clone(2) specifying flags as just SIGCHLD.)
The glibc wrapper invokes any fork handlers that have been
established using pthread_atfork(3).

Why is this while loop not looping?

sshpass is taking control of stdin or possibly replacing it and causing while loop to lose input from redirected stdin.

To work around this issue, avoid reading from stdin.

First, load the file into an array using a while loop.

while read line; do
entries+=("$line")
done < servers.txt

Next, use for loop to parse the lines and execute sshpass within this loop.

for line in "${entries[@]}"; do
set $line
ip=$1
user=$2
pass=$3
echo $ip
sshpass -p "$pass" ssh $user@$ip -o StrictHostKeyChecking=no -T "
echo 'yo'
"
echo 'done'
done

The second loop doesn't read from stdin.

But I will recommend Rany Albeg Wein answer using a separate descriptor than the current stdin.

while read ip user pass <&3; do 
echo $ip
sshpass -p "$pass" ssh $user@$ip -o StrictHostKeyChecking=no -T "
echo 'yo'
"
echo 'done'
done 3<servers.txt

How to sudo su; then run command

Unless you have an unusual setup, you can't normally string su with other preceding commands like that. I would imagine it is running sudo su, then hanging in the root environment/session, because it's waiting for you to exit before preceding to the pm2 commands. Instead, I would consider something along the lines of this using the -c option:

CMD="sudo su -c 'pm2 restart 0; pm2 restart 1'"
ssh -i somepemfile.pem ubuntu@1.1.1.1 "$CMD"

As suggested in another answer, it would also probably be useful to encapsulate the $CMD variable in quotes in the ssh call.



Related Topics



Leave a reply



Submit