Reputation: 7226
Is there some way I can script/automate the following commands so that all I have to do is (in windows terminology) "run a batch file" to do all this? Here are my steps, in order:
Any way to automate all or part of this?
Upvotes: 5
Views: 26912
Reputation: 404
At one point I had a similar problem at work. I was tired of having to log into the shell. The way our main system is secured means that it is offline. In order to log in we have to first ssh to an online system in the network, then ssh from that machine to to the main system. Here 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, all of this seemed rather pointless to me. Being on windows I found that the options for automating putty are not very good. Cygwin to the rescue. It takes a little work to set it up, but once it is up, it is completely worth the time you will save.
Here are my suggested applications to use for terminal/ssh:
Cygwin - https://www.cygwin.com/ or MobaXterm - http://mobaxterm.mobatek.net/
I have used this method with vanilla Cygwin (which can be used with Console2 if you want a tabbed terminal interface) and MobaXterm, which really makes Putty look completely obsolete. If you are going to use Cygwin install the expect package using apt-cyg or the Cygwin setup. If you are going to try using MobaXterm then you will need to download the tcl/expect plugin from the MobaXterm plugins page.
You can use the expect library to automate anything. If you are not comfortable with the TCL language, there is also a Python based version.
Here is a guide - http://www.cotse.com/dlf/man/expect/bulletproof1.htm - which should help you get started learning expect. Using expect I was able to script my entire workspace. Connecting to the remote machine, and logging in has been reduced to a single alias in my Cygwin .bashrc file.
Here is the expect home page - http://expect.sourceforge.net/
Additional details, instructions, and tips on this subject will be coming in future edits.
Upvotes: 1
Reputation: 406
Let's split this in a few tasks:
Connect through SSH to the .
First of all avoid using passwords inside scripts. Instead use public/private keys. Generate a pair with:
$ ssh-keygen
Follow the steps, two files should be generated (id_rsa, id_rsa.pub), id_rsa.pub is the public key.
Go to the server you want to connect, log in as the psoftXXX and in $HOME/.ssh/authorized_keys add the public key stored in id_rsa.pub
Now you should be able to connect to the server as the psoftXXX user without the need of typing the password:
$ ssh psoftXXX@<server>
Now the other task. Removing those files through SSH.
$ ssh psoftXXX@<server> rm -f /logs/hr/DV/appserv/JEN*
This will connect to the server as psoftXXX (the server won't ask the password) and will execute the "rm -f ".
If I had to run this periodically I would use cron. You should check it out.
Btw... try this commands:
$ man <command>
$ man -k <keyword>
$ man man
Upvotes: 0
Reputation: 4114
Yes. Once you know it, you'll find that scripting in Linux is a delight compared to Windows. Imagine the difference between (horrid) command prompt and (less horrid) PowerShell, then multiply that difference by ten, and that's how much nicer it is to script in Linux.
The term is "shell scripting", because the command processor (equivalent of Windows command prompt) is called the 'shell'. Just to make life a bit more fruity, there are various shells available for Linux. The most common are variants of sh
such as bash
and ash
. Some crazy people use csh
or tcsh
or others, though, which have annoyingly different syntax.
To create a 'shell script', i.e. a batch file, just create a text file with this line at the top:
#!/bin/bash
(or whatever the name of your shell is). Typically, the filename may end in .sh
or there may be no special file ending at all.
In the remaining lines of the file just list the commands you would like to run, as if you were typing them in at a normal Linux terminal.
Then assign 'execute' permissions to that file. From the command line, you would use chmod u+x filename.sh
. That means add eXexecute permissions for the current User.
The real fun comes when you start to learn all the different things you can do in the shell. For example there are neat tricks like this:
my-first-command && my-second-command # only runs my-second-command if my-first-command succeeded
or this:
my-background-command & # runs my-background-command in the background, so that you can get on with other things
There are also lots of terrific little Linux/UNIX programs which make it easy to connect other programs together - for example grep
, uniq
, xargs
.
Having explained how great all that is, though, I don't think that's actually what you need to do.
The shell script part of your example boils down to:
rm /logs/hr/DV/appserv/JEN*; ls /logs/hr/DV/appserv
I get the impression you want to log in from Windows automatically to run these commands.
I believe that in PuTTY, if it's connecting via SSH, you can give it a command to run. So here's what I'd do.
authorized_keys
within the .ssh
directory of the psoftXXX
user. You can literally copy and paste it; that's probably easier than doing anything fancy. Be aware that this directory and/or file may not exist, in which case you'll need to create them; if the file exists already then be sure to append the new key to the end of the file instead of overwriting it.psoftXXX
user.Finally, in the PuTTY settings you can probably specify the command-line given above. You might well need to specify it like this:
/bin/bash -c "rm /logs/hr/DV/appserv/JEN*; ls /logs/hr/DV/appserv"
Note that there's one stage I haven't automated, which is pressing 1 at the menu. That's because I suspect this menu is implemented by giving you a special default login shell which isn't /bin/bash
but is instead /something/somewhere/which/shows/a/menu
. I hope that if you specify an alternative command in PuTTY, that setting will be totally ignored and instead you'll get your script to run.
You might need to play around a bit. Good luck!
Upvotes: 7
Reputation: 3364
yes you can write shell scripts in linux. each shell script begins with
#!/bin/bash
<commands>
.
.
.
if you wanted to run the command above it would look something like this
#!/bin/bash
cd /logs/hr/DV/appserv
rm JEN*
ls
as far as automating the first part that might be a little trickier and I think you could cut out the whole sudo su psoftXXX
if you just logged in as psoftxxx in the first place...
hope that helps a little, and here's a good resource to get you started writing shell scripts http://linuxcommand.org/writing_shell_scripts.php
Upvotes: 1