Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Thread Contributor: CovertBotNews - Easy SSH Automation
#1
Easy SSH Automation

<div data-history-node-id="1339961" class="layout layout--onecol">
<div class="layout__region layout__region--content">

<div class="field field--name-field-node-image field--type-image field--label-hidden field--item"> <img src="https://www.linuxjournal.com/sites/default/files/nodeimage/story/bigstock-Education-concept-Head-With-F-61652528.jpg" width="800" height="600" alt="""" typeof="foaf:Image" class="img-responsive" /></div>

<div class="field field--name-node-author field--type-ds field--label-hidden field--item">by <a title="View user profile." href="https://www.linuxjournal.com/users/adam-mcpartlan" lang="" about="https://www.linuxjournal.com/users/adam-mcpartlan" typeof="schemaTongueerson" property="schema:name" datatype="" xml:lang="">Adam McPartlan</a></div>

<div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>
A script a day will allow you some freedom to play and build other useful
and more complicated scripts. Every day, I attempt to make my life
easier—by
this I mean, trying to stop doing the repetitive tasks. If a process is repeatable; it
can be scripted and automated. The idea to automate everything is not new, but
try automating a command on a remote host.
</p>

<p>
SSH is very flexible, and it comes with many options. My absolute favorite is
its ability to let you run a command on a remote server by passing the
<code>-t</code>
flag. An example:

</p><pre>
<code>
ssh -t adam@webserver1.test.com 'cat /etc/hosts'
</code>
</pre>


<p>
This will <code>ssh</code> to webserver1.test.com, then run <code>cat
/etc/hosts</code> in your shell
and return the output.
</p>

<p>
For efficiency, you could create an ssh-key pair.
It's a simple process of creating a passwordless public and a private
keypair. To set this up, use <code>ssh-keygen</code>, and accept the defaults ensuring you
leave the password blank:

</p><pre>
<code>
ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/adam/.ssh/id_rsa): y
Enter passphrase (empty for no passphrase): LEAVE BLANK
Enter same passphrase again:
Your identification has been saved in /home/nynet/.ssh/id_rsa.
Your public key has been saved in /home/nynet/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:jUxrQRObADE8ardXMT9UaoAcOcQPBEKGU622646P8ho
↪adam@webserver1.test.com
The key's randomart image is:
+---[RSA 2048]----+
|B*++*Bo.=o |
|.+. |
|=*= |
+----[SHA256]-----+
</code>
</pre>


<p>
Once completed, copy the public key to the target server. To do this, use
<code>ssh-copy-id</code>:

</p><pre>
<code>
ssh-copy-id adam@webserver1.test.com
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed:
"/home/adam/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s),
↪to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if
↪you are prompted now it is to install the new keys
adam@webserver1.test.com's password: ********
Number of key(s) added: 1
</code>
</pre>


<p>
You will be asked for the password of the target server.
</p>

<p>
If you have set this up correctly, you won't be asked for your password
next time you <code>ssh</code> to your target.
</p>

<p>
Execute the original example. It should be quicker now that you don't need to
enter your password.
</p>

<p>
If you have a handful of servers and want to report
the running kernel versions, you can run <code>uname -r</code> from the command line, but
to do this on multiple devices, you'll need a script.
</p>

<p>
Start with a file
with a list of your servers, called server.txt, and then run your script to
iterate over each server and return the required information:

</p></div>

<div class="field field--name-node-link field--type-ds field--label-hidden field--item"> <a href="https://www.linuxjournal.com/content/easy-ssh-automation" hreflang="en">Go to Full Article</a>
</div>

</div>
</div>




https://www.linuxjournal.com/content/eas...automation
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)