Reputation: 1100
Hi
I am working with shell scripting language (I am new to it). I have 6 different linux machines. I have access to machine A. Through machine A and using ssh -l <username> <ip_address>
, I can connect to B. B has access to C, D, and E only (A cant access them directly. It has to do so via B only). Similarly, F is accessible only via C.
I have to run certain commands on all the linux machines (for eg, pwd
and ls -lrt
) from my system "A". How can I automate that thing? I mean is it possible in this scenario to execute script on machine A, which itself connects to all other machines as shown in figure and give me output of commands executed (via script only) on all machines as a text file in machine A?
I wrote a shell script. On execution of this script on machine A, it connects to machine B. And then, nothing more happens i.e. rest of the script is not executed. When I press Ctrl + D
on machine B, script again starts executing. That is my problem because it is executing commands on machine A only, nothing on machine B. I want the script to keep running even thought the machine is now changed. What i feel the problem is, the machine B has no access to script. So, I need a workaround for how to do that.
If it is not possible in shell, which scripting language will be good for what I want to do?
I googled but the search results are not what I am looking for :(
Upvotes: 0
Views: 1242
Reputation: 8571
I think splitting up code on different machines will help in your case but this will increase your manageability task,
one solution could be,
write scripts on respective nodes for tasks which you want to perform on them and just call those scripts using ssh from their parent node.
write a script/run a command on node A like
ssh user@nodeB "execute script on B(nodeBscript.sh)"
whatever you want to perform on B, write it in a script nodeBscript.sh
now in nodeBscript.sh, call those scripts which are present on node C,D,E and performing the required tasks.
like,
ssh user@nodeC "nodeCscript.sh"
ssh user@nodeD "nodeDscript.sh"
ssh user@nodeE "nodeEscript.sh"
and similar chain goes till end. This allows you to achieve your goal. if you want you can redirect your output in files which are shared by NFS so that you can access those files from your machine A also.
another solution(which I guess you have already tried)
write all in a script like,
ssh user@nodeB "ssh user@nodeC 'your command'" > outputC
ssh user@nodeB "ssh user@nodeD 'your command'" > outputD
ssh user@nodeB "ssh user@nodeE 'your command'" > outputE
and this also should work.
Upvotes: 1
Reputation: 1283
This may meet your issue:
ssh <username>@<hostname> "<your_command>"
More than one command, use “;” to seperate.
Good Luck!
Upvotes: 1