Log in to another server and run commands - Using a script [duplicate] - bash

This question already has answers here:
What is the cleanest way to ssh and run multiple commands in Bash?
(14 answers)
bash script execute commands after ssh
(1 answer)
How to use SSH to run a local shell script on a remote machine?
(23 answers)
When I run a bash script that ssh's into a remote server and run a command like wget it saves on the source not the destination server?
(3 answers)
Closed 3 years ago.
I want to run a script a.sh that will do the following steps -
Run few commands
Call another shell script b.sh in the same server - server01
b.sh will login to another server - server02 and run the commands that follow.
I am able to do till step 2 correctly. In step 3, I am able to loin to another server but it stops there. It does not run the steps that follow.
Have a look at the two scripts.
a.sh
cd ~/sample/home && python helloworld.py
#!/bin/bash
cd ~/sample/ && ./b.sh
b.sh
ssh username#server02.com
echo "In server02"
Both a.sh and b.sh are in the same server, that is server01.com. Here, I want a.sh to run in server01 and then b.sh to run in server01. Once b.sh runs, it should do ssh to server02 and print "In server02".
I am able to do till ssh to server02. After that it is not printing "In server02" in server02.
Is there a way to do it?

Use a heredoc.
ssh username#server02.com << EOF
echo "In server02"
EOF

Related

Exit from SSH using a sh script

I have a RHEL box (bash) and I have SSH'd to an ESXi (sh) from it.
Now on ESXi I have created a simple script
#!/bin/sh
echo hello
exit
This only exits the script. I want to exit the script + exit the ESXi shell and return to my original RHEL bash.
Thanks much.
If you are only SSHing in for the purpose of running this command, then instead you could just have the ssh run the command for you:
[RHEL]$ ssh user#ESXi '/tmp/myscript.sh'
...and if you needed to interact with the script, or watch it's output, add the -t switch:
[RHEL]$ ssh -t user#ESXi '/tmp/mysctipt.sh'
Remove the shebang ie do
echo hello && exit
save it as script and then source the script like
. script

Bash: Set up a new command on a new line without executing [duplicate]

This question already has an answer here:
How to prefill command line input
(1 answer)
Closed 6 years ago.
I'm trying to write a BASH script to output a partially completed command which I can then add parameters to, hit ENTER and then run. I want this to be implemented completely in BASH.
e.g.
~> ./test.sh
~> ls -al <CURSOR POSITION HERE>
The only variable I've found that's close is the PROMPT_COMMAND variable, which when set inside test.sh to 'ls -al', will then immediately execute it once the script has exited.
Is there a way to stop the immediate execution, so I can add, say, *.log?
How about
read -e -p"$PWD> " -i"ls -al " cmd; eval "$cmd"

Check if script was started in current shell [duplicate]

This question already has answers here:
How to detect if a script is being sourced
(22 answers)
Closed 8 years ago.
Is there a way to check within a shell script (ksh) whether or not the script was started in the current shell?
Example
Start script in current shell with . (dot/source) command
$ . ./myscript
$ I run in the current environment!
Start script in own process
$ ./myscript
$ I run in my own process!
This is a simple trick you can use.
#!/bin/ksh
if [ ${.sh.file} != ${0} ]; then
echo I run in the current environment
else
echo I run in my own process
fi
Every Shell Has its Own PID ..
so you can use echo "$$" in ur script ..it will helps us find from where the Script is RAN .
i.e Difference in pid means they are run from different shells .

Executing ssh command in a bash shell script within a loop [duplicate]

This question already has answers here:
in my bash loop over a list of some servers, if the ssh connects the bash script exits
(2 answers)
Closed 8 years ago.
I'm trying to execute an ssh command within a a bash shell script that should perform the following:
1) ssh to host
2) execute command
3) print value of command
4) repeat steps 1 -3
5) exit bash shell script
I have set up password less entry to the remote host, added host key to remote host
I want to test the various states of the httpd process running on the remote host
Within a text file, httpd_process.txt, I have:
/etc/init.d/httpd status (stop, start, restart)
I do the following in the script:
while read LINE
do
echo "Httpd Request: $LINE"
status=`$LINE`
echo "Status: $status"
sleep 5 # sleep so that next
done < /path_name/httpd_process.txt
exit 0
I assumed that each time through the loop another input string is read from the input text file and the request is made to the remote host.
However, what I experience is that after the first request the script terminates.
Am I correct to assume that as the first request is sent it creates a child process and once that process completes my script completes and the next turn through the loop is not executed?
ssh is consuming stdin. Pass it -n to prevent this.

Auto SSH and execute script

I have roughly 12 computers that each have the same script on them. This script merely pings all the other machines, and prints out whether the machine is "reachable" or "unreachable". However, it is inefficient to login to each machine manually using ssh to execute this script.
Suppose I'm logged into node 1. Is there any way to for me to login to node 2-12 automatically using SSH, execute the ping script, pipe the results to a file, logout and proceed to the next machine? Some kind of bash shell script?
I'm afraid I'm at a loss here since I haven't had experience with shell-scripting before.
Since the script is on the other machines, you can just have ssh run the command for you there:
ssh $hostname my_script >> results_file
When you specify a command like that, it's executed instead of the login shell.
I'll leave it up to you to figure out how to loop over hostnames!
One trick you'll need to use is setting up pre-authorized keys for each host. Then you can run a script on one host, running something like 'ssh hostname command > log.hostname'
This script might be what you are looking for: It allows you to execute one command (which can be your script) on multiple remote machines via ssh. It's a simple script with bash source available, so you should be able to customize it to your needs:
http://www.heinzi.at/projects/upgradebest.sh/
Yes you can
You need actually 2 small scripts as following:
remote_ssh.sh ( which takes as first argument the name of the machine and the rest of the arguments are your script that you want to execute with his own arguments)
Example : remote_ssh.sh node5 "echo hello world"
remote_ssh.sh as following:
#!/bin/bash
ALL_ARG=$#
FST_ARG=$1
REST_ARG=${ALL_ARG##$FST_ARG}
echo "Executing REMOTE COMMAND ON $FST_ARG"
/usr/bin/ssh $FST_ARG bash execute_ssh_command.sh $FST_ARG pwd $REST_ARG
execute_ssh_command.sh as following :
#!/bin/bash
ALL_ARG=$#
FST_ARG=$1
DIR_ARG=$2
REM_ARG="$1 $2"
REST_ARG=${ALL_ARG##$REM_ARG}
cd $DIR_ARG
$REST_ARG
of course you have to get this 2 scripts in your path of all your nodes ( maybe ~/bin/ )
Hope that it's helpful

Resources