shell远程操作另外一台机器上数据
shell远程操作另外一台机器上的数据,有两种方式: 1 、配置免密登陆,2、使用sshpass
当前存在两台虚拟机,ip地址分别为:192.168.3.32 192.168.3.33
一、免密登陆操作另外一台机器
1、生成秘钥
两台机器上都做如下操作,三次输入,直接摁回车
[root@localhost 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:
::::bb:::d4:d6:ce::::8c:e5:d2 root@localhost.localdomain
The key's randomart image is:
+--[ RSA ]----+
|+*+o ...+ |
|o++ = o= |
|. E= *o |
| .. +.o. |
| o . S |
| . |
| |
| |
| |
+-----------------+
2、认证
在3.32上执行如下命令,并输入3.33密码
[root@localhost work]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@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::8a:bc:::aa:f9:6e:1a:7e:7c::7d:2f:.
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: key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.3.33's password: Number of key(s) added: Now try logging into the machine, with: "ssh 'root@192.168.3.33'"
and check to make sure that only the key(s) you wanted were added.
在3.33上执行如下命令,并输入3.32密码
[root@localhost work]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@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::cc:da:2e::::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: key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.3.32's password: Number of key(s) added: Now try logging into the machine, with: "ssh 'root@192.168.3.32'"
and check to make sure that only the key(s) you wanted were added.
3、验证免密登陆
在3.32上执行如下命令,提示登陆成功后,输入exit退出,以免操作错误
[root@localhost work]# ssh 192.168.3.33
Last login: Mon Sep :: from 192.168.3.32
[root@localhost ~]# exit
logout
Connection to 192.168.3.33 closed.
在3.33上执行如下命令,提示登陆成功后,输入exit退出,以免操作错误
[root@localhost work]# ssh 192.168.3.32
Last login: Sun Sep :: from 192.168.3.32
[root@localhost ~]# 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解锁表
[root@localhost 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:
3.33 mysql position:
二、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 root@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
输出结果
[root@localhost 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:
3.33 mysql position:
与第一种方式相比,只是多了sshpass -p 密码的输入
shell远程操作另外一台机器上数据的更多相关文章
- 不要将缓存服务器与Tomcat放在单台机器上,否则出现竞争内存问题
缓存分为本地缓存和远程分布式缓存,本地缓存访问速度更快但缓存数据量有限,同时存在与应用程序争用内存的情况. 1.不要将缓存服务器与Tomcat放在单台机器上,否则出现竞争内存问题 2.不要将缓存服务器 ...
- Hexo博客系列(二)-在多台机器上利用Hexo发布博客
[原文链接]:https://www.tecchen.xyz/blog-hexo-env-02.html 我的个人博客:https://www.tecchen.xyz,博文同步发布到博客园. 由于精力 ...
- Git 在同一台机器上配置多个Git帐号
在同一台机器上配置多个Git帐号 By:授客 QQ:1033553122 实践环境 win10 Git-2.21.0-64-bit.exe TortoiseGit-2.8.0.0-64bit.msi ...
- 解决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 ...
- 100台机器上海量IP如何查找出现频率 Top 100?
场景题 有 100 机器,每个机器的磁盘特别大,磁盘大小为 1T,但是内存大小只有 4G,现在每台机器上都产生了很多 ip 日志文件,每个文件假设有50G,那么如果计算出这 100 太机器上访问量最多 ...
- window下在同一台机器上安装多个版本jdk,修改环境变量不生效问题处理办法
window下在同一台机器上安装多个版本jdk,修改环境变量不生效问题处理办法 本机已经安装了jdk1.7,而比较早期的项目需要依赖jdk1.6,于是同时在本机安装了jdk1.6和jdk1.7. 安装 ...
- 通过Mouse Without Borders在多台机器上共享键盘鼠标
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:通过Mouse Without Borders在多台机器上共享键盘鼠标.
- 在同一台机器上让Microsoft SQL Server 2000/ SQL2005/ SQL2008共存
可能很多朋友都遇到想同时在自己的机器上运行Microsoft SQL Server 2000以及Microsoft SQL Server 2005和Microsoft SQL Server 2008. ...
- 一台机器上运行多个ActiveMq
由于业务需要一台机器上运行多个ActiveMq,这里主要说一下有什么地方不重复: 1.brokerName名称不能重复 2.端口号不能重复uri = tcp://localhost:50509 3.k ...
随机推荐
- JavaScript 的查询机制——LHS 与 RHS
JavaScript 引擎在查找一个变量的时候,有两种查找机制:LHS 和 RHS. RHS 的查询是简单地查找到某个变量的值,而 LHS 则是试图找到变量的容器的本身. 一个简单的例子:当我们执行 ...
- 微信小程序wxs如何使用
新建一个.wxs文件 <!-- 引入.wxs文件 src为相对路径,module指定当前模块的名称 --> <wxs module="filter" src=&q ...
- element-ui 中 el-table 相关操作
1.带checkbox 获取所有选择的行. this.$refs.multipleTable.selection 获取选中的单行 this.$refs.roleTable.store.states. ...
- Oracle创建索引;查询索引
1.创建索引 create index 索引名 on 表名(列名); 2.删除索引 drop index 索引名; 3.创建组合索引 create index 索引名 on 表名(列名1,,列名2); ...
- interrupt分析
转载自 https://blog.csdn.net/zhangliangzi/article/details/52485319 interrupt简述 interrupt() 方法只是改变中断状态而已 ...
- 如何用Java代码在SAP Marketing Cloud里创建contact数据
我们可以使用SAP Marketing Cloud提供的Contact create OData API在第三方应用里创建Contact主数据. API地址:/sap/opu/odata/sap/CU ...
- CentOS系统 Amoeba+MySql主从读写分离配置 适合新手傻瓜式教程!-----仅供参考!
废话不说,直接开始: 一.安装mysql的三种方式,这里采用第2种(安装方式不再详解,请参照) http://www.cnblogs.com/babywaa/articles/4837946.html ...
- Hadoop_30_MapReduce_多job串联
一个稍复杂点的处理逻辑往往需要多个mapreduce程序串联处理,多job的串联可以借助mapreduce框架的JobControl实现 示例代码: 每个job装配完成才可以进行下面代码: Cont ...
- redis——redis的一些核心把握
redis单线程,为什么比较快 单线程指的是网络请求模块使用了一个线程(所以不需考虑并发安全性),即一个线程处理所有网络请求,其他模块仍用了多个线程.redis能够快速执行的原因有三点: (1) 绝大 ...
- SSE
最近尝试了一下服务器端的推送,之前的做法都是客户端轮询,定时向服务器发送请求.但这造成了我的一些困扰: 1:轮询是由客户端发起的,那么在服务端就不能判别我要推送的内容是否已经过期,因为我很难判断某个信 ...