美文网首页
shell 脚本

shell 脚本

作者: jayz3 | 来源:发表于2019-05-12 17:09 被阅读0次
#!/usr/bin/ksh  
#usage ./ssh_trust.sh host1 user1 passwd1 host2 user2 passwd2  
#即建立从user1@host1到user2@host2的ssh信任。  
src_host=$1  
src_username=$2  
src_passwd=$3  
  
dst_host=$4  
dst_username=$5  
dst_passwd=$6  
  
#在远程主机1上生成公私钥对  
Keygen()  
{  
expect << EOF  
spawn ssh $src_username@$src_host ssh-keygen -t rsa  
while 1 {  
        expect {  
                 "password:" {  
                              send "$src_passwd\n"  
                               }  
                "yes/no*" {  
                            send "yes\n"  
                          }  
                        "Enter file in which to save the key*" {  
                                        send "\n"  
                        }  
                        "Enter passphrase*" {  
                                        send "\n"  
                        }  
                        "Enter same passphrase again:" {  
                                        send "\n"  
                                        }  
  
                        "Overwrite (y/n)" {  
                                        send "n\n"  
                        }  
                        eof {  
                                   exit  
                        }  
        }  
}  
EOF
}  

#从远程主机1获取公钥保存到本地  
Get_pub()  
{  
expect << EOF  
spawn scp $src_username@$src_host:~/.ssh/id_rsa.pub /tmp  
expect {  
             "password:" {  
                           send "$src_passwd\n";exp_continue  
                }  
                "yes/no*" {  
                           send "yes\n";exp_continue  
                }     
                eof {  
                                exit  
                }  
}  
EOF
}  
#将公钥的内容附加到远程主机2的authorized_keys  
Put_pub()  
{  
src_pub="$(cat /tmp/id_rsa.pub)"  
expect << EOF  
spawn ssh $dst_username@$dst_host "mkdir -p ~/.ssh;echo $src_pub >> ~/.ssh/authorized_keys;chmod 600 ~/.ssh/authorized_keys"  
expect {  
            "password:" {  
                        send "$dst_passwd\n";exp_continue  
             }  
            "yes/no*" {  
                        send "yes\n";exp_continue  
             }     
            eof {  
                        exit  
             }   
}  
EOF
}

Keygen  
Get_pub  
Put_pub  

相关文章

  • Shell入门笔记

    Shell脚本:Linux Shell脚本学习指南菜鸟教程 - Shell教程Linux入门 - Shell脚本是...

  • 2018-09-26

    shell脚本 1.1、什么是shell脚本(shell script , ...

  • Shell script + crontab实现Mysql定时备

    一、Shell 脚本 Shell 脚本(shell script),是一种为 shell 编写的脚本程序。 业界所...

  • 嵌入式day12

    shell脚本的本质 shell脚本语言是解释型语言 shell脚本的本质:shell命令的有序集合 shell编...

  • shell脚本

    什么是shell脚本 Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所说...

  • Shell脚本语法

    1. Shell脚本简介Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所...

  • shell脚本

    什么是Shell脚本 Shell脚本(英语:Shell script),又称Shell命令稿、程序化脚本,是一种电...

  • 【生物信息笔记】shell 脚本 (dry-2)

    shell 和 shell script(脚本)区别: shell 和 shell 脚本是两个不同概念,shell...

  • chapter 11. 构建基本脚本

    创建shell脚本 shell脚本第一行为指定具体shell来运行该脚本,可以指定shell(待验证) echo ...

  • PySparkSQL脚本模板

    PySpark模板分为shell脚本和python脚本两部分,通过shell脚本提交spark任务。 shell脚...

网友评论

      本文标题:shell 脚本

      本文链接:https://www.haomeiwen.com/subject/zkncaqtx.html