美文网首页
shell脚本

shell脚本

作者: id_rsa | 来源:发表于2018-10-05 09:42 被阅读0次

which bash 查看bash 的安装路径


图片.png

1创建shell 脚本文件

touch ip.sh
#!/bin/bash                 声明( 必须写在shell 脚本的第一行)
#----------------
#备注:
#script funtion is scan live host ip
# writer name is ttgo2
#-----------------


赋予 shell 脚本 执行权限

chmod +x ip.sh
脚本 执行前可以先 进行调试

bash -x t.sh  调试脚本

解决expect实现单台、多台服务器批量scp传输文件的问题

1 单台传输

ip.sh (shell脚本)

#!/bin/bash
#------------------
name=$1         #接收第一个位置参数
passwd=$2
ip=$3
file=$4
./scp.exp $name $passwd $ip $file  #调用scp.exp 脚本

scp.exp (expect 脚本)

#!/usr/bin/expect
set timeout 20

set username [lindex $argv 0]     #接收参数
set passwd [lindex $argv 1]
set host [lindex $argv 2]
set src_file [lindex $argv 3]
spawn scp -r $src_file $username@$host:/tmp/      #spawn 执行shell 脚本的命令

expect {
      "(yes/no)?"
          {
              send "yes\r"
              expect "password:" { send "$passwd\n" }
           }
       "password:" 
           {
                send "$passwd\n"
           }
        }

expect "100%"
expect eof

执行前 赋予 文件执行权限 chmod +x ip.sh

执行 ./ip.sh 脚本就可以向目标主机传文件了 (别忘了传参 例: ./ip.sh root 123 10.66.8.56 test.txt)

2 多台传输

首先创建一个存放 ip name password (file path)
也可以用 nmap -sn 扫描存活主机 并将扫描结果 导入文件

这里我 只创建一个 server_list.conf 文件存放要 传给的目标

lmt 7890 10.22.9.41 test.txt   #(test.txt 在当前文件夹下  如果在其它地方记得写绝对路径)
liuxq root 10.22.9.21 test.txt

test.sh (创建一个shell 脚本 读入 server_list.conf 文件 并执行ip.sh脚本 ip.sh 再调用scp.exp )

备注 ip.sh 和 scp.exp 和单台传输 是一样的

#!/bin/bash
host_list="server_list.conf"
cat $host_list | while read line  #一次读 一行
do
    username=`echo $line|awk '{print $1}'`
    password=`echo $line|awk '{print $2}'`
    host_ip=`echo $line|awk '{print $3}'`
        src_file=`echo $line|awk '{print $4}'`

./ip.sh $username $password $host_ip $src_file

done

相关文章

  • 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/ktmcoftx.html