Visit Our UNIX and Linux User Community


Sudo connect to a remote server and execute scripts in remote server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sudo connect to a remote server and execute scripts in remote server
# 1  
Old 07-30-2016
Sudo connect to a remote server and execute scripts in remote server

Hello Every one!!

I am trying to write a shell script which will connect to a remote server and execute scripts which are at a certain path in the remote server.

Before this I am using a sudo command to change the user.

The place where I am stuck is, I am able to connect to the remote server but neither am able to change the directory path nor I am able to execute scripts which I need to.

Also the log files which I have given in the script are not being created nor the log is being captured in the files even if I create them manually.

Below is the prototype of the script which I have written:


Code:
 #!/bin/bash
#***************************************
 
ssh user@remote_server
if [ $? -eq 0 ];
then
echo "connection established successfully" >> path/to/log/file #(this log file is not being created nor is anything being inserted even if I create it manually)#
exit 0
else
echo "connection unsuccessful" >> path/to/log/file
fi
sh /path/to/remote/scripts/script_name
if [ $? -eq 0 ];
then
echo "script executed successfully" >> path/to/log/file #(this log file is not being created nor is anything being inserted even if I create it manually)#
else
echo "script not executed" >> path/to/log/file
fi
 exit

Please suggest as where am I going wrong and what needs to be corrected.

Can't thank you all enough!!

Regards.




Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 07-30-2016 at 06:30 AM.. Reason: Added code tags.
# 2  
Old 07-30-2016
The way you issue the ssh command opens an interactive session on the remote host, waiting for your input on stdin. The rest of the script is executed only after you logged out from the host, and the log files should be created locally (unless their path doesn't exist).

ssh will execute the command (list) on its command line, or stdin if redirected to e.g. a "here document".
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-30-2016
Hi,

You can try Like

Code:
HNAME=$(ssh -o StrictHostKeyChecking=no username@Server_address "/bin/uname")
if [ -n "${HNAME}" ];then
   echo "connection established successfully" > path/to/log/file
   ssh -o StrictHostKeyChecking=no username@Server_address << EOF >> path/to/log/file
    sh /path/to/remote/scripts/script_name > /dev/null 2>&1;
   if [ $? -eq 0 ];then
      echo "script executed successfully"
   else
       echo "script not executed"
   fi
EOF
else
     echo "connection unsuccessful" > path/to/log/file
fi

Regard's
Venkat K
# 4  
Old 07-30-2016
Hi Venkat,

Thank you very much for the reply, just got few doubts in the prototype you have attached.

1) In the first line, you have mentioned there is "/bin/uname" what is this exactly for and in place of uname do I need to give the username with which I am connecting to remote server?

2) Also there is "> /dev/null 2>&1;" in the script, is it mandatory to have this line in the script as I don't have permission to that path, including it may fail the script and can I specify any other path instead of it?

Please don't mind if the above questions sound silly, as I am a novice to unix, I find them hard to under stand.

Thanks for your help Smilie

Regards.
# 5  
Old 07-30-2016
Hi masubram,

1. uname command will give the OS name.it is to check connectivity, variable HNAME is not empty means its connected to remote server.

2.its not mandatory, you can specify any path.


Regard's
Venkat K
# 6  
Old 07-30-2016
Redirecting to /dev/null is a common method to suppress output. Different from many other entries in /dev, null is read/writeable by "other"s, i.e. anyone on the system.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 07-30-2016
Hi Venkat,

I have made the required changes to the script and tried running it but am getting an error as below

Pseudo-terminal will not be allocated because stdin is not a terminal.

Can you please specify what this is about, I have googled it but it's not clear as in some portals they suggested to add -T and all, but its not working.

Thanks for your help

Regards

Previous Thread | Next Thread
Test Your Knowledge in Computers #285
Difficulty: Easy
AIX was the first operating system to have a proc file system.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script connect to remote server, not find files and exit only from remote server, but not from scrip

I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code TARFILE=${NAME}.tar TARGZFILE=${NAME}.tar.gz ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;... (3 Replies)
Discussion started by: digioleg54
3 Replies

2. AIX

Command to connect to remote AIX server

Hi All, Im looking for a command that can remotely connect to another AIX machine and execute scripts present in that server(Possible scenarios are a complete a restart of 3 servers in a cluster through a single script execution present in one of the servers). Im relatively new to AIX and... (5 Replies)
Discussion started by: Vishakh
5 Replies

3. Shell Programming and Scripting

Problem connect to a different server then do sudo login and finally run some scripts and get result

I have to write a shell script in my current linux server and I have to connect to a different server then do sudo login and finally run some scripts residing in a particular directory and get results back. I am starting to write my shell script as below but after I do ssh login it prompts for... (2 Replies)
Discussion started by: Devesh5683
2 Replies

4. Red Hat

unable to connect remote server using ssh

hi all i am unable to connect remote server using ssh but i am able to ping the server ssh service is running. (5 Replies)
Discussion started by: nikhil kasar
5 Replies

5. UNIX for Dummies Questions & Answers

How do I connect to a remote X11 server ?

Hi, how do I connect to a remote x server? i tried this: startx -display 192.168.0.1:0 # when I do this, it just connects to my own x server i.e I see my own desktop. and export DISPLAY=192.168.0.1:0.0 # when I do this nothing happens. (it doesn't even try to connect, no activity in... (7 Replies)
Discussion started by: uxfuser
7 Replies

6. Shell Programming and Scripting

Execute scripts on remote server

Hi All, I need to first of all establish a connection to remote unix server non-interactively with the help of a shell script and then connect to oracle database from that server all with this script of mine. Please suggest the best method which could be used to connect to server for executing... (1 Reply)
Discussion started by: m_kapur83
1 Replies

7. Shell Programming and Scripting

Script to connect to a remote server and execute scripts

Hello All I need a script or set of commands which can establish a remote connection with another server and execute some scripts over there. Basically it has to establish the connection with the remote server as an user ,say 'testuser' and then execute the script 'testscript'. and return the... (5 Replies)
Discussion started by: sgbhat
5 Replies

8. Shell Programming and Scripting

Connect to a Remote Sybase Server Through Script

Hi all, I am trying to connect to Remote Sybase database Server through shell script. I am operating on WindowsXP, connect to Unix(version SunOS: 5.8) The thing is i dont know how to connect to Sybase Server through my script file? Are there any manual pages which can guide me through the... (3 Replies)
Discussion started by: Aparna_k82
3 Replies

9. Windows & DOS: Issues & Discussions

Connect to a Remote Sybase Server Through Script.

Hi all, I am trying to connect to Remote Sybase database Server through shell script. I am operating on WindowsXP, connect to Unix(version SunOS: 5.8) The thing is i dont know how to connect to Sybase Server through my script file? Are there any manual pages which can guide me through the... (2 Replies)
Discussion started by: Aparna_k82
2 Replies

10. UNIX for Dummies Questions & Answers

Remote Connect - SCO Open Server

Hello.. Please helppppppp...!!! I am using SCO Open Server version 5.0 at work. I am trying to find a third party software to use on my computer at home which has windows 98 on it to do remote connect to my work. Is there any such software ? I know there is PC ANYWHERE but i don't think it... (2 Replies)
Discussion started by: jackpotp
2 Replies

Featured Tech Videos