SSH through another id

First off, I don't have root permissions :wall:. All I have is sudo rights.

Im trying to ssh with another ID to multiple servers without the server asking for a password. The other ID already has ssh keys on all the other servers and can pass through the other server with a password of passphrase. But I can't use that same other ID in a script to pass through other servers without using a password or the the script not doing anything at all. Here is the current script I have now:

#!/bin/bash
 
echo "`date +%D:%H:%M:%S`" ; read -p " User ID"= UserID
 
echo "`date +%D:%H:%M:%S`"
read -p "Enter Server(s)"= servertext

for i in $servertext
do
        echo "$i"
        echo "`date +%D:%H:%M:%S`"
        ssh -t -t  ssh -f -o "Batchmode yes"   -o "StrictHostKeyChecking no" AnotherID@$i   id $UserID
done

If I take out the -o "Batchmode yes" , and leave in -o "StrictHostKeyChecking no" the server that I'm ssh -ing to asks for a password.

If I leave in the -o "Batchmode yes" and take out -o "StrictHostKeyChecking no" , enter a service name , as soon as I press enter, it does this :

Enter Server(s)= servername

[User@servername~] $

Goes back to the command prompt.

In the other ID, i've already made a config file in the .ssh directory. In the config file are these options :

 
StrictHostKeyChecking no
FallBackToRsh no
BatchMode yes
CheckHostIP no
UsePrivilegedPort no

That's all I have.

If you can do 'sudo bash' you have root permissions.

The other user possesses a key file which allows him to do so. If your user doesn't have the same key, with the right file permissions on that key, it won't go.

ssh -v -v might show more about what's being refused when.

Didn't have the same permission's. Thats what it was. I have one more problem. I'm trying to execute a function an put a "if/then" statement within the single line of ssh command.

echo "`date +%D:%H:%M:%S`" ; read -p " User ID"= UserID

echo "`date +%D:%H:%M:%S`"
read -p "Enter Server(s)"= servertext


for i in $servertext
do

echo "$i"
echo "`date +%D:%H:%M:%S`"

 
ssh -t -t $i 'if [ $? -eq 0 ] ; then printf "User Exist"  -a  pcffunc ; else echo " user does not exist" ;  fi '
 
pcffunc() {
 
   functions.....
 
 
}
 
done

The script will do the first part but won't execute the function. Any thoughts?

Functions entered on localhost won't appear on the other host.

I have no idea what this '-a' option is and can't find it in any of my manual pages.

I have no idea what that shell statement is supposed to accomplish either since, if the user doesn't exist, you'll never log into the host in the first place and NONE of the statements in single quotes will run.

I looked on some other pages and the -a takes the place of && . I mistated what I was trying to do (forgive me :D). This what I was trying to do (reset a password):

#!/bin/bash

echo "`date +%D:%H:%M:%S`" ; read -p " User ID"= UserID
echo "`date +%D:%H:%M:%S`" ; read -p "User Password"= userpasswd 

echo "`date +%D:%H:%M:%S`"
read -p "Enter Server(s)"= servertext

for i in $servertext
do

echo "$i"
echo "`date +%D:%H:%M:%S`"

ssh -t -t $i 'if [ $? -eq 0 ] ; then printf "User Exist" -a pcffunc ; else echo " user does not exist" ; fi '

pcffunc() {

  echo $userpasswd |  sudo /usr/bin/passwd --stdin $UserID


}

done

I tried to do it the ssh portion like this :

ssh -t -t $i 'if [ $? -eq 0 ] ; then printf "User Exist" && echo $userpasswd | sudo /usr/bin/passwd --stdin $UserID ; else echo " user does not exist" ; fi '

This is the result:

User Exist
ssh: echo | sudo /usr/bin/passwd --stdin : Name or service not known

It's like it dismisses my function which you said: Functions entered on localhost won't appear on the other host.

But why does it dismiss my varibles?

Variables from your end don't go to the other end either, and don't get substituted before they're sent because you put them in single quotes.

Your program still makes no sense. Why would you test whether ssh succeeded inside ssh? If it doesn't work, the program will never happen. I'm not sure what you're even trying to do.

Just reset a simple password. I'm not testing is ssh is on ssh. Im testing if the user is on the server. if the user is then reset the password. If not the echo the user is not on the server.

If the user isn't present on the server, ssh won't login in the first place and no reset will happen. So if the statement executes at all, then they must exist.

The ' around && aren't optional. They're what make the echo happen on the remote end instead of the local one -- your shell sees the quotes and considers it a string, strips them off, and adds it to ssh's arguments. The remote end sees a raw && and considers it part of its own shell statement, and follows the logic as appropriate.

echo "$userpassword" | ssh -t -t $i sudo /usr/bin/passwd --stdin $UserID '&&' echo "'$UserID reset'"

Any one else have any thoughts? Any help would be most
appreciative

---------- Post updated at 09:01 AM ---------- Previous update was at 08:55 AM ----------

Sorry Corona688, didn't see your last post. Thank you for the imput. I'll try the new command you purposed.

It gave me this as a result:

tcgetattr: Invalid argument
Changing password for user userid.
passwd: Authentication information cannot be recovered

Why did it give the error "tcgetattr: Invalid argument" ?

Did you get the same thing or something different?