sudo connect to a remote server and execute scripts in remote server
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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:
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)
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.