Home > Blockchain >  Interactive ssh session started from bash command
Interactive ssh session started from bash command

Time:09-21

I'm trying to launch an interactive ssh session created from a bash command (as shown below).

If I do not add the 'ttt', I just get a 'non interactive' session message and the ssh session is closed.

After adding the 'ttt' flag, the session remains open but I can not enter commands or interact with the remote session.

This is a simplified version of the command I'm trying to execute

 echo "username@host" | xargs -I{} bash -x -c 'ssh -ttt" $0' {}

Not sure if there is an additional flag that should be added or alternative way of doing this.

CodePudding user response:

Input to xargs (and therefore bash and therefore ssh) is coming from the pipe, not your terminal. Depending on the larger context, one of these options should work to let ssh read from the terminal:

  • Capture the list of hosts to an array first, then use a for loop instead of xargs. Something like this:

     readarray -t hostarray < <(echo "username@host")
     for host in "${hostarray[@]}"; do
         ssh "$host"
     done
    
  • Add the -o option to xargs, which tells it to redirect stdin of the command from /dev/tty (i.e. the terminal).

  • Redirect the regular stdin around the xargs command via a different file descriptor, e.g. #3 (note: I haven't tested this):

     { echo "username@host" | xargs -I{} bash -x -c 'ssh -ttt "$0" <&3' {}; } 3<&0
    

BTW, in addition to problems with ssh not getting input it's supposed to get (i.e. from the terminal), you can also have trouble with it stealing input that was intended for something else, like parts of the list of hosts you wanted xargs to read. See BashFAQ #89: I'm reading a file line by line and running ssh or ffmpeg, only the first line gets processed!

  • Related