码迷,mamicode.com
首页 > 系统相关 > 详细

shell远程操作另外一台机器上数据

时间:2019-09-22 16:47:30      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:bash   finger   try   dom   warning   mys   防止   font   save   

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:
60:25:67:28:bb:50:92:d4:d6:ce:68:40:60:8c:e5:d2 root@localhost.localdomain
The keys randomart image is:
+--[ RSA 2048]----+
|+*+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) cant be established.
ECDSA key fingerprint is cb:96:8a:bc:74:22:aa:f9:6e:1a:7e:7c:95:7d:2f:03.
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: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.3.33s password: 

Number of key(s) added: 1

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) cant be established.
ECDSA key fingerprint is 0e:38:cc:da:2e:27:33:16: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: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.3.32s password: 

Number of key(s) added: 1

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 23 00:01:04 2019 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 22 23:59:56 2019 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: 414
3.33 mysql position: 573

二、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: 414
3.33 mysql position: 573

与第一种方式相比,只是多了sshpass -p 密码的输入

 

shell远程操作另外一台机器上数据

标签:bash   finger   try   dom   warning   mys   防止   font   save   

原文地址:https://www.cnblogs.com/qq931399960/p/11567938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!