简体   繁体   English

使用Shell脚本在不同服务器上(从文本文件中获取)运行多个命令并将输出写入另一个文件

[英]To Run multiple commands on different servers (taken from a text file) using shell script and writing the output to another file

Sorry for the trouble.. here is the code what works for a single server. 很抱歉给您带来麻烦。这是适用于单个服务器的代码。 I need help to loop it to multiple servers. 我需要帮助将其循环到多个服务器。 Thanks in advance, Please help me out . 在此先感谢,请帮帮我。

I need to know information of multiple servers like their: 我需要了解多个服务器的信息,例如:

Operating system info
Hostname and dns info
Network info
Who is online
Last logged in users and so on

logic is to pass the server names from a text file and display the same info for all the server in the file and write output to other file 逻辑是从文本文件传递服务器名称,并在文件中显示所有服务器的相同信息,然后将输出写入其他文件

Below are the different fucntions which fetch the details of a server. 以下是获取服务器详细信息的不同功能。 The same should be iterated for multiple servers.. 对于多个服务器,应该重复相同的操作。

#!/bin/bash
# grabsysinfo.sh - A simple menu driven shell script to to get information about your 
# Linux server / desktop.

# Define variables
LSB=/usr/bin/lsb_release

# Purpose: Display pause prompt
# $1-> Message (optional)
function pause(){
local message="$@"
[ -z $message ] && message="Press [Enter] key to continue..."
read -p "$message" readEnterKey
}

# Purpose - Display a menu on screen
function show_menu(){
date
echo "---------------------------"
echo " Main Menu"
echo "---------------------------"
echo "1. Operating system info"
echo "2. Hostname and dns info"
echo "3. Network info"
echo "4. Who is online"
echo "5. Last logged in users"
echo "6. Free and used memory info"
echo "7. exit"
}

# Purpose - Display header message
# $1 - message
function write_header(){
local h="$@"
echo "---------------------------------------------------------------"
echo " ${h}"
echo "---------------------------------------------------------------"
}

# Purpose - Get info about your operating system
function os_info(){
write_header " System information "
echo "Operating system : $(uname)"
[ -x $LSB ] && $LSB -a || echo "$LSB command is not insalled (set \$LSB variable)"
#pause "Press [Enter] key to continue..."
pause
}

# Purpose - Get info about host such as dns, IP, and hostname
local dnsips=$(sed -e '/^$/d' /etc/resolv.conf | awk '{if (tolower($1)=="nameserver") print $2}')
write_header " Hostname and DNS information "
echo "Hostname : $(hostname -s)"
echo "DNS domain : $(hostname -d)"
echo "Fully qualified domain name : $(hostname -f)"
echo "Network address (IP) : $(hostname -i)"
echo "DNS name servers (DNS IP) : ${dnsips}"
pause
}

# Purpose - Network inferface and routing info
function net_info(){
devices=$(netstat -i | cut -d" " -f1 | egrep -v "^Kernel|Iface|lo")
write_header " Network information "
echo "Total network interfaces found : $(wc -w <<<${devices})"

echo "*** IP Addresses Information ***"
ip -4 address show

echo "***********************"
echo "*** Network routing ***"
echo "***********************"
netstat -nr

echo "**************************************"
echo "*** Interface traffic information ***"
echo "**************************************"
netstat -i

pause 
}

# Purpose - Display a list of users currently logged on 
# display a list of receltly loggged in users 
function user_info(){
local cmd="$1"
case "$cmd" in 
who) write_header " Who is online "; who -H; pause ;;
last) write_header " List of last logged in users "; last ; pause ;;
esac 
}

# Purpose - Display used and free memory info
function mem_info(){
write_header " Free and used memory "
free -m

echo "*********************************"
echo "*** Virtual memory statistics ***"
echo "*********************************"
vmstat
echo "***********************************"
echo "*** Top 5 memory eating process ***"
echo "***********************************" 
ps auxf | sort -nr -k 4 | head -5 
pause
}
# Purpose - Get input via the keyboard and make a decision using case..esac 
function read_input(){
local c
read -p "Enter your choice [ 1 - 7 ] " c
case $c in
1) os_info ;;
2) host_info ;;
3) net_info ;;
4) user_info "who" ;;
5) user_info "last" ;;
6) mem_info ;;
7) echo "Bye!"; exit 0 ;;
*) 
echo "Please select between 1 to 7 choice only."
pause
esac
}

# ignore CTRL+C, CTRL+Z and quit singles using the trap
trap '' SIGINT SIGQUIT SIGTSTP

First you need Key-Based SSH login to your remote servers ( https://help.ubuntu.com/community/SSH/OpenSSH/Keys#Key-Based_SSH_Logins ) 首先,您需要基于密钥的SSH登录到远程服务器( https://help.ubuntu.com/community/SSH/OpenSSH/Keys#Key-Based_SSH_Logins

After that you use a loop like this: 之后,使用如下循环:

function read_input(){
    read -p "Enter your choice [ 1 - 7 ] " c
    for server in $(cat your_server_file);
    do
        case $c in
          1) os_info $server;;
          2) host_info $server;;
          3) net_info $server;;
          4) user_info "who" $server;;
          5) user_info "last" $server;;
          6) mem_info $server;;
          7) echo "Bye!"; exit 0 ;;
          *) 
              echo "Please select between 1 to 7 choice only."
              pause
              esac
   done
}

And in example your mem_info function 在示例中,您的mem_info函数

function mem_info(){
    server=$1
    write_header " Free and used memory "
    ssh ssh_remote_user@server free -m
    echo "*********************************"
    echo "*** Virtual memory statistics ***"
    echo "*********************************"
    ssh ssh_remote_user@server vmstat
    echo "***********************************"
    echo "*** Top 5 memory eating process ***"
    echo "***********************************" 
    ssh ssh_remote_user@server ps auxf | sort -nr -k 4 | head -5 
    pause
}

UPDATE 更新

If you use sshpass you have to change a little bit. 如果使用sshpass ,则必须进行一些更改。

Your your_server_file should look like this: 您的your_server_file应该如下所示:

user1@password1|user1@server1
user2@password2|user2@server2
 ...

Your main function 您的主要职能

function read_input(){
    read -p "Enter your choice [ 1 - 7 ] " c
    for line in $(cat your_server_file);
    do
        user_pass=$(echo $line | sed -e 's/\(.*\)|\(.*\)/\1/')
        server=$(echo $line | sed -e 's/\(.*\)|\(.*\)/\2/')
        case $c in
          1) os_info $server $user;;
          2) host_info $server $user;;
          3) net_info $server $user;;
          4) user_info "who" $server $user;;
          5) user_info "last" $server $user;;
          6) mem_info $server $user;;
          7) echo "Bye!"; exit 0 ;;
          *) 
              echo "Please select between 1 to 7 choice only."
              pause
              esac
   done
}

And the mem_info function 还有mem_info函数

function mem_info(){
    server=$1
    user_pass=$2
    write_header " Free and used memory "
    sshpass -p $user_pass ssh $server free -m
    echo "*********************************"
    echo "*** Virtual memory statistics ***"
    echo "*********************************"
    sshpass -p $user_pass ssh $server vmstat
    echo "***********************************"
    echo "*** Top 5 memory eating process ***"
    echo "***********************************" 
    sshpass -p $user_pass ssh $server ps auxf | sort -nr -k 4 | head -5 
    pause
}

I did not quote anything. 我什么也没引用。 Please take care about special characters 请注意特殊字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用shell脚本在文件对上运行多个命令? - How to run multiple commands on file pairs using a shell script? 从shell脚本中的另一个文件中读取命令? - Reading commands from another file in a shell script? 在单个文件上运行多个 shell 命令并将输出重定向到同一个文件 - Run multiple shell commands on a single file and redirect the output to the same file 具有多个命令的Shell脚本文件 - Shell script file with multiple commands 从文本文件中读取命令并将其传递给 Shell 中的脚本 - Reading commands from text file and passing it into Script in Shell 从文件中读取输入,然后将输出保存到Shell脚本中的另一个文件中 - Read the inputs from a file and save the output to another file in shell script 如何从另一个Shell脚本运行一个Shell脚本,该脚本从CSV文件中读取第一个Shell脚本名称 - How to run a shell script from another shell script reading first shell script name from a CSV file 将文本复制到另一个文件中并附加不同的字符串Shell脚本 - copy text in another file and append different strings shell script 使用 python buitin 命令写入文件与使用子进程运行类似的 shell 命令 - Using python buitin commands for writing to a file vs using subprocess to run similar shell command 通过bash脚本从文本文件运行命令 - Run commands from a text file through a bash script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM