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 ...
随机推荐
- bash shell脚本之查看当前日期以及登陆用户
查看当前日期以及登陆用户: cat test1: #!/bin/bash # This script displays the date and who's logged on echo -n The ...
- 在Ubuntu中使用uwsgi 启动 Django ,但是静态文件映射出错
错误 : 找不到/static/下面的静态文件 解决方法: 在uswgi.ini 文件中配置参数 static-map=/static=/home/wb/Desktop/test_django/st ...
- SpringCloud之Ribbon负载均衡配置
一.负载均衡解决方案分类及特征 业界主流的负载均衡解决方案有: 1.1 集中式负载均衡 即在客户端和服务端之间使用独立的负载均衡设施(可以是硬件,如F5, 也可以是软件,如nginx), 由该设施负责 ...
- 【Distributed】大型网站高并发和高可用
一.DNS域名解析 二.大型网站系统应有的特点 三.网站架构演变过程 3.1 传统架构 3.2 分布式架构 3.3 SOA架构 3.4 微服务架构 四.高并发设计原则 4.1 拆分系统 4.2 服务化 ...
- CodeForcs 1169B Good Triple
CodeForcs 1169B Good Triple 题目链接:http://codeforces.com/problemset/problem/1169/B 题目描述:给你m对不超过n的数字,找出 ...
- c#中,点击一个菜单项后调用exe文件
using System.Diagnostics; private void 导出数据ToolStripMenuItem_Click(object sender, EventArgs e) { Pro ...
- linux——命令2—删除—查看—搜索
多种查看命令: 多种搜索命令: ll命令 -rw-rw-rw - 表示文件 drw-rw-rw d 表示目录文件夹 ========================== 使用rm删除文件 例如:r ...
- mysqll中索引详细讲解
MySQL(五) MySQL中的索引详讲 序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于My ...
- 【CQOI2017】老C的键盘
Description https://loj.ac/problem/3023 一句话题意:给你一棵完全二叉树,每条边有一个方向,求这棵树有多少种不同的拓扑序. Solution 简化题意后,其实就是 ...
- cmake学习笔记之add_library、target_link_libraries和link_directories
cmake是Linux(这里默认是Ubuntu系统)下常使用的编译C++的工具,而使用cmake就需要先在CmakeLists.txt文件中对编译规则进行.这里介绍常用的三种指令add_library ...