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远程操作另外一台机器上数据的更多相关文章

  1. 不要将缓存服务器与Tomcat放在单台机器上,否则出现竞争内存问题

    缓存分为本地缓存和远程分布式缓存,本地缓存访问速度更快但缓存数据量有限,同时存在与应用程序争用内存的情况. 1.不要将缓存服务器与Tomcat放在单台机器上,否则出现竞争内存问题 2.不要将缓存服务器 ...

  2. Hexo博客系列(二)-在多台机器上利用Hexo发布博客

    [原文链接]:https://www.tecchen.xyz/blog-hexo-env-02.html 我的个人博客:https://www.tecchen.xyz,博文同步发布到博客园. 由于精力 ...

  3. Git 在同一台机器上配置多个Git帐号

    在同一台机器上配置多个Git帐号 By:授客 QQ:1033553122 实践环境 win10 Git-2.21.0-64-bit.exe TortoiseGit-2.8.0.0-64bit.msi ...

  4. 解决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 ...

  5. 100台机器上海量IP如何查找出现频率 Top 100?

    场景题 有 100 机器,每个机器的磁盘特别大,磁盘大小为 1T,但是内存大小只有 4G,现在每台机器上都产生了很多 ip 日志文件,每个文件假设有50G,那么如果计算出这 100 太机器上访问量最多 ...

  6. window下在同一台机器上安装多个版本jdk,修改环境变量不生效问题处理办法

    window下在同一台机器上安装多个版本jdk,修改环境变量不生效问题处理办法 本机已经安装了jdk1.7,而比较早期的项目需要依赖jdk1.6,于是同时在本机安装了jdk1.6和jdk1.7. 安装 ...

  7. 通过Mouse Without Borders在多台机器上共享键盘鼠标

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:通过Mouse Without Borders在多台机器上共享键盘鼠标.

  8. 在同一台机器上让Microsoft SQL Server 2000/ SQL2005/ SQL2008共存

    可能很多朋友都遇到想同时在自己的机器上运行Microsoft SQL Server 2000以及Microsoft SQL Server 2005和Microsoft SQL Server 2008. ...

  9. 一台机器上运行多个ActiveMq

    由于业务需要一台机器上运行多个ActiveMq,这里主要说一下有什么地方不重复: 1.brokerName名称不能重复 2.端口号不能重复uri = tcp://localhost:50509 3.k ...

随机推荐

  1. Linux学习(二)-Xshell 5和Xftp 5的安装和使用

    (一)软件介绍: (1)Xshell: Xshell通过互联网可以连接到远程的服务器,然后通过模拟终端来实现对服务器的各种操作,而且这款软件可以很好的解决中文乱码问题,非常的方便快捷. (2)Xftp ...

  2. case when语法

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数: CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...

  3. S/4HANA中的销售计划管理

    大家好,我所在的S/4HANA Sales(SD)成都研发团队,主要负责S/4HANA里销售模块相关的标准产品研发. 作为产品研发团队,我们遵循SCRUM迭代式增量软件开发过程,以两个星期为一个迭代, ...

  4. 运行tomcat7w.exe提示指定的服务未安装 解决办法

    一.问题重现点击bin下tomcat7w.exe出现如下提示:提示指定的服务未安装 二.原因分析tomcat7.exe和tomcat7w.exe要起作用必须先为这两个文件安装服务.其中tomcat7. ...

  5. 【Distributed】CDN

    一.概述 1.1 Web前端优化 1.2 DNS域名解析过程 1.3 传统方式请求静态资源 二.CDN内容分发 2.1 什么是CDN 2.2 CDN内容分发原理 2.3 阿里云环境实战搭建CDN内容分 ...

  6. PHP中pdo的使用

    <?php /** *下面代码中information为表名 * */ //1.先要连数据库 $pdo=new PDO('mysql:host=localhost;dbname=数据库名','用 ...

  7. VMware虚拟机与Linux Centos7下载及安装教程

    1.CentOS下载CentOS是免费版,推荐在官网上直接下载,网址:https://www.centos.org/download/ DVD ISO:普通光盘完整安装版镜像,可离线安装到计算机硬盘上 ...

  8. CAFFE(FAQ.2):Ubuntu 配置caffe 框架之数据库读取,错误解决:ImportError: No module named leveldb解决办法

    Z: 在安装了caffe框架后需要读取大量的数据进行学习训练.比如在MNIST识别训练中,一般直接读图片会比较耗时,我们一般将图片转存为数据库中.目前主流的数据库有以下两种选择: LevelDB Lm ...

  9. winfrom 自动关闭 重新MessageBox.Show("Test");

    复制代码  自动关闭 调用    AutoClosingMessageBox.Show("添加失败", "提示", 1000); #region alert p ...

  10. web开发:jquery之DOM

    一.文档结构 二.文档操作 三.文档操作案例 四.form表单 五.正则 六.form案例 一.文档结构 ```jsvar $sup = $('.sup');console.log($sup.chil ...