shell在远程机器上执行本地脚本

avatar 2023年4月30日18:19:02 评论 132 次浏览

工作中遇到很多情况,必须一批新机器,需要在每个机器上执行一个命令,不过如果考虑ansible也是可以的,但是有些环境确实不太适合ansible,那么有没有什么方法是否可以不用ansble也可以完成,下面看一下,远程执行本地的脚本。如果有多台,我们只需要循环ip地址就可以了。为了避免操作时需要输入密码,我这里先设置免密码登录。

 [wolf@wulaoer.org 🔥🔥🔥🔥 sh]# ssh-keygen
 Generating 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:
 SHA256:vqnsRUf63m6coKRjOmXYYk55RiREXe2y104t+XmzF+Y root@Swarm
 The key's randomart image is:
 +---[RSA 3072]----+
 |   oo. ...       |
 |    . o   .      |
 |     o   ..      |
 |      . .o.      |
 |     =  So.. o   |
 |    * *oooo = .o |
 |   + * ooo.= +o..|
 |    o.+..+ .= oEo|
 |    .=+oo .oo  o+|
 +----[SHA256]-----+
 [wolf@wulaoer.org 🔥🔥🔥🔥 sh]# ssh-copy-id root@10.211.55.25
 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
 /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 keys
 root@10.211.55.25's password:
 
 Number of key(s) added: 1
 
 Now try logging into the machine, with:   "ssh 'root@10.211.55.25'"
 and check to make sure that only the key(s) you wanted were added.

免密码登录完成了,下面我在本地创建一个脚本,就创建文件的脚本吧,方便后期,针对服务的管理。

 [wolf@wulaoer.org 🔥🔥🔥🔥 sh]# vim tach.sh
 #!/bin/bash
 mkdir -p /apps/works/{service,logs,data}
 ls /apps/works/
 [wolf@wulaoer.org 🔥🔥🔥🔥 sh]# ssh root@10.211.55.25 < tach.sh
 Pseudo-terminal will not be allocated because stdin is not a terminal.
 data
 logs
 service
 [wolf@wulaoer.org 🔥🔥🔥🔥 sh]#  ssh root@10.211.55.25 "date"
Fri Apr 28 14:20:37 CST 2023

这样就完成脚本的创建,并查看文件,我们可以登录到25机器上看一下,一定是创建成功了,如果有多个机器都需要执行的话,我们可以把ip地址写到一个文件中,然后打开文件,循环ip地址就可以了。这里就不测试了,好了,没有了看些其他的吧。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: