zoukankan      html  css  js  c++  java
  • 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:
    60:25:67:28:bb:50:92:d4:d6:ce:68:40:60:8c:e5:d2 root@localhost.localdomain
    The key's 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)' can't 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.33's 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)' can't 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.32's 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 密码的输入

  • 相关阅读:
    JZ-C-36
    JZ-C-35
    JZ-C-34
    JZ-C-33
    JZ-C-32
    JZ-C-31
    JZ-C-30
    JZ-C-29
    JZ-C-28
    JZ-C-27
  • 原文地址:https://www.cnblogs.com/qq931399960/p/11567938.html
Copyright © 2011-2022 走看看