Need script to run the command in remote server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need script to run the command in remote server
# 1  
Old 04-26-2012
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:


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:

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

Moderator's Comments:
Mod Comment Please use [code] tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-26-2012 at 09:29 AM..
# 2  
Old 04-26-2012
you can use ssh to directly run commands on remote server like this

Code:
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.
This User Gave Thanks to mohanm For This Post:
# 3  
Old 04-26-2012
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.
# 4  
Old 04-26-2012
try using here document in shell script to achieve your requirement

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

This User Gave Thanks to mohanm For This Post:
# 5  
Old 04-26-2012
it works.. thanks a lot....
# 6  
Old 04-26-2012
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:

Code:
[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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Except script to run a local shell script on remote server using root access

local script: cat > first.sh cd /tmp echo $PWD echo `whoami` cd /tmp/123 tar -cvf 789.tar 456 sleep 10 except script: cat > first #!/usr/bin/expect set ip 10.5.15.20 set user "xyz123" set password "123456" set script first.sh spawn sh -c "ssh $user@$ip bash < $script" (1 Reply)
Discussion started by: Aditya Avanth
1 Replies

2. UNIX for Beginners Questions & Answers

How to run command on remote server?

Hi I am trying to write a script which when I run from server A it execute few command on another server say B and show me the output. below is the script but it is not showing me the o/p of B machine but instead showing me A machine o/p every time. #!/bin/bash for i in `cat... (14 Replies)
Discussion started by: scriptor
14 Replies

3. Ubuntu

Run a script at remote server without ssh password

Hello, What I want to do is to run a file on remote server by running a script at localhost but script should not ask ssh password of my remote server when script is executed. Scenario1: To copy files from server2 to data server:$ scp -r root@server2_ip:/var/www/html/*.* /var/ When I enter... (6 Replies)
Discussion started by: baris35
6 Replies

4. Shell Programming and Scripting

To run a shell script in remote server from windows batch file

Hi all, i need to run a shell script on remote server. I have created file .bat file in windows server with following code, c:\Users\Desktop\putty.exe -ssh -pw password user@server ./script.sh i need to run the script.sh in my remote server Above command is not working, any... (4 Replies)
Discussion started by: rammm
4 Replies

5. Shell Programming and Scripting

Until string from remote command equals value run remote command

I solved my issue by using the following code #!/bin/bash function GET_STATUS { #values Active Passive Failed ssh -a localhost '/home/user/fakecommand.sh' } STATE="unknown" until ] do echo $STATE sleep 5 STATUS=`GET_STATUS` echo $STATUS | grep Active &&... (1 Reply)
Discussion started by: $scipt_Kid
1 Replies

6. Shell Programming and Scripting

Copy and run that script in Remote server

Hi All, I need script to perform below task. 1. I have a script in one server and need to copy this script to remote server 2. login in to remote server 3. run the script which i copied to this server. #!/bin/bash read a scp /tmp/script.sh user@hostname:/tmp ssh user@$a ./scirpt.sh ... (2 Replies)
Discussion started by: bapu1981
2 Replies

7. Shell Programming and Scripting

Need help on how to exit a script run on a server from a remote server

hi, I am using the below line to run a script from remote server(say server A) to another server(say server B). ssh username@servername ksh script name. The issue is the script logs into server B, executes the script on server B, transfers the file to server A but does not exit from... (4 Replies)
Discussion started by: yohasini
4 Replies

8. Programming

Expect script to run a Shell script on remote server

Hi All, I am using a expect script to run a shell script on remote server, the code is as follows. But the problem is that it executes only first command, and hangs it doesn't run the next commands. spawn ssh $uid@$host expect "password:" send "$password\r" expect "*\r" send... (2 Replies)
Discussion started by: yashwanthsn
2 Replies

9. Shell Programming and Scripting

Run command on remote sever from script

I have two redhat linux server. i have created one script which contain some command that run on Local server as well as remote server.I am using this command to connect to remote server ssh user1@192.x.x.x 'command' but when i am running the script in local server it connecting to the server... (1 Reply)
Discussion started by: ranvijaidba
1 Replies

10. Shell Programming and Scripting

run a application from a remote server via script?

I have a ksh script that does a bunch of things, then runs telnet server_b I then manually login, manually run one command (which launches an application with display back to my workstation), then logout at which point the main script takes back over, runs something else, then ends. Is... (4 Replies)
Discussion started by: yankee428
4 Replies
Login or Register to Ask a Question