LinuxQuestions.org
Help answer threads with 0 replies.
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 11-22-2013, 07:51 AM   #1
ashutosh784
LQ Newbie
 
Registered: Nov 2013
Posts: 1

Rep: Reputation: Disabled
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..
 
Old 11-22-2013, 08:18 AM   #2
lpwevers
Member
 
Registered: Apr 2005
Location: The Netherlands
Distribution: SuSE, CentOS
Posts: 143

Rep: Reputation: 20
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>
 
1 members found this post helpful.
Old 11-22-2013, 08:23 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,983
Blog Entries: 12

Rep: Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044
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.
 
1 members found this post helpful.
Old 11-22-2013, 09:46 AM   #4
youngstorm
Member
 
Registered: May 2003
Location: USA
Distribution: Fedora 21, RHEL 5,6&7, Windoze 7
Posts: 235

Rep: Reputation: 31
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
 
Old 11-22-2013, 08:44 PM   #5
jefro
Moderator
 
Registered: Mar 2008
Posts: 17,195

Rep: Reputation: 2562Reputation: 2562Reputation: 2562Reputation: 2562Reputation: 2562Reputation: 2562Reputation: 2562Reputation: 2562Reputation: 2562Reputation: 2562Reputation: 2562
Autoexpect seem to be pretty easy too.
 
Old 11-22-2013, 09:43 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
...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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
SHELL SCRIPT Write at the right of the shell window solo9300 Linux - General 3 09-29-2009 05:56 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 07:24 AM
help to write shell script vikrambhimbar Linux - Software 2 04-03-2006 05:43 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 04:00 PM

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

All times are GMT -5. The time now is 07:55 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration