skip to Main Content

I’m running a script from Ubuntu 22 (WSL), and it was functioning OK until it suddenly stopped.

This script connects to remote servers and collects data via ssh using ssh-pass the code with the issue listed below.

for IP in `cat $1`
do

        SERVERVER=`eval "export SSHPASS='""$(echo $password  )""'";sshpass -e ssh  $username@$IP -o "ConnectTimeout 3" "uname -sn"`

if [ $? == 0 ]; then

$1 – IP list

The issue is ssh connection fail due to additional characters "$" and "r"

++ sshpass -e ssh $'[email protected]' -o 'ConnectTimeout 3' 'uname -sn'
+ SERVERVER=
+ '[' 255 == 0 ']'

I tried dos2unix but still same.

Expected

++ sshpass -e ssh '[email protected]' -o 'ConnectTimeout 3' 'uname -sn'

2

Answers


  1. I’m not familiar with sshpass so I can’t say anything about the validity of the command-line and options itself, but I would separate the commands and remove the eval:

    export SSHPASS="$password"
    SERVERVER="$(sshpass -e ssh "$username@$IP" -o "ConnectTimeout 3" "uname -sn")"
    

    The rouge r could be removed with tr:

    for IP in $(tr -d 'r' < "$1")
    do
        ...
    done
    
    Login or Signup to reply.
  2. The trace is pretty obvious:

     ++ sshpass -e ssh $'[email protected]' -o 'ConnectTimeout 3' 'uname -sn'
    

    So you have a carriage return character after the IP address. You get the IP address from

    cat $1
    

    which means that the file named by $1 has a carriage return. Since $1 is the first named parameter to your script, it means that this file is in error.

    Either pass a different (and correct) file to your script or remove the carriage returns from that file.

    An alternative would be to manually remove the carriage return(s) from the IP address in your script:

    IP=${IP//$'r'/}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search