0

I am writing some scripts to get the result of remote server command using sshpass.

Before try to multiple remote host, I am trying to test with only one test server to get result of the command.

The command is to check if the user exist in the host. If the user exist, I want to print the result. So, I coded like below.

#!/bin/bash
exec sshpass -pMY_PASSWORD ssh -o StrictHostKeyChecking=no root@IP_ADDRESS grep USER /etc/passwd | cut -f1 -d:

if [ $? -ne 0 ]
    then 
        echo "command failed..."
        exit 1
fi

But If I try the script above, it print some server login message and below errors.

basename: extra operand `tty'
Try `basename --help' for more information.
basename: extra operand `tty'
Try `basename --help' for more information.

I only want to get the result if the user exist, and also without those errors.

Anyone could help?

1 Answer 1

0

The remote server has code in its login shell profile that is poorly written and cannot handle a non-interactive login such a this one. Fix that and the basename/ttyproblem will go away. (On the remote system, temporarily rename root's .profile and .bash_profile and try your ssh -o StrictHostKeyChecking=no root@IP_ADDRESS id. If you still get the error it's in the system login scripts; if you don't it's in root's login scripts. They are probably not protecting use of tty with [ -t 1 ] && ...)

However, since you're using exec to run the sshpass program, none of the remainder of the script you've shown us will be run. Remove exec. (Don't use it - ever - until you know why you do need to use it.) But even when you do this, that next code segment that tests for success will only check the result of the last command in the pipeline, ie. cut, which is highly unlikely to fail.

Is there any particular reason you're using sshpass with an embedded clear-text password rather than ssh with certificate based authentication?

Is there any particular reason you're logging in to the remote system as root rather than a non-privileged account?

1
  • Hello!, Thank you for your comment. Well first of all, our system does not allow login without using password currently, I don't know why but maybe because of the operational issue I guess. And secondly, how can I fix the remote server login shell profile..? What is it you mean with the poorly written profile?? I am quite not sure about that.
    – 홍종우
    Dec 2, 2020 at 9:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .