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, 2019 in Linux Administration by Damon Salvatore
• 5,980 points
17,337 views

2 answers 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.

Hope this answer help you.

To learn more about Linux, enroll in Linux administration course online today.

Thanks.

answered Jun 27, 2019 by ajs3033
• 7,300 points
0 votes

Sorry in advance for any formatting.
Check out the Expect scripting language.  Expect - Wikipedia
You can design it to log into another system and perform the sudo (or su) command.
To avoid putting passwords into command prompts (security violation), have it prompt the user for the remote system password(s).
----
You can 'expect' prompts like the rsa key and have it answer and continue.
----
Example of an embedded expect function inside a shell (/bin/sh) script.  I handles several scenarios including rsa prompt.

function nonrootuser {
expect << END
set timeout 30
spawn ssh $2@$1
expect {
  "No route" {
    exit 5;
  }  "yes/no" {
    send "yes\r"
    exp_continue;
  }  "password:" {
    send "$3\r"
  }
}
expect "home/$2"
send "$4\r"
expect "home/$2"
send "exit\r"
expect EOF
END
}

Call it with:
nonrootuser $remotehost "$USERID" "$USERPASSWORD" "$COMMAND"

-----
This function sends a single linux command to the remote host.  It does not check the output, but it will be on the screen.  You can also redirect it to a file if you choose.
Since it is contained within a script, you can prompt the user for the password so it is never hardcoded or put in a command line (where it can been seen using 'history')
You can also add a looping mechanism for multiple devices.

-----
If sudo is already set-up on your end device, you can just call it.
If it isn't, here is the added steps for becoming root (before the send "$4\r" statement), you will also need another send "exit\r" that is not shown here.
send "su -\r"
expect "word:"
send "$5\r"
expect {
  ":/root" {
    expect ":/root"
  } "denied" {
    exit 6
  } "timeout {
    exit 7
  }
}

Of course since it has an additional parameter (the root password), it would need to be called using such.

nonrootuser $remotehost "$USERID" "$USERPASSWORD" "$COMMAND" "$ROOTPASSWORD"

answered Feb 7, 2021 by anonymous

Related Questions In Linux Administration

0 votes
1 answer

How do I check if a directory exists in a Bash shell script?

To check if a directory exists in ...READ MORE

answered Jun 7, 2022 in Linux Administration by Korak
• 5,820 points
1,823 views
0 votes
1 answer

How to run GUI program in SSH server from remote desktop?

Hi@akhtar, First you have to configure SSH server ...READ MORE

answered Mar 31, 2020 in Linux Administration by MD
• 95,460 points
4,476 views
0 votes
1 answer

How to set key based authentication in remote machine?

Hi@akhtar, You can set key based authentication in ...READ MORE

answered Apr 1, 2020 in Linux Administration by MD
• 95,460 points
854 views
0 votes
1 answer

How to find the first field from a file in Bash Shell?

Hi@akhtar, You can extract text from a file. ...READ MORE

answered Oct 20, 2020 in Linux Administration by MD
• 95,460 points
1,092 views
0 votes
1 answer
0 votes
0 answers

How do I copy a folder from remote to local using scp?

How do I copy a folder from ...READ MORE

Apr 13, 2022 in Linux Administration by Soham
• 9,710 points
598 views
+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
6,266 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,350 points
5,587 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, 2019 in Linux Administration by ajs3033
• 7,300 points
1,241 views
0 votes
1 answer

How to execute a linux command repeatedly after a set time?

To watch the step every say 30 ...READ MORE

answered Jul 26, 2019 in Linux Administration by ajs3033
• 7,300 points
1,080 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP