0

I am trying to use typeset command to execute the function remotely. but unfornately it exit with error.

i have two functions one is for user creation and one that will use to run function remotely

##./create_user -u test1 -g staff -c "test test"  -h host1
uadd() {
  if grep -E "$username" /etc/passwd >/dev/null; then
    echo "$username user already exists in the server $host"
    exit 1
  else

    if grep -E "$groupname" /etc/group >/dev/null; then
      /usr/sbin/useradd -G staff,"$groupname" -c "$comment" "$username"
      echo "$username user created with staff and $groupname groups!"
      exit 1
    else
      /usr/sbin/useradd -G staff -c "$comment" "$username"
      echo "$username user created with staff group only!"
      exit 1
    fi
  fi

}

remoteuadd() {

  #ssh sysadm@$host "$(typeset -f useradd); useradd"
  ssh root@"$host" -p 5829 "$(declare -f uadd);uadd"

  #ssh root@"$host" -p 5829 "$(printf "%q " bash -c "$(declare -f uadd); uadd")"

  #typeset -f uadd | ssh -v  root@$host -p 5829  `$(cat); `

  #ssh root@"$host" -p 5829 bash -s <<EOF
  #$(declare -f uadd)
  #uadd
  #EOF

}

its working but result looks like this

+ remoteuadd
+++ typeset -f uadd
++ uadd '()' '{' egrep '"$username"' /etc/passwd '>' '/dev/null;' if '[' '$?' == 0 '];' then echo '"$username' user already exists in the server '$hostname";' exit '1;' else egrep '$groupname' /etc/group '>' '/dev/null;' if '[' '$?' == 0 '];' then /usr/sbin/useradd -G 'staff,$groupname' -c '"$comment"' '$username;' echo '"$username' user created with '$groupname' 'groups!";' exit '1;' else /usr/sbin/useradd -G staff -c '"$comment"' '$username;' echo '"$username' user created with staff group 'only!";' exit '1;' 'fi;' fi '}'
++ egrep test1 /etc/passwd
++ '[' 0 == 0 ']'
++ echo 'test1 user already exists in the server '
++ exit 1
+ ssh root@localhost -p 5829 test1 user already exists in the server
bash: test1: command not found
+ exit 1

as you can see script try to create user if not exits and so it works and found that user already exits but still script didn't exit it catch the result value and run again and ultimately script break with error.

Any suggestion highly appreciated.

Regards, Mir

Léa Gris
  • 17,497
  • 4
  • 32
  • 41
M. H
  • 1
  • 2
  • replace `if grep -E "$username" /etc/passwd >/dev/null; then` with `if getent passwd "$username" >/dev/null` and replace `grep -E "$groupname" /etc/group >/dev/null` by `if getent group "$groupname" >/dev/null` which do not make assumption that these databases are actual local files. – Léa Gris May 22 '22 at 13:53
  • Thank you @Lea Gris, good point. actually i have to run script which run remotely and remove user on remote server which i will mention in my command.. – M. H May 22 '22 at 14:14

1 Answers1

1

to execute the function remotely

Yet, you are using ` backticks which execute stuff locally. You are exeucuting $(typeset -f uadd);uadd and then executing ssh with the result of the function.

The most understandable is a redirection:

ssh root@"$host" -p 5829 bash -s <<EOF
$(declare -f uadd)
uadd
EOF

Note that ssh kind-of-evals the argument. You can also double-escape for ssh:

ssh root@"$host" -p 5829 "$(printf "%q " bash -c "$(declare -f uadd); uarr")"

Notes:

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thank you @KamilCuk. But if i didn't use ` backticks script didn't pick the variables values. and i have tried to use declare but still it didn't work. – M. H May 22 '22 at 10:37
  • `if i didn't use backticks script didn't pick the variables values` the script you presented uses no variable values, and sure it didn't - you can't expect remote process on a remote computer to know about your variable values, you have to serizlize them with `declare -p`. `still it didn't work` You have to post what exactly "didn't work", otherwise it's empty statement, too vague. – KamilCuk May 22 '22 at 11:56
  • See https://stackoverflow.com/questions/66086353/unable-to-use-variables-in-remote-ssh-command/66086438#66086438 – KamilCuk May 22 '22 at 11:57
  • please check the post again i have edit it with exact code of piece. Please check again. I think something i missed but not sure what i am going to miss. – M. H May 22 '22 at 12:00