LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-28-2016, 01:06 PM   #1
masubram
LQ Newbie
 
Registered: Jul 2016
Posts: 2

Rep: Reputation: Disabled
Question sudo connect to a remote server and execute scripts in remote server


I am pretty much new to unix and am starting out learning it and finding it very much interesting. Hope this forum will be much helpful for me in understanding unix in a much better way.

I am facing a scenario where I have a requirement as below.

1)I am logging into a unix server using my credentials after which I have to do a sudo to another user.

2) Once done I have to ssh to a remote server.

3) Once into remote server, I have a path where I have some scripts and those scripts need to be executed in the remote server itself.

I have written a script which is helping me till I connect to remote server, but the problem I am facing is once connected to remote server it is by default taking a certain path and is not changing to the path where I my scripts.

I have learnt through research that cd command doesn't work inside a script so have a generic script where I am using a predefined environment variables and tired using them, but that isn't working even.

my script looks some what like this.

user@localhost: sudo super_user
local_server|/export/home/user > ssh user@remote_server
remote_server|/export/home/user> ...this the default path which it is taking and I want it to be changed to /abc/def/ghi/my_script.sh

can anyone please guide me on how to proceed with this. If my question is unclear please let me know, will make it bit more elaborated.

Can't thank you all enough!!
 
Old 07-28-2016, 02:01 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
cd command works in a script...
You can't change directory to a file though (/abc/def/ghi/my_script.sh is a file, no?)

You want to change directory to /abc/def/ghi on remote server?
 
Old 07-28-2016, 03:00 PM   #3
masubram
LQ Newbie
 
Registered: Jul 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
Hi, thank you for the reply...

No cd command doesn't work inside a script as I have found on numerous websites and to answer your question... Yes I only want to navigate to the directory /abc/def/ghi on the remote server
 
Old 07-28-2016, 05:48 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
cd command does work inside a script. The "problem" is that when a script runs it starts its own process with its own environment so when the script terminates your back to your "starting" directory.

Without knowing anything about your scripts it is a bit difficult to provide specific help. To run remote programs via ssh you can use:

ssh user@remote_server "cd /path/to/scripts; ./script1; ./script2"
 
Old 07-30-2016, 06:41 AM   #5
24x7servermanagement
Member
 
Registered: Jul 2016
Location: India
Distribution: CentOS, Redhat, Ubuntu and Debian
Posts: 57

Rep: Reputation: Disabled
Yes, cd command should work fine within script. I'm not sure about your script but when you are in remote server via script then you should give the absolute path of directory where the script(s) rest. (An absolute or full path points to the same location in a file system regardless of the current working directory)
 
Old 07-31-2016, 09:33 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by masubram View Post
Hi, thank you for the reply...

No cd command doesn't work inside a script as I have found on numerous websites and to answer your question... Yes I only want to navigate to the directory /abc/def/ghi on the remote server
To add to the totally correct information that michaelk gave you (which is the SIMPLEST way to go), I'll add slightly to it. You will probably need the "-c" option to pass the command to the remote server to executed. And, you can also look into expect. Once that remote shell is spawned, the script on the local side will wait for it to complete, before continuing. Using expect, it will actually 'look' at the prompt, and type things in, as if a user was doing it.

Last edited by TB0ne; 07-31-2016 at 09:36 AM.
 
Old 07-31-2016, 09:35 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by 24x7servermanagement View Post
Yes, cd command should work fine within script. I'm not sure about your script but when you are in remote server via script then you should give the absolute path of directory where the script(s) rest. (An absolute or full path points to the same location in a file system regardless of the current working directory)
Sorry, totally wrong. As michaelk stated, the cd command will work LOCALLY, not remote, so giving a path is totally useless in this context. Please stop posting bad/rehashed/pointless advice to get your spam-signature noticed.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] I am trying to remote execute a python command vis ssh. I am getting a syntax error from the remote server. buckb Linux - Networking 7 12-29-2015 07:52 AM
Unable to connect MS SQL server 2008 Remote Server through SQUID proxy server samank75 Linux - Server 5 01-19-2012 09:19 PM
Execute script on remote linux server snorky Linux - Newbie 2 12-28-2010 04:37 PM
Execute script on local server as normal user to run commands on remote server ALInux Linux - Software 1 01-01-2010 06:30 AM
Execute commands or scripts on a remote server gurl4sh25 Linux - Software 3 06-27-2007 09:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration