LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to write a shell script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-write-a-shell-script-4175485504/)

ashutosh784 11-22-2013 06:51 AM

How to write a shell script
 
How to write a shell script to ssh to a remote machine and the switch user command.. and then execute commands ??

Please Help me out..

lpwevers 11-22-2013 07:18 AM

Hi,

Ok, if I understand correctly, what you're trying to do is write a script that runs on machine A. This script will execute some commands on machine B. And these commands must be run as root.

What I would do in a case like this is this:
  • Exchange ssh keys so that the user from machine A can log in password less on machine B as root
  • Then use the ssh command to execute remote commands:
    Code:

    ssh root@machineb <command>

rtmistler 11-22-2013 07:23 AM

Give this a look, I just completed it a few days ago about bash scripting.

Bash Scripting for Dummies and Geniuses

The most important points to me are that whatever you write in a bash script is the same stuff you can type in a terminal. Therefore don't try to write it all in one line; instead follow the one-by-one steps you would normally do in a command line; within your script.

The next thing is to actually attempt the script you're asking about, debug it as best as you can, or even get it fully working. If you have trouble, post the script you do have, note what's not working or what your desired intention was that didn't occur. Or even if you're successful and wish to see if others have suggestions you may consider, post the script, say that it does work and that you're wondering if anyone has suggestions as to how to do it differently.

Other links to look at are general guides for bash programming:

Bash HOWTO - a very good starting guide and I recommend you read the first chapter or two and actually do the first few example scripts. Because if you don't have a fundamental understanding about how to write a bash script at all, you should at least understand why the syntax is the way it is. Anyone can look at their machine and see other scripts. Attempting to copy those and alter them to do what you want is one way to get things done, sometimes a difficult path, but sometimes effective. Problem is, that way is a die toss, and depends on what you started with as an example.

Bash Beginners Guide - another good starting guide.

Advanced Bash Scripting Guide - As it says "advanced" however this is close to a De-facto guide on bash programming, it will show you the exact syntax, or what many also call the ABNF, which is a name you can look up, it means the formalized definition of whatever language it is describing; in this case Bash script.

youngstorm 11-22-2013 08:46 AM

you can try expect too.

http://linux.die.net/man/1/expect
http://www.thegeekstuff.com/2010/10/expect-examples/
http://stackoverflow.com/questions/4...to-ssh-command

jefro 11-22-2013 07:44 PM

Autoexpect seem to be pretty easy too.

unSpawn 11-22-2013 08:43 PM

...and please do not default to advertising "ssh root@" unless you're also willing to teach the OP about the ways it breaks SSH and security best practices and how to mitigate risks.


All times are GMT -5. The time now is 10:39 PM.