shell远程操作另外一台机器上数据

shell远程操作另外一台机器上的数据,有两种方式: 1 、配置免密登陆,2、使用sshpass

当前存在两台虚拟机,ip地址分别为:192.168.3.32 192.168.3.33

一、免密登陆操作另外一台机器

1、生成秘钥

两台机器上都做如下操作,三次输入,直接摁回车

[[email protected] work]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory ‘/root/.ssh‘.
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:
60:25:67:28:bb:50:92:d4:d6:ce:68:40:60:8c:e5:d2 [email protected]
The key‘s randomart image is:
+--[ RSA 2048]----+
|+*+o ...+        |
|o++ = o=         |
|. E= *o          |
| .. +.o.         |
|   o .  S        |
|    .            |
|                 |
|                 |
|                 |
+-----------------+

2、认证

在3.32上执行如下命令,并输入3.33密码

[[email protected] work]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]192.168.3.33
The authenticity of host ‘192.168.3.33 (192.168.3.33)‘ can‘t be established.
ECDSA key fingerprint is cb:96:8a:bc:74:22:aa:f9:6e:1a:7e:7c:95:7d:2f:03.
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 keys
[email protected]192.168.3.33‘s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh ‘[email protected]‘"
and check to make sure that only the key(s) you wanted were added.

在3.33上执行如下命令,并输入3.32密码

[[email protected] work]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]192.168.3.32
The authenticity of host ‘192.168.3.32 (192.168.3.32)‘ can‘t be established.
ECDSA key fingerprint is 0e:38:cc:da:2e:27:33:16:5b:3d:ee:8f:a1:4f:c0:7d.
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 keys
[email protected]192.168.3.32‘s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh ‘[email protected]‘"
and check to make sure that only the key(s) you wanted were added.

3、验证免密登陆

在3.32上执行如下命令,提示登陆成功后,输入exit退出,以免操作错误

[[email protected] work]# ssh 192.168.3.33
Last login: Mon Sep 23 00:01:04 2019 from 192.168.3.32
[[email protected] ~]# exit
logout
Connection to 192.168.3.33 closed.

在3.33上执行如下命令,提示登陆成功后,输入exit退出,以免操作错误

[[email protected] work]# ssh 192.168.3.32
Last login: Sun Sep 22 23:59:56 2019 from 192.168.3.32
[[email protected] ~]# exit
logout
Connection to 192.168.3.32 closed.

若出现在次提示输入密码,则根据提示输入,退出远程登陆的服务器后,再次执行ssh尝试

4、shell脚本操作本机和远程服务器

假设当前需要分别获取3.32和3.33上两台机器上的mysql position,以便后续做复制操作时使用,由于交付同事在配置时经常出错,所以需要做成脚本,并且尽量只在一台机器上执行。假设两台服务器上mysql已经安装,且position不一样,(如果相同,可通过建表建库,插入数据使其不一致,这样才能看出来效果)

#!/bin/bash

pos_332="`ssh 192.168.3.33 "/usr/local/mysql/bin/mysql -uroot -p12345 -e ‘show master status‘" | grep mysql-bin | awk ‘{print $2}‘`"

pos_333="`/usr/local/mysql/bin/mysql -uroot -p12345 -e ‘show master status‘ | grep mysql-bin | awk ‘{print $2}‘`"

echo "3.32 mysql position: "$pos_332
echo "3.33 mysql position: "$pos_333

输出结果,前两行为提示信息,不用管,后两行为我们脚本打印结果。可以登陆到各服务器上mysql,分别执行show master status查看是输出是否一致,若不一致,则可能是由于其他的写操作造成position变化,可以通过FLUSH TABLE WITH READ LOCK锁住表,获取到position后在通过UNLOCK TABLE解锁表

[[email protected] work]# ./get_mysql_position.sh
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
3.32 mysql position: 414
3.33 mysql position: 573

二、sshpass操作另外一台服务器

1、删除秘钥,之后通过ssh相互分别登陆两次,第二次会直接提示输入对方密码,防止之前免密登陆还有效

rm -rf /root/.ssh/*

2、安装sshpass,如果双方都需要操作对方数据,则双方都安装sshpass,本例中通过,32来访问33,故只在32上安装sshpass

yum -y install sshpass

3、获取两台服务器上mysql position

#!/bin/bash

pos_332="`sshpass -p root ssh [email protected] "/usr/local/mysql/bin/mysql -uroot -p12345 -e ‘show master status‘" | grep mysql-bin | awk ‘{print $2}‘`"

pos_333="`/usr/local/mysql/bin/mysql -uroot -p12345 -e ‘show master status‘ | grep mysql-bin | awk ‘{print $2}‘`"

echo "3.32 mysql position: "$pos_332
echo "3.33 mysql position: "$pos_333

输出结果

[[email protected] work]# ./get_mysql_position.sh
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
3.32 mysql position: 414
3.33 mysql position: 573

与第一种方式相比,只是多了sshpass -p 密码的输入

原文地址:https://www.cnblogs.com/qq931399960/p/11567938.html

时间: 2024-08-29 05:14:06

shell远程操作另外一台机器上数据的相关文章

解决mysql跟php不在同一台机器上,编译安装php服务报错问题:configure: error: Cannot find MySQL header files under /application/mysql.

在编译安装php服务时报错: configure: error: Cannot find MySQL header files under /application/mysql. Note that the MySQL client library is not bundled anymore! 前边搭建lnmp环境时,是把mysql和php安装在了同一台机器上,编译php的时候,需要通过参数 --with-mysql来指定mysql的安装路径,但在生产环境中,通常php和mysql是不在同一台

如何确定Hadoop守护进程分别会在哪台机器上运行

经过一段时间的配置,Hadoop环境总算运行起来了,但是呢,为何主节点就没有跑tasktracker和datanode进程,slave节点也没有跑secondary进程,Hadoop是如何控制的呢? 经过看权威指南(267页)和跟群里同学讨论,还有自己测试,最终确定: tasktracker和datanode这两个守护进程 —— 只会在conf/slaves文件里指定的那些节点上运行 secondarynamenode 这个守护进程 —— 只会在conf/masters文件里指定的那个节点上运行

如何在一台机器上配置多个git的rsa

如何在一台机器上配置多个git的rsa 问题的提出 很多时候,我们一台机器上要使用多个git库,比如 github, csdn 以及 自己公司的.那么 rsa就要有多份.那么该如何让这些共同存在呢? 原理就是:建立多个不同的rsa 然后 在ssh config中分别不同的配置. 具体步骤 1 建立rsa ssh-keygen -t rsa -C "你的邮箱地址" 执行完这条命令之后, 会弹出如下提示: Enter file in which to save the key (/User

在一台机器上搭建多个redis实例

默认Redis程序安装在/usr/local/redis目录下: 配置文件:/usr/local/redis/redis.conf,该配置文件中配置的端口为默认端口:6379: Redis的启动命令路径:/usr/local/bin/redis-server. 可以指定端口启动多个Redis进程. #/usr/local/bin/redis-server --port 6380 &    #启动6380端口的redis实例. ====================以下每个进程对应一个配置文件(

一台机器上运行多个ActiveMq

由于业务需要一台机器上运行多个ActiveMq,这里主要说一下有什么地方不重复: 1.brokerName名称不能重复 2.端口号不能重复uri = tcp://localhost:50509 3.kahadb路径不能重复 4.管理端口不能重复contextPort = 2019 jmxServiceUrl = service:jmx:rmi:///jndi/rmi://localhost:2019/jmxrmi

在同一台机器上启动多个tomcat服务

一台机器上启动多个tomcat服务应用,能够让我们更好的测试下自己的分布式应用,下面简单介绍下如何在一台机器上开启多个tomcat应用,其实会弄两个,之后的多个都是一样的了 找到电脑上的tomcat安装目录,复制一份,出现两份tomcat,为了区别给复制的一份起一个特别的名称apache-tomcat-6.0.35-8090 进入apache-tomcat-6.0.35-8090目录中,进入conf目录下找到server.xml需要进行多个地方修改 conf目录下修改sever.xml 主要修改

java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息

1.java使用Jsch实现远程操作linux服务器进行文件上传.下载,删除和显示目录信息. 参考链接:https://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html https://www.cnblogs.com/longyg/archive/2012/06/25/2561332.html https://www.cnblogs.com/qdwyg2013/p/5650764.html#top 引入jar包的maven依赖如下所示:

同一台机器上有多个Python版本?

有关Python网站上的官方文档,如何在Linux上的同一台机器上安装和运行多个版本的Python? 我可以找到大量的博客帖子和答案,但我想知道是否有“标准”官方方式这样做? 或者这完全取决于操作系统? 解决方案 我认为它是完全独立的.只需安装它们,然后你就可以使用命令/usr/bin/python2.5和/usr/bin/python2.6.链接/usr/bin/python到您要用作默认值的链接. 无论如何,所有库都在单独的文件夹中(以版本命名). 如果要手动编译版本,请参阅Python源代

用pf透明地将流量从一台机器转到另一台机器上的缘起及实现方式对比

下面是也是我在12580工作时发生的事情,重新记录并发出来.这种特殊需求很考 验PF的功底.在新旧系统并存,做重构的时候有时很需要这种救急的作法.一.缘起miscweb1(172.16.88.228)的系统近段时间经常死掉,没有查到最终原因,现在的 策略是将其中一个端口上的服务摘出来,以确认问题,所以新准备了另一台机器 (172.16.88.116),由于miscweb1上还有别的服务,所以不能通过切换域名到新机器的 方式进行测试,另外也不方便让所有调用待迁移服务的部门手工改程序调用新的机器,这