jeudi 10 septembre 2020

Shell Script: Cant use some variables in SSH

This is David.

I have a problem in shell script. It's a function for unlock users. Im trying something similar like:

#!/bin/bash
read -r -p " Give me your name: " NAME
read -r -p " Give me Machines: " MACHINES
for i in $MACHINES; do
    
   ssh root@$i " uname ;
   
    if [ $SSO = "SunOS" ] # FOR SunOS OS
    then 
            passwd -u $NAME
            if [ $? = 0 ]
            then 
                printf "%s\n" "OK "
            else
                printf "%s\n" "FAIL"
            fi
                
    elif [ $STAT = "HP-UX" ] # For HP-UX OS
    then
            /usr/lbin/modprpw -k $NAME 
            if [ $? = 0 ]
            then 
                printf "%s\n" "OK"
            else
                printf "%s\n" "FAIL"
            fi              
            
    else
            passwd -u $NAME # For REDHAT OS
            if [ $? = 0 ]
            then 
                printf "%s\n" "OK"
            else
                printf "%s\n" "FAIL"
            fi
    
    fi
    "
 done

My problem is that i need to use ("") in ssh line, for use $NAME and $MACHINES variables but "uname" is executing in my local machine, not in the remote host. Also, i tried to use export command, but nothing. When i put this line out of my script, testing in my local machine, i get this error:

 export LOCAL = $(uname`)
 bash: "export: `=': not a valid identifier"
 bash: export: `HP-UX': not a valid identifier

$(uname) is correct. If i use "uname" without export command, inside ssh line, do it with local machine result. (My local machine is HP-UX)

How can i execute "uname" in my remote host and use that result with "if" sentences? Thanks!

Aucun commentaire:

Enregistrer un commentaire