0

I'm writing a script which purpose is to connect to a number of servers and create an account. The "core" is:

ssh user@ip
sudo su -
useradd -m -p 123 $1
if [ $? -eq 0 ]; then
   echo "$1 successfully created on ip."
fi
chage -d 0 $1
chown -R $1 /home/$1

exit #exit root
exit #exit the server

I have established a private-public key relationship between the servers in order to be able to perform the ssh without being prompted for the password, however, when I run the script it does the ssh but then doesn't perform the next commands on the target machine. Instead, when manually exiting from the target server, I see that those commands were executed (or better said, tried to be executed) on the local machine.

2
  • 1
    You'd better do ssh user@ip -c "useradd..." Commented Feb 14, 2014 at 11:12
  • @mbratch solved with that question. ssh -l user ip "script" Commented Feb 14, 2014 at 13:18

3 Answers 3

0

So there should be no asking password when run both ssh and sudo command

ssh user@ip bash -c "'
sudo su -
useradd -m -p 123 $1
if [ $? -eq 0 ]; then
   echo "$1 successfully created on ip."
fi
chage -d 0 $1
chown -R $1 /home/$1

exit #exit root
exit #exit the server
'"
Sign up to request clarification or add additional context in comments.

Comments

0

If you are planning to sudo why don't you just ssh as root: root@ip? Just do:

ssh root@ip 'command1; command2; command3'

In your case if you want to be sure they are all successfull in order to proceed:

ssh root@ip 'USER=someUser; useradd -m -p 123 $USER && chage -d 0 $USER && chown -R $USER /home/$USER'

EDIT:

If the root access is not alowed if would do the following:

  1. Create the script with the commands you want to execute on the remote machine, for instance script.sh:

    #!/bin/bash
    USER=someUser 
    useradd -m -p 123 $USER && chage -d 0 $USER && chown -R $USER /home/$USER
    
  2. Copy the script to the remote machine:

    scp script.sh user@ip:/destination/dir
    
  3. Invoke it remotely:

    ssh user@ip 'sudo /destination/dir/script.sh'
    

EDIT2:

Other option without creating any files:

ssh user@ip "sudo bash -c 'USER=someUser && useradd -m -p 123 $USER && chage -d 0 $USER && chown -R $USER /home/$USER'" 

5 Comments

root access is not allowed.
made and edit to my answer.
that's the same Zsolt Botykai said on his point #2. My boss doesn't want that, he wants to "centralize" the scripts on a machine.
I don't understant what does "centralize the script on the machine" mean? So what would you like to achieve?
edited again, hope that this is what you need.
0

It won't work this way. You shoudl do it like:

  1. ssh user@ip 'yourcommands ; listed ; etc.' or
  2. copy the script you want to execute on the servers via scp /your/scriptname user@ip:/tmp/ then execute it ssh user@ip 'sh /tmp/yourscriptname'

But you are starting another script when starting sudo.

Now you have (at least) two options:

  1. ssh user@ip 'sudo -s -- "yourcommands ; listed ; etc."' or
  2. copy the part after the sudo to a different script, then:

    ssh user@ip 'sudo -s -- "sh differentscript"'`
    

2 Comments

I don't know why but the sudo su - is not working. For example, ssh user@ip 'ls -la'works and shows the ls of the user's home, but ssh user@ip 'sudo su - ; ls -la' does not return anything, I got to finish it by CTRL+C. It's not a privilege issue, as I can manually do ssh user@ip and then sudo su -
ls -la doesn't run until the shell started by sudo su - completes.