Need script to run the command in remote server

hi,

I need script to perform below task.

  1. Read the IP address [already done]
  2. copy the script from origin server to destination. [already done]
  3. get root access on destination server [not working]
  4. run the script on destination server
  5. return to the origin server

Code:

#!/bin/bash
echo "Enter Server IP Address"
read a
scp -p /tmp/script.sh srom@$a:/tmp
ssh srom@$a /usr/local/bin/sudo su -

Error message:

Must be attached to terminal for 'am I' option
Must be attached to terminal for 'am I' option
stty: : Invalid argument

you can use ssh to directly run commands on remote server like this

ssh root@remote_host "/tmp/script.sh"

This will prompt you for password, unless you have copied your local user's public key to the authorized_keys file on the home of user .ssh's directory. This will allow for password less authentication.

1 Like

I dnt have root access. 1st i have to login as user account then i can get root access with this command - /usr/local/bin/sudo su -
i have to run the script in to nearly 200 servers.

try using here document in shell script to achieve your requirement

ssh srom@$a << EOF
password
/usr/local/bin/sudo su -
Scripts_you_to_run
exit
EOF
1 Like

it works.. thanks a lot....

sudo does not prompt you for password? you can even skip copying the script, and running su just to run another shell...

sh has -s option to read commands from stdin. example:

[mute@geek ~]$ cat local-script
echo Hello World >/home/mute/test.txt
[mute@geek ~]$ ssh mute@localhost sudo /bin/bash -s < local-script
mute@localhost's password:
[mute@geek ~]$ cat test.txt
Hello World