2021-06-30

linux 各节点间做ssh互信,免密登陆(脚本方式)

通过脚本实现

下面展示一些 内联代码片

[root@vp02 ~]# ssh nn01
Last login: Wed Jun 30 16:10:03 2021 from 10.0.4.82
[root@nn01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:f1:14:ea brd ff:ff:ff:ff:ff:ff
    inet 10.0.4.88/24 brd 10.0.4.255 scope global ens192
       valid_lft forever preferred_lft forever
    inet6 fe80::e5b6:9af7:1d4:ffd1/64 scope link 
       valid_lft forever preferred_lft forever
[root@nn01 ~]# exit
logout
Connection to nn01 closed.
[root@vp02 ~]# 

[root@vp02 ~]# ssh nn01
Last login: Wed Jun 30 16:10:03 2021 from 10.0.4.82
[root@nn01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:f1:14:ea brd ff:ff:ff:ff:ff:ff
    inet 10.0.4.88/24 brd 10.0.4.255 scope global ens192
       valid_lft forever preferred_lft forever
    inet6 fe80::e5b6:9af7:1d4:ffd1/64 scope link 
       valid_lft forever preferred_lft forever
[root@nn01 ~]# exit
logout
Connection to nn01 closed.
[root@vp02 ~]# 

需要软件expect

下面展示一些 内联代码片

[root@vp02 ~]# yum -y install expect
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
MooseFS                                                                                          |  951 B  00:00:00     
rhel7                                                                                            | 4.1 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package expect.x86_64 0:5.45-14.el7_1 will be installed
--> Processing Dependency: libtcl8.5.so()(64bit) for package: expect-5.45-14.el7_1.x86_64
--> Running transaction check
---> Package tcl.x86_64 1:8.5.13-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================
 Package                   Arch                      Version                             Repository                Size
========================================================================================================================
Installing:
 expect                    x86_64                    5.45-14.el7_1                       rhel7                    262 k
Installing for dependencies:
 tcl                       x86_64                    1:8.5.13-8.el7                      rhel7                    1.9 M

Transaction Summary
========================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 2.1 M
Installed size: 4.9 M
Downloading packages:
(1/2): expect-5.45-14.el7_1.x86_64.rpm                                                           | 262 kB  00:00:00     
(2/2): tcl-8.5.13-8.el7.x86_64.rpm                                                               | 1.9 MB  00:00:00     
------------------------------------------------------------------------------------------------------------------------
Total                                                                                    16 MB/s | 2.1 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:tcl-8.5.13-8.el7.x86_64                                                                            1/2 
  Installing : expect-5.45-14.el7_1.x86_64                                                                          2/2 
  Verifying  : 1:tcl-8.5.13-8.el7.x86_64                                                                            1/2 
  Verifying  : expect-5.45-14.el7_1.x86_64                                                                          2/2 

Installed:
  expect.x86_64 0:5.45-14.el7_1                                                                                         

Dependency Installed:
  tcl.x86_64 1:8.5.13-8.el7                                                                                             

Complete!
[root@vp02 ~]# which expect
/usr/bin/expect

[root@vp02 ~]# yum -y install expect
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
MooseFS                                                                                          |  951 B  00:00:00     
rhel7                                                                                            | 4.1 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package expect.x86_64 0:5.45-14.el7_1 will be installed
--> Processing Dependency: libtcl8.5.so()(64bit) for package: expect-5.45-14.el7_1.x86_64
--> Running transaction check
---> Package tcl.x86_64 1:8.5.13-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================
 Package                   Arch                      Version                             Repository                Size
========================================================================================================================
Installing:
 expect                    x86_64                    5.45-14.el7_1                       rhel7                    262 k
Installing for dependencies:
 tcl                       x86_64                    1:8.5.13-8.el7                      rhel7                    1.9 M

Transaction Summary
========================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 2.1 M
Installed size: 4.9 M
Downloading packages:
(1/2): expect-5.45-14.el7_1.x86_64.rpm                                                           | 262 kB  00:00:00     
(2/2): tcl-8.5.13-8.el7.x86_64.rpm                                                               | 1.9 MB  00:00:00     
------------------------------------------------------------------------------------------------------------------------
Total                                                                                    16 MB/s | 2.1 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:tcl-8.5.13-8.el7.x86_64                                                                            1/2 
  Installing : expect-5.45-14.el7_1.x86_64                                                                          2/2 
  Verifying  : 1:tcl-8.5.13-8.el7.x86_64                                                                            1/2 
  Verifying  : expect-5.45-14.el7_1.x86_64                                                                          2/2 

Installed:
  expect.x86_64 0:5.45-14.el7_1                                                                                         

Dependency Installed:
  tcl.x86_64 1:8.5.13-8.el7                                                                                             

Complete!
[root@vp02 ~]# which expect
/usr/bin/expect

安装expect
yum -y install expect
脚本

下面展示一些 内联代码片

#!/bin/bash

echo "PassWord: "
read -s password
user="op"
masterip="10.0.4.81"
dir="/home/op"

function_ssh(){
  for ip in `cat ip.list`
  do
  /usr/bin/expect<< EOF
  set timeout -1
  spawn ssh $user@$ip ssh-keygen -t rsa
  expect {
         "yes/no" {send "yes\r";exp_continue}
         "*assword*" {send "$password\r";exp_continue}
         "id_rsa" {send "\r";exp_continue}
         "Overwrite" {send "y\r";exp_continue}
         "phrase" {send "\r";exp_continue}
         "again" {send "\r"}
        }
EOF
done
}
#!/bin/bash

echo "PassWord: "
read -s password
user="op"
masterip="10.0.4.81"
dir="/home/op"

function_ssh(){
  for ip in `cat ip.list`
  do
  /usr/bin/expect<< EOF
  set timeout -1
  spawn ssh $user@$ip ssh-keygen -t rsa
  expect {
         "yes/no" {send "yes\r";exp_continue}
         "*assword*" {send "$password\r";exp_continue}
         "id_rsa" {send "\r";exp_continue}
         "Overwrite" {send "y\r";exp_continue}
         "phrase" {send "\r";exp_continue}
         "again" {send "\r"}
        }
EOF
done
}
以下是一个更复杂的 SQL 查询语句,它使用了多个子查询和窗口函数: WITH -- 定义一个子查询,获取销售额排名前10的产品 top_products AS ( SELECT product_id, SUM(sales) AS total_sales FROM orders WHERE order_date BETWEEN '2021-01-01' AND '2021-06-30' GROUP BY product_id ORDER BY total_sales DESC LIMIT 10 ), -- 定义一个子查询,获取销售额排名前10的客户 top_customers AS ( SELECT customer_id, SUM(sales) AS total_sales FROM orders WHERE order_date BETWEEN '2021-01-01' AND '2021-06-30' GROUP BY customer_id ORDER BY total_sales DESC LIMIT 10 ), -- 定义一个窗口函数,计算每个客户的销售额排名 customer_sales_rank AS ( SELECT customer_id, SUM(sales) AS total_sales, ROW_NUMBER() OVER (ORDER BY SUM(sales) DESC) AS sales_rank FROM orders WHERE order_date BETWEEN '2021-01-01' AND '2021-06-30' GROUP BY customer_id ) -- 最终查询,获取纽约市销售额排名前10的客户,以及他们购买的销售额排名前10的产品 SELECT customers.id AS customer_id, customers.name AS customer_name, products.id AS product_id, products.name AS product_name, SUM(orders.sales) AS total_sales FROM orders -- 连接顾客信息 INNER JOIN customers ON orders.customer_id = customers.id -- 连接产品信息 INNER JOIN products ON orders.product_id = products.id -- 仅查询纽约市的客户 WHERE customers.city = 'New York' -- 仅查询销售额排名前10的客户 AND customers.id IN (SELECT customer_id FROM top_customers) -- 仅查询销售额排名前10的产品 AND products.id IN (SELECT product_id FROM top_products) -- 仅查询客户销售额排名前10的订单 AND customers.id IN (SELECT customer_id FROM customer_sales_rank WHERE sales_rank <= 10) GROUP BY customers.id, customers.name, products.id, products.name ORDER BY customers.id, total_sales DESC, products.id;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值