简体   繁体   English

如何自动化一些linux命令

[英]how to automate some linux commands

I've done a lot of windows command shell scripting but I'm a novice in linux. 我已经做了很多windows命令shell脚本,但我是linux的新手。 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? 有没有什么方法可以编写脚本/自动化以下命令,以便我所要做的就是(在Windows术语中)“运行批处理文件”来完成所有这些操作? Here are my steps, in order: 以下是我的步骤:

  • launch putty, choose hostname & port, click Open (would love to script/automate this 1st part too) 启动putty,选择主机名和端口,单击Open(希望脚本/自动化这个第一部分)
  • linux shell/terminal opens linux shell /终端打开
  • I enter my login and pwd 我输入我的登录名和密码
  • I enter this command: sudo su - psoftXXX 我输入这个命令:sudo su - psoftXXX
  • I enter my pwd again and hit enter 我再次进入我的密码并点击进入
  • I am presented with a little cmd-shell menu and a prompt. 我看到一个小cmd-shell菜单和提示。 I have to type "1" and hit enter 我必须输入“1”然后按回车键
  • cd / cd /
  • cd logs cd日志
  • cd hr cd hr
  • cd DV cd DV
  • cd appserv cd appserv
  • rm JEN* rm JEN *
  • ls LS

Any way to automate all or part of this? 有什么方法可以自动化全部或部分内容? THANKS. 谢谢。

Yes. 是。 Once you know it, you'll find that scripting in Linux is a delight compared to Windows. 一旦你知道它,你就会发现Linux中的脚本与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. 想象一下(可怕的)命令提示符和(不那么可怕)PowerShell之间的区别,然后将这个差异乘以10,这就是Linux中脚本的好坏程度。

The term is "shell scripting", because the command processor (equivalent of Windows command prompt) is called the 'shell'. 术语是“shell脚本”,因为命令处理器(相当于Windows命令提示符)称为“shell”。 Just to make life a bit more fruity, there are various shells available for Linux. 只是为了让生活变得更有果味,Linux有各种各样的shell。 The most common are variants of sh such as bash and ash . 最常见的是sh变体,如bashash Some crazy people use csh or tcsh or others, though, which have annoyingly different syntax. 然而,一些疯狂的人使用cshtcsh或其他人,这些语法有着令人烦恼的不同。

Creating a script 创建脚本

To create a 'shell script', ie a batch file, just create a text file with this line at the top: 要创建“shell脚本”,即批处理文件,只需在顶部创建一个带有此行的文本文件:

#!/bin/bash

(or whatever the name of your shell is). (或者你的shell的名字是什么)。 Typically, the filename may end in .sh or there may be no special file ending at all. 通常,文件名可能以.sh结尾,或者根本没有特殊文件结尾。

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. 在文件的其余行中,只列出您要运行的命令,就好像您在普通的Linux终端上键入它们一样。

Then assign 'execute' permissions to that file. 然后为该文件分配“执行”权限。 From the command line, you would use chmod u+x filename.sh . 从命令行,您将使用chmod u+x filename.sh That means add eXexecute permissions for the current User. 这意味着为当前用户添加eXexecute权限。

The real fun comes when you start to learn all the different things you can do in the shell. 当你开始学习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 . 还有许多非常棒的Linux / UNIX程序可以很容易地将其他程序连接在一起 - 例如grepuniqxargs

Your specific example 你的具体例子

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: 您的示例的shell脚本部分归结为:

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. 我得到了您想要从Windows自动登录以运行这些命令的印象。

I believe that in PuTTY, if it's connecting via SSH, you can give it a command to run. 我相信在PuTTY中,如果它通过SSH连接,你可以给它一个命令来运行。 So here's what I'd do. 所以这就是我要做的。

  1. Generate ssh keys on your Windows machine using PuTTY. 使用PuTTY在Windows机器上生成ssh密钥。 (I can't remember how to do this - I think there's PuTTYGen or similar). (我不记得怎么做 - 我认为有PuTTYGen或类似的)。
  2. Copy the public key that results into a file called authorized_keys within the .ssh directory of the psoftXXX user. 复制导致到一个名为文件中的公钥 authorized_keys的范围内.ssh的目录psoftXXX用户。 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. 如果文件已经存在,那么一定要将新密钥附加到文件的末尾而不是覆盖它。
  3. Now try to connect again using PuTTY and ssh. 现在尝试使用PuTTY和ssh再次连接。 It should automatically log in as the psoftXXX user. 它应该自动以psoftXXX用户身份登录。
  4. Finally, in the PuTTY settings you can probably specify the command-line given above. 最后,在PuTTY设置中,您可以指定上面给出的命令行。 You might well need to specify it like this: 您可能需要像这样指定它:

    /bin/bash -c "rm /logs/hr/DV/appserv/JEN*; ls /logs/hr/DV/appserv" / 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. 请注意,我没有自动化的一个阶段,即在菜单上按1。 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 . 那是因为我怀疑这个菜单是通过给你一个特殊的默认登录shell来实现的,而不是/bin/bash而是/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. 我希望如果你在PuTTY中指定一个替代命令,那么该设置将被完全忽略,而你将运行你的脚本。

You might need to play around a bit. 你可能需要玩一下。 Good luck! 祝好运!

yes you can write shell scripts in linux. 是的,你可以在linux中编写shell脚本。 each shell script begins with 每个shell脚本都以

#!/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... 至于自动化第一部分可能有点棘手,我认为你可以删除整个sudo su psoftXXX如果你刚刚以psoftxxx登录...

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 希望有所帮助,这是一个很好的资源,可以帮助您开始编写shell脚本http://linuxcommand.org/writing_shell_scripts.php

At one point I had a similar problem at work. 有一次我在工作中遇到了类似的问题。 I was tired of having to log into the shell. 我厌倦了必须登录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. 为了登录,我们必须首先ssh到网络中的在线系统,然后从该机器ssh到主系统。 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. 在Windows上我发现自动腻子的选择不是很好。 Cygwin to the rescue. Cygwin来救援。 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: 以下是我建议的用于终端/ ssh的应用程序:

Cygwin - https://www.cygwin.com/ or MobaXterm - http://mobaxterm.mobatek.net/ Cygwin - https://www.cygwin.com/或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. 我已经将这种方法与vanilla Cygwin(如果你想要一个标签式终端界面可以与Console2一起使用)和MobaXterm一起使用,这真的让Putty看起来完全过时了。 If you are going to use Cygwin install the expect package using apt-cyg or the Cygwin setup. 如果您打算使用Cygwin,请使用apt-cyg或Cygwin设置安装expect包。 If you are going to try using MobaXterm then you will need to download the tcl/expect plugin from the MobaXterm plugins page. 如果您打算尝试使用MobaXterm,则需要从MobaXterm插件页面下载tcl / expect插件。

You can use the expect library to automate anything. 您可以使用expect库自动执行任何操作。 If you are not comfortable with the TCL language, there is also a Python based version. 如果您对TCL语言不满意,还有一个基于Python的版本。

Here is a guide - http://www.cotse.com/dlf/man/expect/bulletproof1.htm - which should help you get started learning expect. 这是一个指南 - http://www.cotse.com/dlf/man/expect/bulletproof1.htm - 这应该可以帮助您开始学习期望。 Using expect I was able to script my entire workspace. 使用expect我能够编写整个工作区的脚本。 Connecting to the remote machine, and logging in has been reduced to a single alias in my Cygwin .bashrc file. 连接到远程计算机,登录已减少到我的Cygwin .bashrc文件中的单个别名。

Here is the expect home page - http://expect.sourceforge.net/ 这是期待的主页 - http://expect.sourceforge.net/

Additional details, instructions, and tips on this subject will be coming in future edits. 有关此主题的其他详细信息,说明和提示将在未来的编辑中提供。

Let's split this in a few tasks: 让我们把它分成几个任务:

Connect through SSH to the . 通过SSH连接到。

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. 按照步骤,应生成两个文件(id_rsa,id_rsa.pub),id_rsa.pub是公钥。

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 转到要连接的服务器,以psoftXXX身份登录,在$ HOME / .ssh / authorized_keys中添加存储在id_rsa.pub中的公钥

Now you should be able to connect to the server as the psoftXXX user without the need of typing the password: 现在,您应该能够以psoftXXX用户身份连接到服务器,而无需输入密码:

$ ssh psoftXXX@<server>

Now the other task. 现在是另一项任务。 Removing those files through SSH. 通过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 ". 这将以psoftXXX连接到服务器(服务器不会询问密码)并执行“rm -f”。

If I had to run this periodically I would use cron. 如果我不得不定期运行,我会使用cron。 You should check it out. 你应该检查一下。

Btw... try this commands: 顺便说一下......试试这个命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM