How to automate some Linux commands

I have done a lot of Windows command shell scripts, but I am new to Linux. Is there a way I can script / automate the following commands so that all I have to do is (in Windows terminology) "run batch file" to do the whole thing? Here are my steps to:

  • run putty, select hostname and port, click open (would like to script / automate this 1st part too)
  • Opens linux shell / terminal
  • I enter my username and pwd
  • I enter this command: sudo su - psoftXXX
  • I go into my pwd again and hit enter
  • I am presented with a small cmd-shell menu and hint. I have to enter "1" and press Enter
  • cd /
  • cd logs
  • cd hr
  • cd DV
  • cd appserv
  • rm JEN *
  • Ls

Any way to automate all or part of this? THANKS.

+3


source to share


4 answers


Yes. Once you know this, you will find that scripting in Linux is a delight when compared to Windows. Imagine the difference between the command line (horrid) and the (less terrible) PowerShell, and then multiply that difference by ten, and how much more script-friendly it is on Linux.

The term "shell scripting" is because the command processor (equivalent to the Windows command line) is called a "shell". Just to make life a little more fruity, there are different shells available for Linux. The most common are options sh

such as bash

and ash

. Some crazy people use csh

either tcsh

or others, although they have annoyingly different syntax.

Script creation

To create a "shell script" i.e. batch file, just create a text file with this line at the top:

#!/bin/bash

      

(or whatever your shell name is). Typically, the filename may end with, .sh

or there may be no special file at all.

On the rest of the lines of the file, simply list the commands you would like to run as if you were entering them on a regular Linux terminal.

Then assign execute rights to this file. From the command line, you must use chmod u+x filename.sh

. This means eXexecute permissions are added for the current user.

The real pleasure comes when you start learning everything you can do in the shell. For example, there are clear tricks:

my-first-command && my-second-command   # only runs my-second-command if my-first-command succeeded

      

or that:



my-background-command &   # runs my-background-command in the background, so that you can get on with other things

      

There are also many amazing Linux / UNIX programs that facilitate connection to other programs - for example, grep

, uniq

, xargs

.

Your specific example

After explaining how big all this is, I don't think you actually need to do anything.

The shell script part of your example boils down to this:

rm /logs/hr/DV/appserv/JEN*; ls /logs/hr/DV/appserv

      

I am under the impression that you want to be automatically logged in from Windows to run these commands.

I believe that in PuTTY, if it connects via SSH, you can give it a command to start. So this is what I will do.

  • Generate ssh keys on your windows machine using PuTTY. (I don't remember how to do this - I think PuTTYGen or similar).
  • Copy the public key that is output to a file named authorized_keys

    in the .ssh

    user's directory psoftXXX

    . You can literally copy and paste it; which is probably easier than doing something out of the ordinary. Be aware that this directory and / or file may not exist, in which case you will need to create them; if the file already exists, be sure to add the new key to the end of the file rather than overwrite it.
  • Now try connecting again using PuTTY and ssh. It should be automatically logged in as a user psoftXXX

    .
  • Finally, in your PuTTY settings, you can possibly specify the above command line. You may need to specify it like this:

    / bin / bash -c "rm / logs / hr / DV / appserv / JEN *; ls / logs / hr / DV / appserv"

Please note that I did not automate one stage by pressing 1 in the menu. This is because I suspect that this menu is implemented by giving you a custom default login shell, which is not /bin/bash

, but instead /something/somewhere/which/shows/a/menu

. I hope that if you specify an alternative command in PuTTY, this parameter will be completely ignored and you will run your script instead.

You may need to play a little. Good luck!

+5


source


yes, you can write shell scripts in Linux. each shell script starts with

#!/bin/bash
<commands>
. 
.
.

      

if you want to run the command above, it will look something like this:



#!/bin/bash
cd /logs/hr/DV/appserv
rm JEN*
ls

      

how automated the first part is, which might be a little tricky and I think you could cut everything sudo su psoftXXX

if you only logged in as psoftxxx in the first place ...

hope this helps a little and here's a good resource to get you started writing shell scripts http://linuxcommand.org/writing_shell_scripts.php

+1


source


At some point, I had a similar problem at work. I'm tired of entering the shell. The way our main system is protected means it is disabled. To log in, we must first ssh connect to an online system on the network, then ssh from that machine to the main system. This is what the process looked like.

1.) Launch putty using the profile set up for the first machine. I had already made the effort to set the login command to ssh into the next server. 2.) Enter my user password 3.) use su to log into root 4.) enter the root password 5.) use gradm on my user 6.) source my .bashrc

Needless to say, it all seemed pointless to me. While on windows I found that the options for automating the putty weren't very good. Cygwin to the rescue. It takes a bit of work to set it up, but once it's finished it will be worth the time you save.

Here are my recommended applications for using in terminal / ssh:

Cygwin - https://www.cygwin.com/ or MobaXterm - http://mobaxterm.mobatek.net/

I've used this method with vanilla Cygwin (which can be used with Console2 if you want to use a tabbed interface) and MobaXterm, which really makes Putty look completely outdated. If you are going to use Cygwin, install the standby package using apt-cyg or Cygwin installation. If you try to use MobaXterm you need to download the tcl / expect plugin from the MobaXterm plugins.

You can use the expectation library to automate everything. If you don't like the TCL language, there is also a Python-based version.

Here's a guide - http://www.cotse.com/dlf/man/expect/bulletproof1.htm - that should get you started. Using wait, I was able to script my entire workspace. Remote computer connection and login have been boiled down to a single alias in my Cygwin.bashrc file.

Here is the expected home page - http://expect.sourceforge.net/

Additional information, instructions and advice on this matter will be published in future editions.

+1


source


Let's break this down into several tasks:

Connect via SSH to.

First of all, avoid using passwords inside scripts. Use public / private keys instead. Create a pair with:

$ ssh-keygen

      

Follow the instructions, you need to create two files (id_rsa, id_rsa.pub), id_rsa.pub is the public key.

Go to the server you want to connect to, login as psoftXXX and in $ HOME / .ssh / authorized_keys add the public key stored in id_rsa.pub

You should now be able to connect to the server as user psoftXXX without having to enter a password:

$ ssh psoftXXX@<server>

      

Now for another task. Removing these files via SSH.

$ ssh psoftXXX@<server> rm -f /logs/hr/DV/appserv/JEN*

      

This will connect to the server as psoftXXX (the server will not ask for a password) and issue the "rm -f" command.

If I had to run this periodically, I would use cron. You should check it out.

Btw ... try the following commands:

$ man <command>
$ man -k <keyword>
$ man man

      

0


source







All Articles