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.

3 Answers3

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"'`
    
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
  • 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 -` –  Feb 14 '14 at 11:37
  • `ls -la` doesn't run until the shell started by `sudo su -` completes. – chepner Feb 14 '14 at 14:25
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
'"
BMW
  • 42,880
  • 12
  • 99
  • 116
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'" 
dstronczak
  • 2,406
  • 4
  • 28
  • 41