运维自动化轻量级工具pssh

网友投稿 1368 2022-10-03

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

运维自动化轻量级工具pssh

一、前言

每个机器都重新配置ip地址,可以远程访问,数十台服务器如何管理呢??每台服务器的主机名都相同,需要修改;每台服务器要更新软件。以后肯定也有很多其他需要批量操作的问题,比如批量安装监控,总不能数十台一个个手动操作吧!!本文,就研究下Linux批量操作的相关方法和工具,重点研究下pssh。

二、批量操作思路

首先定义两个概念:管理机和客户机,本文中的管理机是指管理其他服务器的服务器,客户机是指被管理的服务器。管理机ip地址为:20.20.20.122,客户机ip为20.20.20.115-117,用户名都是root。

环境信息如下 :

ip地址 操作系统环境 角色信息20.20.20.122 centos7.2-1511 管理机20.20.20.115 centos7.2-1511 客户机20.20.20.116 centos7.2-1511 客户机20.20.20.117 centos7.2-1511 客户机

三、pssh简介

pssh是一个python编写可以在多台服务器上批量执行命令的工具,同时支持拷贝文件,与同类工具中很出色的。它的用法可以ansible的一些简单用法,执行起来速度比ansible快它支持文件并行复制,远程命令执行,杀掉远程主机上的进程等等。杀手锏是文件并行复制,,当进行再远程主机批量上传下载的时候,最好使用它。

四、pssh的使用前提条件

在使用pssh之前,必须要保证管理主机和本地主机进行密钥的认证,或者是在进行批量时,没有做过密钥认证,但是必须保证被管理的多台主机的密码要相同。

五、pssh安装

(1)、在管理服务器上安装pssh软件

链接:https://pan.baidu.com/s/1r_XFXFDfMToKNjp2xtaZKg 提取码:e8ns

[root@psshmanager ~]# lsanaconda-ks.cfg pssh-2.3.1.tar.gz[root@psshmanager ~]# tar -zxvf pssh-2.3.1.tar.gzpssh-2.3.1/pssh-2.3.1/setup.pypssh-2.3.1/man/pssh-2.3.1/man/man1/pssh-2.3.1/man/man1/pslurp.1pssh-2.3.1/man/man1/pssh.1pssh-2.3.1/man/man1/pscp.1pssh-2.3.1/man/man1/prsync.1pssh-2.3.1/man/man1/pnuke.1pssh-2.3.1/INSTALLpssh-2.3.1/ChangeLogpssh-2.3.1/bin/pssh-2.3.1/bin/pscppssh-2.3.1/bin/prsyncpssh-2.3.1/bin/pnukepssh-2.3.1/bin/pssh-askpasspssh-2.3.1/bin/psshpssh-2.3.1/bin/pslurppssh-2.3.1/COPYINGpssh-2.3.1/AUTHORSpssh-2.3.1/psshlib/pssh-2.3.1/psshlib/psshutil.pypssh-2.3.1/psshlib/askpass_client.pypssh-2.3.1/psshlib/manager.pypssh-2.3.1/psshlib/version.pypssh-2.3.1/psshlib/__init__.pypssh-2.3.1/psshlib/color.pypssh-2.3.1/psshlib/task.pypssh-2.3.1/psshlib/cli.pypssh-2.3.1/psshlib/askpass_server.pypssh-2.3.1/PKG-INFOpssh-2.3.1/test/pssh-2.3.1/test/test.py[root@localhost ~]#[root@localhost pssh-2.3.1]# python setup.py installrunning installrunning buildrunning build_pycreating buildcreating build/libcreating build/lib/psshlibcopying psshlib/psshutil.py -> build/lib/psshlibcopying psshlib/askpass_client.py -> build/lib/psshlibcopying psshlib/manager.py -> build/lib/psshlibcopying psshlib/version.py -> build/lib/psshlibcopying psshlib/__init__.py -> build/lib/psshlibcopying psshlib/color.py -> build/lib/psshlibcopying psshlib/task.py -> build/lib/psshlibcopying psshlib/cli.py -> build/lib/psshlibcopying psshlib/askpass_server.py -> build/lib/psshlibrunning build_scriptscreating build/scripts-2.7copying and adjusting bin/pssh -> build/scripts-2.7copying and adjusting bin/pnuke -> build/scripts-2.7copying and adjusting bin/prsync -> build/scripts-2.7copying and adjusting bin/pslurp -> build/scripts-2.7copying and adjusting bin/pscp -> build/scripts-2.7copying and adjusting bin/pssh-askpass -> build/scripts-2.7changing mode of build/scripts-2.7/pssh from 644 to 755changing mode of build/scripts-2.7/pnuke from 644 to 755changing mode of build/scripts-2.7/prsync from 644 to 755changing mode of build/scripts-2.7/pslurp from 644 to 755changing mode of build/scripts-2.7/pscp from 644 to 755changing mode of build/scripts-2.7/pssh-askpass from 644 to 755running install_libcreating usr/lib/python2.7/site-packages/psshlibcopying build/lib/psshlib/psshutil.py -> usr/lib/python2.7/site-packages/psshlibcopying build/lib/psshlib/askpass_client.py -> usr/lib/python2.7/site-packages/psshlibcopying build/lib/psshlib/manager.py -> usr/lib/python2.7/site-packages/psshlibcopying build/lib/psshlib/version.py -> usr/lib/python2.7/site-packages/psshlibcopying build/lib/psshlib/__init__.py -> usr/lib/python2.7/site-packages/psshlibcopying build/lib/psshlib/color.py -> usr/lib/python2.7/site-packages/psshlibcopying build/lib/psshlib/task.py -> usr/lib/python2.7/site-packages/psshlibcopying build/lib/psshlib/cli.py -> usr/lib/python2.7/site-packages/psshlibcopying build/lib/psshlib/askpass_server.py -> usr/lib/python2.7/site-packages/psshlibbyte-compiling usr/lib/python2.7/site-packages/psshlib/psshutil.py to psshutil.pycbyte-compiling usr/lib/python2.7/site-packages/psshlib/askpass_client.py to askpass_client.pycbyte-compiling usr/lib/python2.7/site-packages/psshlib/manager.py to manager.pycbyte-compiling usr/lib/python2.7/site-packages/psshlib/version.py to version.pycbyte-compiling usr/lib/python2.7/site-packages/psshlib/__init__.py to __init__.pycbyte-compiling usr/lib/python2.7/site-packages/psshlib/color.py to color.pycbyte-compiling usr/lib/python2.7/site-packages/psshlib/task.py to task.pycbyte-compiling usr/lib/python2.7/site-packages/psshlib/cli.py to cli.pycbyte-compiling usr/lib/python2.7/site-packages/psshlib/askpass_server.py to askpass_server.pycrunning install_scriptscopying build/scripts-2.7/pssh -> usr/bincopying build/scripts-2.7/pnuke -> usr/bincopying build/scripts-2.7/prsync -> usr/bincopying build/scripts-2.7/pslurp -> usr/bincopying build/scripts-2.7/pscp -> usr/bincopying build/scripts-2.7/pssh-askpass -> usr/binchanging mode of usr/bin/pssh to 755changing mode of usr/bin/pnuke to 755changing mode of usr/bin/prsync to 755changing mode of usr/bin/pslurp to 755changing mode of usr/bin/pscp to 755changing mode of usr/bin/pssh-askpass to 755running install_datacreating usr/mancreating usr/man/man1copying man/man1/pssh.1 -> usr/man/man1copying man/man1/pscp.1 -> usr/man/man1copying man/man1/prsync.1 -> usr/man/man1copying man/man1/pslurp.1 -> usr/man/man1copying man/man1/pnuke.1 -> usr/man/man1running install_egg_infoWriting usr/lib/python2.7/site-packages/pssh-2.3.1-py2.7.egg-info[root@psshmanager pssh-2.3.1]#

查看pssh版本

[root@psshmanager ~]# pssh --version2.3.1[root@psshmanager ~]#

(2)、pssh命令格式

命令格式:pssh [选项] 命令 [...]选项:

--version:查看版本--help:查看帮助,即此信息-h:主机文件列表,内容格式"[user@]host[:port]"-H:主机字符串,内容格式"[user@]host[:port]"-l:登录使用的用户名-p:并发的线程数【可选】-o:输出的文件目录【可选】-e:错误输入文件【可选】-t:TIMEOUT 超时时间设置,0无限制【可选】-O:SSH的选项-v:详细模式-A:手动输入密码模式-x:额外的命令行参数使用空白符号,引号,反斜线处理-X:额外的命令行参数,单个参数模式,同-x-i:每个服务器内部处理信息输出-P:打印出服务器返回信息

六、实战篇

1、添加密钥认证访问

(1)、在管理机上创建密钥

[root@psshmanager ~]#[root@psshmanager ~]# ssh-keygenGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa):Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in root/.ssh/id_rsa.Your public key has been saved in root/.ssh/id_rsa.pub.The key fingerprint is:51:bd:42:93:0d:bb:ba:ba:39:d0:4a:e0:91:0b:21:cd root@node01The key's randomart image is:+--[ RSA 2048]----+| o o= ||o E .+.o ||.. . .... . ||. + .... || o + . S .. || o o . . || . o . || . .. . || ++. |+-----------------+[root@node01 ~]#

(2)、把公钥拷贝到所有客户机中

[root@psshmanager ~]# ssh-copy-id -i .ssh/id_rsa.pub 20.20.20.115The authenticity of host '20.20.20.112 (20.20.20.112)' can't be established.ECDSA key fingerprint is f5:87:e6:de:aa:68:21:79:5f:66:40:0c:4f:12:2d:cc.Are you sure you want to continue connecting (yes/no)? yes/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 keysroot@20.20.20.115's password:Number of key(s) added: 1Now try logging into the machine, with: "ssh '20.20.20.115'"and check to make sure that only the key(s) you wanted were added.[root@psshmanager ~]#

20.20.20.115 是客户机ip地址

(3)、测试免密登录在管理机中,使用ssh 20.20.20.115测试登录,我们发现已经不需要输入密码进行登录

[root@psshmanager ~]# ssh 20.20.20.115Last login: Wed May 15 10:16:11 2019 from 192.168.128.235[root@localhost ~]#

(4)、测试命令

[root@psshmanager ~]# ssh 20.20.20.115 "ip add"1: lo: mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 7a:2c:0f:0f:67:9a brd ff:ff:ff:ff:ff:ff inet 20.20.20.115/24 brd 20.20.20.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::782c:fff:fe0f:679a/64 scope link valid_lft forever preferred_lft forever[root@psshmanager ~]#

返回了115客户机的ip add执行结果,测试成功。

2、获取每台客户端uptime信息

(1)、在管理机上创建ip.txt,内容为

[root@psshmanager ~]# cat ip.txt 20.20.20.11520.20.20.11620.20.20.117[root@psshmanager ~]#

(2)、执行uptime命令

[root@psshmanager ~]# pssh -h ip.txt -i uptime[1] 11:22:34 [SUCCESS] 20.20.20.115 11:22:34 up 1:08, 1 user, load average: 0.00, 0.01, 0.05[2] 11:22:34 [SUCCESS] 20.20.20.117 11:22:34 up 2:02, 1 user, load average: 0.00, 0.01, 0.05[3] 11:22:34 [SUCCESS] 20.20.20.116 11:22:34 up 2:02, 1 user, load average: 0.00, 0.01, 0.05[root@psshmanager ~]#

(3)、保存执行结果(可选)

[root@psshmanager ~]# pssh -h ip.txt -i -o tmp/pssh/ uptime[1] 11:23:32 [SUCCESS] 20.20.20.115 11:23:32 up 1:09, 1 user, load average: 0.00, 0.01, 0.05[2] 11:23:32 [SUCCESS] 20.20.20.116 11:23:32 up 2:03, 1 user, load average: 0.00, 0.01, 0.05[3] 11:23:32 [SUCCESS] 20.20.20.117 11:23:31 up 2:03, 1 user, load average: 0.00, 0.01, 0.05[root@psshmanager ~]# cat tmp/pssh/20.20.20.115 11:23:32 up 1:09, 1 user, load average: 0.00, 0.01, 0.05[root@psshmanager ~]#

3、批量修改hostname主机名

我们把客户机的主机名修改为node115 node116 node117(1)、新建hosts.txt文件,内容如下:

[root@psshmanager ~]# cat hosts.txt 20.20.20.115 node11520.20.20.116 node11620.20.20.117 node117[root@psshmanager ~]#

(2)、新建hostname.sh脚本,内容如下:

#!/bin/baship=`ifconfig eth0|awk 'NR==2 {print $2}'`hostname=`cat root/hosts.txt |grep $ip | awk '{print $2}'`echo $ipecho $hostnamehostnamectl set-hostname --static $hostnamehostname $hostname

(3)、发送到host.txt和hostname.sh到客户机/root/目录下

[root@psshmanager ~]# pscp -h ip.txt root/hosts.txt root[1] 11:45:51 [SUCCESS] 20.20.20.116[2] 11:45:51 [SUCCESS] 20.20.20.115[3] 11:45:51 [SUCCESS] 20.20.20.117[root@psshmanager ~]# pscp -h ip.txt root/hostname.sh root[1] 11:46:16 [SUCCESS] 20.20.20.117[2] 11:46:16 [SUCCESS] 20.20.20.115[3] 11:46:16 [SUCCESS] 20.20.20.116[root@psshmanager ~]#

ip.txt 是记录需要cp的主机ip地址、hosts.txt、hostname.sh是修改主机名的脚本文件、/root是目标路径

(4)、批量授予hostname.sh可执行权限

[root@psshmanager ~]# pssh -h ip.txt -i 'chmod +x root/hostname.sh'[1] 12:33:17 [SUCCESS] 20.20.20.115[2] 12:33:17 [SUCCESS] 20.20.20.116[3] 12:33:17 [SUCCESS] 20.20.20.117[root@psshmanager ~]#

(5)、批量执行hostname.sh脚本

[root@psshmanager ~]# pssh -h ip.txt -i '/root/hostname.sh'[1] 15:25:15 [SUCCESS] 20.20.20.11720.20.20.117node117[2] 15:25:15 [SUCCESS] 20.20.20.11520.20.20.115node115[3] 15:25:15 [SUCCESS] 20.20.20.11620.20.20.116node116[root@psshmanager ~]#执行成功

(6)、验证主机名是否修改成功

[root@psshmanager ~]# pssh -h ip.txt -i hostname[1] 12:56:29 [SUCCESS] 20.20.20.115node115[2] 12:56:29 [SUCCESS] 20.20.20.117node117[3] 12:56:29 [SUCCESS] 20.20.20.116node116[root@psshmanager ~]#

4、批量修改密码使用expect命令

(1)、管理机上新建passwd.sh脚本,内如如下:

[root@psshmanager ~]# cat passwd.sh#!/bin/bashuser=rootpassword=123456expect << EOFspawn passwd $userexpect "Enter new UNIX password:"send "${password}\r"expect "Retype new UNIX password:"send "${password}\r"expect eof;EOF[root@psshmanager ~]#

(2)、在客户机上安装expect(可以用whereis expect查看是否安装)

[root@psshmanager ~]# pssh -h ip.txt -i 'whereis expect'[1] 14:05:44 [SUCCESS] 20.20.20.116expect: usr/bin/expect usr/share/man/man1/expect.1.gz[2] 14:05:44 [SUCCESS] 20.20.20.115expect: usr/bin/expect usr/share/man/man1/expect.1.gz[3] 14:05:45 [SUCCESS] 20.20.20.117expect: usr/bin/expect usr/share/man/man1/expect.1.gz[root@psshmanager ~]#

(3)、拷贝passwd.sh脚本到客户机

[root@psshmanager ~]# pscp -h ip.txt passwd.sh root/[1] 14:07:00 [SUCCESS] 20.20.20.116[2] 14:07:00 [SUCCESS] 20.20.20.117[3] 14:07:00 [SUCCESS] 20.20.20.115

(4)、添加执行权限

[root@psshmanager ~]# pssh -h ip.txt -i 'chmod +x root/passwd.sh'[1] 14:08:42 [SUCCESS] 20.20.20.115[2] 14:08:42 [SUCCESS] 20.20.20.116[3] 14:08:42 [SUCCESS] 20.20.20.117[root@psshmanager ~]#

(5)、执行修改密码脚本

[root@psshmanager ~]# pssh -h ip.txt -i 'sh passwd.sh'[1] 14:10:15 [SUCCESS] 20.20.20.115spawn passwd rootChanging password for user root.New password:BAD PASSWORD: The password is shorter than 8 charactersRetype new password:passwd: all authentication tokens updated successfully.[2] 14:10:15 [SUCCESS] 20.20.20.116spawn passwd rootChanging password for user root.New password:BAD PASSWORD: The password is shorter than 8 charactersRetype new password:passwd: all authentication tokens updated successfully.[3] 14:10:15 [SUCCESS] 20.20.20.117spawn passwd rootChanging password for user root.New password:BAD PASSWORD: The password is shorter than 8 charactersRetype new password:passwd: all authentication tokens updated successfully.[root@psshmanager ~]#

5、通过批量执行命令修改密码

命令如下:[root@psshmanager ~]# pssh -h ip.txt -i 'echo "123456" | passwd root --stdin > dev/null 2>&1'[1] 13:54:18 [SUCCESS] 20.20.20.115[2] 13:54:18 [SUCCESS] 20.20.20.117[3] 13:54:18 [SUCCESS] 20.20.20.116

七、pssh 软件其他命令介绍

pssh 在多个主机上并行运行命令pscp 把文件并行复制到多个主机上prsync 通过rsync协议把文件高效并行复制到多个主机上pslurp 把文件并行地从多个远程主机复制到中心主机上pnuke 并行地在多个远程主机上杀死进程

1、pscp命令

pscp:把文件或者目录并行复制到多个主机上面pscp 命令选项:

Usage: pscp [OPTIONS] -h hosts.txt local remote-r 递归复制目录-h 主机文件列表,格式"host[:port] [user]"-l 用户名-p --par 并发线程数-o --outdir 输出的文件目录-e --errdir 错误输出的文件目录-t --timeout 设置命令执行超时时间 -1表示无限制-O --options 设置ssh的一些选项-v --verbose 详细模式

例如:从管理机拷贝压缩包到客户机/root/

[root@psshmanager ~]# pscp -h ip.txt '/root/V510R18C06B04-beta21.tar.gz' /root[1] 17:51:15 [SUCCESS] 20.20.20.116[2] 17:51:16 [SUCCESS] 20.20.20.115[3] 17:51:18 [SUCCESS] 20.20.20.117[root@psshmanager ~]#

2、pslurp命令(从远程主机复制到本地)

pslurp 命令选项:

Usage: pslurp [OPTIONS] -h hosts.txt -o outdir remote local -r --recursive recusively copy directories (OPTIONAL) -L --localdir output directory for remote file copies -h --hosts hosts file (each line "host[:port] ") -l --user username (OPTIONAL) -p --par max number of parallel threads (OPTIONAL) -o --outdir output directory for stdout files (OPTIONAL) -e --errdir output directory for stderr files (OPTIONAL) -t --timeout timeout (secs) (-1 = no timeout) per host (OPTIONAL) -v --verbose turn on warning and diagnostic messages (OPTIONAL) -O --options SSH options (OPTIONAL)-L:指定从远程主机拷贝文件放置的目录,拷贝/root/expect.sh文件,并将其重命名为test1.如果拷贝目录需要使用-r参数

3、prsync命令

prsync 命令选项:

Usage: prsync [OPTIONS] -h hosts.txt local remote -r --recursive recusively copy directories (OPTIONAL) -a --archive use rsync -a (archive mode) (OPTIONAL) -z --compress use rsync compression (OPTIONAL) -h --hosts hosts file (each line "host[:port] ") -l --user username (OPTIONAL) -p --par max number of parallel threads (OPTIONAL) -o --outdir output directory for stdout files (OPTIONAL) -e --errdir output directory for stderr files (OPTIONAL) -t --timeout timeout (secs) (-1 = no timeout) per host (OPTIONAL) -v --verbose turn on warning and diagnostic messages (OPTIONAL) -O --options SSH options (OPTIONAL)prsync的主要作用是通过rsync协议将文件或目录从本地主机同步到远程多个主机上。-r:递归复制。-a:保持文件的属性不变-z:压缩文件,一般不推荐。

4、pnuke命令

pnuke 命令选项:

Usage: pnuke [OPTIONS] -h hosts.txt pattern -h --hosts hosts file (each line "host[:port] [user]") -l --user username (OPTIONAL) -p --par max number of parallel threads (OPTIONAL) -o --outdir output directory for stdout files (OPTIONAL) -e --errdir output directory for stderr files (OPTIONAL) -t --timeout timeout (secs) (-1 = no timeout) per host (OPTIONAL) -v --verbose turn on warning and diagnostic messages (OPTIONAL) -O --options SSH options (OPTIONAL)

pnuke的主要作用是在远程多主机上并行杀掉某一进程,相当于killall命令。可以直接使用pssh 来传递killall命令,这里就不做介绍了。

上一篇:Linux sudo 权限提升漏洞,升级呀运维!
下一篇:运维知识:CentOS7使用源码编译安装Nginx
相关文章

 发表评论

暂时没有评论,来抢沙发吧~