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. 利用css伪类选择器hover控制两个元素属性

    示例1: <html> <body> <style> #a:hover {color : #FFFF00;} #a:hover > #b:first-chil ...

  2. js之数据类型(对象类型——单体内置对象——JSON)

    JSON(Java Script Object Notation)使用JavaScript语法,是用于存储和传输数据的格式,通常用于服务端向网页传递数据.JSON格式仅仅是一个文本,文本可以被任何编程 ...

  3. 18.SSM整合_搭建开发环境

    1.导入jar包 mybatis的Jar包 ehcache的Jar包 spring的 Jar包 mybatis 与 spring 整合Jar包 JSON的jar包 Jaskson的Jar包 Hiber ...

  4. js 扁平化输出数组

    1.使用数组的flat方法 [1,2,[3,[4,5]]].flat(Infinity) //[1, 2, 3, 4, 5] 2.实现方式二: var arr = [[1, 2, 23], [13, ...

  5. GitHub代码复现之opencv

    GitHub代码复现之opencv链接:https://github.com/vonzhou/opencv 待解决!!! ISSUE汇总: Issue1:vs2015找不到配置dirent.h头文件? ...

  6. 怎么解决Win7电脑更新出现80072EE2代码的错误?

    我们在使用Win7系统时经常会遇到更新,这些更新可以修复一些系统漏洞,提高系统的安全性.但有时我们在进行相关更新时会出现错误,而导致最后的更新失败.下面好系统重装助手就和大家分享一下Win7系统更新出 ...

  7. 4.1. Scrapy配置安装

    Scrapy的安装介绍 Scrapy框架官方网址:http://doc.scrapy.org/en/latest Scrapy中文维护站点:http://scrapy-chs.readthedocs. ...

  8. linux-2.6.38poll机制简析(以tiny6410按键中断程序为基础)

    一.应用程序 /* struct pollfd { int fd; //文件描述符 short events; //表示请求检测的事件 short revents; //表示检测之后返回的事件 }; ...

  9. windows BAT脚本2个服务器间传递文件

    1. 脚本功能: 实现2个服务器间文件的传递,例如从A服务器往B服务器上传文件 2. 实现步骤: 2.1 服务器连结,找到指定路径,读取所需要上传的文件,将文件名称复制到一个文件下 (此处考虑可能需要 ...

  10. 5.caffe:train.sh 和 test.sh (训练与测试 )

    一,train.sh #!/usr/bin/env sh ./build/tools/caffe train --solver=myself/00b/solver.prototxt # cd CAFF ...