How do I use a shell script to SSH in to a remote machine to execute commands?

0 votes

I want to know:

  1. How can I execute a shell script with a set of commands(some require sudo) on multiple remote linux machines that I've set up?
  2. How can I handle prompts fr RSA fingerprint authentication while SSHing ?

Jun 27 in Linux Administration by Damon Salvatore
• 5,510 points
66 views

1 answer to this question.

0 votes

Your first problem can be solved like this:

#!/bin/bash
USERNAME=someUser
HOSTS="host1 host2 host3"
SCRIPT="pwd; ls"
for HOSTNAME in ${HOSTS} ; do
    ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}"
done

You can add the StrictHostKeyChecking=no option to ssh:

For RSA authentication just add StrictHostKeyChecking=no option to ssh:

ssh -o StrictHostKeyChecking=no -l username hostname "pwd; ls"

This should disable the host key check and will straight away add the host key to the list of known hosts. In case you choose not to add the host added to the host file you can also add the -o UserKnownHostsFile=/dev/null .

Note: This should not be practiced in a security sensitive environment as these settings will disable certain security checks.

answered Jun 27 by ajs3033
• 7,280 points

Related Questions In Linux Administration

0 votes
1 answer
0 votes
1 answer

How to take input from user in bash script?

You can use if-else branch to check ...READ MORE

answered Jan 31 in Linux Administration by Omkar
• 67,660 points
44 views
0 votes
1 answer

How to create a symlink in Linux?

To create a symbolic link, you can ...READ MORE

answered Jan 31 in Linux Administration by Omkar
• 67,660 points
77 views
0 votes
1 answer

To run a shell script on a Unix console or Mac terminal

To run a non-executable sh script, use: sh myscript To run ...READ MORE

answered May 16 in Linux Administration by Upasana
• 8,550 points
58 views
0 votes
1 answer
+1 vote
2 answers

Execute Pig Script from Grunt Shell

From your current directory run  pig -x local Then ...READ MORE

answered Oct 25, 2018 in Big Data Hadoop by Kunal
960 views
0 votes
1 answer

How to stop messages from being displayed on spark console?

In your log4j.properties file you need to ...READ MORE

answered Apr 24, 2018 in Apache Spark by kurt_cobain
• 9,260 points
1,349 views
0 votes
1 answer

How to get Spark dataset metadata?

There are a bunch of functions that ...READ MORE

answered Apr 26, 2018 in Apache Spark by kurt_cobain
• 9,260 points
469 views
0 votes
1 answer

How to send mail using linux shell script?

If you've already configured the server, with ...READ MORE

answered Jun 11 in Linux Administration by ajs3033
• 7,280 points
46 views
0 votes
1 answer