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 ...
随机推荐
- 给datagrid一列中的数据加上单位
{ field:'computeRate', title:'完成百分比', width:100, align:'center', halign:'center', sortable:true, for ...
- S5PV210 点亮Led
GPC1CON, R/W, Address = 0xE020_0080 GPC1DAT, R/W, Address = 0xE020_0084 举例 #define GPC1CON *((volati ...
- djnago中间件
前言 在form表单中当我们提交表单时会有这样的错误>>>>请求post时候的会出现403 forbidden,那我们就说说这个类中间件,(csrf只是中间件的一种) 以前我们 ...
- input子系统驱动
input子系统驱动 框架分析 核心层 文件为:/drivers/input/input.c: 首先找到入口函数为**static int __init input_init(void)**,在该函数 ...
- spark数据倾斜处理
spark数据倾斜处理 危害: 当出现数据倾斜时,小量任务耗时远高于其它任务,从而使得整体耗时过大,未能充分发挥分布式系统的并行计算优势. 当发生数据倾斜时,部分任务处理的数据量过大,可能造成内存不足 ...
- indexedDB 前端数据库(使用的简单案例)
前端存储 之 indexDB 1.indexedDB是什么? indexedDB是一个非关系型数据库 它不需要我们去写一些特定的SQL语句来对数据库进行操作 它是NoSQL的,数据形式使用的json ...
- BZOJ 1845: [Cqoi2005] 三角形面积并 (辛普森积分)
大力辛普森积分 精度什么的搞了我好久- 学到了Simpson的一个trick 深度开11,eps开1e-4.跑的比有些扫描线还快- CODE #include <bits/stdc++.h> ...
- Failed to start MariaDB database server. (已解决) 之前配过主从
[root@linux-node1 /var/log/mariadb]# systemctl status mariadb.service● mariadb.service - MariaDB dat ...
- 爬虫代理池源代码测试-Python3WebSpider
元类属性的使用 来源: https://github.com/Python3WebSpider/ProxyPool/blob/master/proxypool/crawler.py 主要关于元类的使用 ...
- [SDOI2006]最短距离
洛谷题目链接 声明: 本篇文章只大概讲思路 原串设为$s1$,目标串设为$s2$,$n1,n2$分别为他们的长度 我们考虑$dp$,设$f[i][j]$表示$s1$中删除到了第$i$个字符,$s2$中 ...