首页 > 编程知识 正文

ps批量操作,ps批处理命令

时间:2023-05-04 12:20:28 阅读:231321 作者:2831

Pssh pssh是python写的可以并发在多台机器上批量执行命令的工具,它的用法可以媲美ansible的一些简单用法,执行起来速度比ansible快它支持文件并行复制,远程命令执行,杀掉远程主机上的进程等等。杀手锏是文件并行复制,,当进行再远程主机批量上传下载的时候,最好使用它。 在使用pssh之前,必须要保证管理主机和本地主机进行过密钥的认证,或者是在进行批量时,没有做过密钥认证,但是必须保证被管理的多台主机的密码相同。 环境准备

Redhat 7.4 三台
Centos 1.4一台
10.155.155.10
10.155.155.20
10.155.155.30
10.155.155.40

一、编译安装 [root@localhost ~]# tar zxf pssh-2.3.1.tar.gz [root@localhost ~]# cd pssh-2.3.1[root@localhost pssh-2.3.1]# python setup.py installpssh:在远程多台主机上并行运行命令pscp :把文件并行复制到多台远程主机上prsync:使用rsync协议本地文件同步到远程多台主机上。pnuke:在远程多台主机上并行killall某一进程pslurp:把文件从远程多台主机上复制到本地主机上

二、pssh 参数
2.1
[root@localhost pssh-2.3.1]# pssh --help
Usage: pssh [OPTIONS] command […]

Options: --version show program's version number and exit --help show this help message and exit -h HOST_FILE, --hosts=HOST_FILE hosts file (each line "[user@]host[:port]") -H HOST_STRING, --host=HOST_STRING additional host entries ("[user@]host[:port]") -l USER, --user=USER username (OPTIONAL) -p PAR, --par=PAR max number of parallel threads (OPTIONAL) -o OUTDIR, --outdir=OUTDIR output directory for stdout files (OPTIONAL) -e ERRDIR, --errdir=ERRDIR output directory for stderr files (OPTIONAL) -t TIMEOUT, --timeout=TIMEOUT timeout (secs) (0 = no timeout) per host (OPTIONAL) -O OPTION, --option=OPTION SSH option (OPTIONAL) -v, --verbose turn on warning and diagnostic messages (OPTIONAL) -A, --askpass Ask for a password (OPTIONAL) -x ARGS, --extra-args=ARGS Extra command-line arguments, with processing for spaces, quotes, and backslashes -X ARG, --extra-arg=ARG Extra command-line argument -i, --inline inline aggregated output and error for each server --inline-stdout inline standard output for each server -I, --send-input read from standard input and send as input to ssh -P, --print print output as we get itExample: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime--version:查看版本--help:查看帮助,即此信息-h:主机文件列表,内容格式”[user@]host[:port]”-H:主机字符串,内容格式”[user@]host[:port]”-:登录使用的用户名-p:并发的线程数【可选】-o:输出的文件目录【可选】-e:错误输入文件【可选】-t:TIMEOUT 超时时间设置,0无限制【可选】-O:SSH的选项-v:详细模式-A:手动输入密码模式-x:额外的命令行参数使用空白符号,引号,反斜线处理-X:额外的命令行参数,单个参数模式,同-x-i:每个服务器内部处理信息输出-P:打印出服务器返回信息

2.2

具体常用介绍 -h HOST_FILE 后边跟远程主机列表(ip)-H HOST_STRING 后边跟远程主机名或者ip地址-l USER 指定远程主机的用户名-p PAR 指定pssh最大的并行线程数。-o 将输出的内容重定向到一个指定的文件中-O 指定ssh参数的具体配置-e 将执行错误重定向到一个指定的文件中-t 设定命令执行超时时间-x 传递ssh命令的一些参数-i 在远程主机上执行命令完成后显示标准输出和标准错误-P 在执行远程命令时,输出执行结果

三、示例
1)获取每台服务器的uptime

查看主机列表 [root@localhost ~]# cat ip.txt root@10.155.155.20root@10.155.155.30root@10.155.155.40 执行命令 [root@localhost ~]# pssh -h 10.155.155.20 -i uptimeCould not open hosts file: No such file or directory[root@localhost ~]# pssh -h ip.txt -i uptime[1] 13:45:35 [SUCCESS] root@10.155.155.20 09:45:35 up 2:37, 1 user, load average: 0.00, 0.01, 0.05[2] 13:45:35 [SUCCESS] root@10.155.155.30 01:45:35 up 3:43, 0 users, load average: 0.00, 0.01, 0.05[3] 13:45:35 [SUCCESS] root@10.155.155.40 01:45:35 up 3:37, 0 users, load average: 0.00, 0.01, 0.05 2)执行单个ip [root@localhost ~]# pssh -h 10.155.155.20 -i uptimeCould not open hosts file: No such file or directory[root@localhost ~]# pssh -H 10.155.155.20 -i uptime[1] 13:46:52 [SUCCESS] 10.155.155.20 09:46:53 up 2:38, 1 user, load average: 0.00, 0.01, 0.05 2)保存每台服务器运行的结果 [root@localhost ~]# pssh -h ip.txt -i -o /tmp/pssh/ uptime[1] 13:52:43 [SUCCESS] root@10.155.155.20 09:52:43 up 2:44, 1 user, load average: 0.01, 0.02, 0.05[2] 13:52:43 [SUCCESS] root@10.155.155.30 01:52:43 up 3:51, 0 users, load average: 0.00, 0.01, 0.05[3] 13:52:43 [SUCCESS] root@10.155.155.40 01:52:43 up 3:44, 0 users, load average: 0.00, 0.01, 0.05[root@localhost ~]# ls /tmp/pssh/root@10.155.155.20 root@10.155.155.30 root@10.155.155.40 3)pscp拷贝文件到远程主机 [root@localhost ~]# pscp -h ip.txt /root/expect.sh /tmp/[1] 13:57:41 [SUCCESS] root@10.155.155.20[2] 13:57:41 [SUCCESS] root@10.155.155.40[3] 13:57:41 [SUCCESS] root@10.155.155.30[root@localhost ~]# pssh -h ip.txt -i ls /tmp/[1] 13:58:18 [SUCCESS] root@10.155.155.20expect.shsystemd-private-97ff18ee9e6341f6ad475f452d152c6f-chronyd.service-LuFptJsystemd-private-97ff18ee9e6341f6ad475f452d152c6f-vgauthd.service-Fdu6zysystemd-private-97ff18ee9e6341f6ad475f452d152c6f-vmtoolsd.service-NA1I7n[2] 13:58:18 [SUCCESS] root@10.155.155.40expect.shsystemd-private-c1676ded76c24586a7539c379be7b45f-chronyd.service-oWTa5ksystemd-private-c1676ded76c24586a7539c379be7b45f-vgauthd.service-XhqOJDsystemd-private-c1676ded76c24586a7539c379be7b45f-vmtoolsd.service-P0RN9Y[3] 13:58:18 [SUCCESS] root@10.155.155.30expect.shsystemd-private-d8e1d0054ea44fd39fccfad8fa818a7b-chronyd.service-5IH50Nsystemd-private-d8e1d0054ea44fd39fccfad8fa818a7b-vgauthd.service-yxejqdsystemd-private-d8e1d0054ea44fd39fccfad8fa818a7b-vmtoolsd.service-kAuAIv[root@localhost ~]# pssh -h ip.txt -i ls /tmp/|grep expect.shexpect.shexpect.shexpect.sh 4)pnuke杀掉某进程 [root@localhost ~]# pssh -h ip.txt -i "yum install httpd -y"[root@localhost ~]# pssh -h ip.txt -i "systemctl start httpd"[1] 14:01:02 [SUCCESS] root@10.155.155.20[2] 14:01:02 [SUCCESS] root@10.155.155.30[3] 14:01:02 [SUCCESS] root@10.155.155.40[root@localhost ~]# pnuke -h ip.txt httpd[1] 14:01:27 [SUCCESS] root@10.155.155.20[2] 14:01:27 [SUCCESS] root@10.155.155.30[3] 14:01:27 [SUCCESS] root@10.155.155.40 5)远程主机拷贝文件到本地主机 [root@localhost ~]# pslurp -h ip.txt -L /tmp/ /tmp/expect.sh expect.exp[1] 14:13:27 [SUCCESS] root@10.155.155.40[2] 14:13:27 [SUCCESS] root@10.155.155.20[3] 14:13:27 [SUCCESS] root@10.155.155.30 将远程主机tmp下expect.sh复制到本地tmp下 并重新命名为expect.exp [root@localhost ~]# ls /tmp/[root@localhost ~]# tree /tmp//tmp/├── 10.155.155.20 │ └── expect.exp├── 10.155.155.30│ └── expect.exp├── 10.155.155.40│ └── expect.exp 把远程主机/root/.ssh/目录拷贝到本地opt下 [root@localhost ~]# pslurp -h ip.txt -r -L /opt/ /root/.ssh/ miyao[1] 14:18:52 [SUCCESS] root@10.155.155.20[2] 14:18:52 [SUCCESS] root@10.155.155.30[3] 14:18:52 [SUCCESS] root@10.155.155.40[root@localhost ~]# tree /opt//opt/├── 10.155.155.20│ └── miyao│ ├── authorized_keys│ ├── id_rsa│ └── id_rsa.pub├── 10.155.155.30│ └── miyao│ ├── authorized_keys│ ├── id_rsa│ └── id_rsa.pub└── 10.155.155.40 └── miyao ├── authorized_keys ├── id_rsa └── id_rsa.pub6 directories, 9 files

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。