hostname.sh
#!/bin/bash
>/root/hostname.txt
#all hosts,seperated by space
all_hosts=(192.168.10.245)
for host in ${all_hosts[*]}
do
{
start_time=$(date +%s)
#need to set ssh trust
ssh $host "hostname" &>/dev/null
sleep 2
stop_time=$(date +%s)
time_consuming=$((stop_time-start_time))
echo "$host:$time_consuming" >>/root/hostname.txt
}&
done
#waiting until all background jobs ended
wait
host=$(sort -n -k 2 /root/hostname.txt | head -1 | awk -F':' '{print $1}')
ssh $host "top -b -n 1"
验证:
[root@logstash ~]# sh hostname.sh
top - 11:26:19 up 1:09, 1 user, load average: 0.01, 0.04, 0.05
Tasks: 184 total, 1 running, 183 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 4026152 total, 2692392 free, 685536 used, 648224 buff/cache
KiB Swap: 3145724 total, 3145724 free, 0 used. 3075424 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1149 root 20 0 1036724 33756 14220 S 6.2 0.8 0:24.56 containerd
1 root 20 0 125736 4208 2604 S 0.0 0.1 0:01.57 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0+
5 root 20 0 0 0 0 S 0.0 0.0 0:00.19 kworker/u+
6 root 20 0 0 0 0 S 0.0 0.0 0:00.04 ksoftirqd+
7 root rt 0 0 0 0 S 0.0 0.0 0:00.11 migration+
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.42 rcu_sched
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-d+
11 root rt 0 0 0 0 S 0.0 0.0 0:00.01 watchdog/0
12 root rt 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/1
13 root rt 0 0 0 0 S 0.0 0.0 0:00.01 migration+
14 root 20 0 0 0 0 S 0.0 0.0 0:00.04 ksoftirqd+
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1+
18 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
20 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd
21 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeback
22 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrit+
23 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
24 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
25 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
26 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kblockd
27 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 md
28 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 edac-poll+
29 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 watchdogd
35 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kswapd0
36 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd
37 root 39 19 0 0 0 S 0.0 0.0 0:00.15 khugepaged
38 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 crypto
46 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kthrotld
48 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kmpath_rd+
49 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kaluad
50 root 20 0 0 0 0 S 0.0 0.0 0:00.78 kworker/0+
51 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kpsmoused
53 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ipv6_addr+
66 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 deferwq
103 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kauditd
288 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 mpt_poll_0
290 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ata_sff
292 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 mpt/0
294 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 nfit
297 root 20 0 0 0 0 S 0.0 0.0 0:00.00 scsi_eh_0
298 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 scsi_tmf_0
300 root 20 0 0 0 0 S 0.0 0.0 0:00.01 scsi_eh_1
301 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 scsi_tmf_1
302 root 20 0 0 0 0 S 0.0 0.0 0:00.00 scsi_eh_2
303 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 scsi_tmf_2
305 root -51 0 0 0 0 S 0.0 0.0 0:00.01 irq/16-vm+
306 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ttm_swap
381 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kdmflush
382 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
393 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kdmflush
394 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
407 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
408 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfsalloc
409 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs_mru_c+
410 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-buf/d+
411 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-data/+
412 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-conv/+
413 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-cil/d+
414 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-recla+
415 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-log/d+
416 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-eofbl+
417 root 20 0 0 0 0 S 0.0 0.0 0:01.25 xfsaild/d+
418 root 0 -20 0 0 0 S 0.0 0.0 0:00.01 kworker/0+
502 root 20 0 39292 3880 3380 S 0.0 0.1 0:00.16 systemd-j+
530 root 20 0 190376 1344 952 S 0.0 0.0 0:00.00 lvmetad
542 root 20 0 46284 2612 1336 S 0.0 0.1 0:02.96 systemd-u+
556 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1+
631 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-buf/s+
632 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-data/+
633 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-conv/+
634 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-cil/s+
635 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-recla+
636 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-log/s+
637 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-eofbl+
638 root 20 0 0 0 0 S 0.0 0.0 0:00.00 xfsaild/s+
651 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kdmflush
652 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
661 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-buf/d+
662 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-data/+
663 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-conv/+
664 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-cil/d+
665 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-recla+
666 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-log/d+
667 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs-eofbl+
668 root 20 0 0 0 0 S 0.0 0.0 0:00.00 xfsaild/d+
691 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 rpciod
692 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xprtiod
694 root 16 -4 55532 848 448 S 0.0 0.0 0:00.02 auditd
696 root 12 -8 84556 912 768 S 0.0 0.0 0:00.01 audispd
698 root 16 -4 55620 1408 1068 S 0.0 0.0 0:00.00 sedispatch
722 polkitd 20 0 616704 15072 5140 S 0.0 0.4 0:00.62 polkitd
724 avahi 20 0 62268 2276 1812 S 0.0 0.1 0:00.05 avahi-dae+
730 root 20 0 90568 3200 2336 S 0.0 0.1 0:01.21 rngd
731 root 20 0 26384 1808 1484 S 0.0 0.0 0:00.04 systemd-l+
732 root 20 0 396804 4340 3376 S 0.0 0.1 0:00.07 accounts-+
733 root 20 0 168304 5176 3740 S 0.0 0.1 0:00.01 VGAuthSer+
734 root 20 0 295564 5296 4048 S 0.0 0.1 0:02.50 vmtoolsd
735 root 20 0 430624 5480 4204 S 0.0 0.1 0:00.02 ModemMana+
736 rpc 20 0 69256 1008 468 S 0.0 0.0 0:00.01 rpcbind
741 avahi 20 0 62140 400 0 S 0.0 0.0 0:00.00 avahi-dae+
744 root 20 0 201428 1268 768 S 0.0 0.0 0:00.00 gssproxy
746 dbus 20 0 61520 3380 1880 S 0.0 0.1 0:00.27 dbus-daem+
749 chrony 20 0 117808 1832 1372 S 0.0 0.0 0:00.03 chronyd
811 root 20 0 451268 8492 4932 S 0.0 0.2 0:00.04 udisksd
814 root 20 0 52852 2808 1988 S 0.0 0.1 0:00.01 smartd
815 libstor+ 20 0 8580 828 676 S 0.0 0.0 0:00.06 lsmd
816 root 20 0 228436 5868 3936 S 0.0 0.1 0:00.02 abrtd
818 root 20 0 225916 4900 3420 S 0.0 0.1 0:00.02 abrt-watc+
819 root 20 0 225916 4900 3420 S 0.0 0.1 0:00.01 abrt-watc+
825 root 20 0 21680 1296 992 S 0.0 0.0 0:00.27 irqbalance
826 rtkit 21 1 198784 1784 1416 S 0.0 0.0 0:00.06 rtkit-dae+
828 root 20 0 553000 9476 7140 S 0.0 0.2 0:00.11 NetworkMa+
833 root 20 0 6612 296 136 S 0.0 0.0 0:00.00 mcelog
844 root 20 0 115408 948 692 S 0.0 0.0 0:00.05 ksmtuned
909 root 20 0 102904 5516 3456 S 0.0 0.1 0:00.01 dhclient
1138 root 20 0 198072 4284 3136 S 0.0 0.1 0:00.01 cupsd
1140 root 20 0 574284 19520 6140 S 0.0 0.5 0:00.44 tuned
1145 root 20 0 112900 4360 3332 S 0.0 0.1 0:00.01 sshd
1147 root 20 0 240596 16668 4372 S 0.0 0.4 0:00.27 rsyslogd
1151 root 20 0 1004980 16108 10052 S 0.0 0.4 0:00.29 libvirtd
1158 root 20 0 25908 948 748 S 0.0 0.0 0:00.00 atd
1159 root 20 0 126384 1620 1000 S 0.0 0.0 0:00.01 crond
1160 root 20 0 481556 6728 3676 S 0.0 0.2 0:00.03 gdm
1214 root 20 0 307924 31408 22464 S 0.0 0.8 0:00.37 X
1264 root 20 0 91792 2160 1144 S 0.0 0.1 0:00.02 master
1265 postfix 20 0 91896 4080 3072 S 0.0 0.1 0:00.01 pickup
1266 postfix 20 0 91964 4096 3088 S 0.0 0.1 0:00.01 qmgr
1537 nobody 20 0 53876 1088 704 S 0.0 0.0 0:00.00 dnsmasq
1538 root 20 0 53848 380 0 S 0.0 0.0 0:00.00 dnsmasq
1565 root 20 0 971068 52432 20928 S 0.0 1.3 0:00.62 dockerd
1700 root 20 0 363244 4840 3840 S 0.0 0.1 0:00.02 gdm-sessi+
1732 gdm 20 0 671348 11112 6692 S 0.0 0.3 0:00.20 gnome-ses+
1743 gdm 20 0 59016 960 460 S 0.0 0.0 0:00.00 dbus-laun+
1744 gdm 20 0 60472 1872 1072 S 0.0 0.0 0:00.05 dbus-daem+
1749 gdm 20 0 346780 3700 3040 S 0.0 0.1 0:00.01 at-spi-bu+
1754 gdm 20 0 60064 2300 1804 S 0.0 0.1 0:00.01 dbus-daem+
1756 gdm 20 0 233104 3952 3188 S 0.0 0.1 0:00.01 at-spi2-r+
1781 gdm 20 0 3245776 138600 65248 S 0.0 3.4 0:03.52 gnome-she+
1789 root 20 0 422284 5024 3960 S 0.0 0.1 0:00.01 upowerd
1798 gdm 20 0 1171664 3604 2516 S 0.0 0.1 0:00.01 pulseaudio
1842 gdm 20 0 453320 5484 3464 S 0.0 0.1 0:00.07 ibus-daem+
1845 gdm 20 0 376172 5608 2984 S 0.0 0.1 0:00.01 ibus-dconf
1848 gdm 20 0 465092 13768 8896 S 0.0 0.3 0:00.06 ibus-x11
1854 gdm 20 0 376152 3448 2876 S 0.0 0.1 0:00.01 ibus-port+
1861 gdm 20 0 364920 4940 2408 S 0.0 0.1 0:00.01 xdg-permi+
1871 root 20 0 398900 4132 3348 S 0.0 0.1 0:00.01 boltd
1874 gdm 20 0 615500 15112 9760 S 0.0 0.4 0:00.08 gsd-xsett+
1875 root 20 0 410768 7532 4396 S 0.0 0.2 0:00.05 packageki+
1883 gdm 20 0 376708 3420 2844 S 0.0 0.1 0:00.00 gsd-a11y-+
1885 gdm 20 0 464772 13544 8752 S 0.0 0.3 0:00.07 gsd-clipb+
1886 root 20 0 78660 3360 2584 S 0.0 0.1 0:00.01 wpa_suppl+
1891 gdm 20 0 714372 17080 9612 S 0.0 0.4 0:00.26 gsd-color
1894 gdm 20 0 465660 8004 6016 S 0.0 0.2 0:00.00 gsd-datet+
1898 gdm 20 0 380888 3016 2476 S 0.0 0.1 0:00.01 gsd-house+
1900 gdm 20 0 614520 13728 8900 S 0.0 0.3 0:00.06 gsd-keybo+
1905 gdm 20 0 995948 15768 10292 S 0.0 0.4 0:00.08 gsd-media+
1912 gdm 20 0 300624 4924 2392 S 0.0 0.1 0:00.01 gsd-mouse
1913 gdm 20 0 632036 16868 9616 S 0.0 0.4 0:00.09 gsd-power
1916 gdm 20 0 363352 4556 3512 S 0.0 0.1 0:00.01 gsd-print+
1922 gdm 20 0 317852 3120 2548 S 0.0 0.1 0:00.00 gsd-rfkill
1924 gdm 20 0 374360 3060 2520 S 0.0 0.1 0:00.00 gsd-scree+
1934 gdm 20 0 411716 4336 3468 S 0.0 0.1 0:00.00 gsd-shari+
1939 gdm 20 0 472444 5176 4296 S 0.0 0.1 0:00.03 gsd-smart+
1943 gdm 20 0 455208 4908 3836 S 0.0 0.1 0:00.01 gsd-sound
1946 gdm 20 0 549588 14372 9216 S 0.0 0.4 0:00.07 gsd-wacom
1966 colord 20 0 419700 6328 4748 S 0.0 0.2 0:00.02 colord
1981 gdm 20 0 302356 3396 2832 S 0.0 0.1 0:00.00 ibus-engi+
2026 root 20 0 163596 6128 4732 S 0.0 0.2 0:00.07 sshd
2032 root 20 0 116652 3228 1836 S 0.0 0.1 0:00.05 bash
2310 root 20 0 0 0 0 S 0.0 0.0 0:00.20 kworker/1+
2667 root 20 0 0 0 0 S 0.0 0.0 0:00.03 kworker/u+
2787 root 20 0 0 0 0 S 0.0 0.0 0:00.80 kworker/0+
2866 root 20 0 125484 1144 896 S 0.0 0.0 0:00.00 anacron
3048 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/1+
3102 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kworker/1+
3182 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kworker/u+
3245 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/1+
3261 root 20 0 108052 356 280 S 0.0 0.0 0:00.00 sleep
3291 root 20 0 156776 5628 4320 S 0.0 0.1 0:00.00 sshd
3295 root 20 0 162120 2168 1524 R 0.0 0.1 0:00.00 top
[root@logstash ~]# cat hostname.txt
192.168.10.245:2
[root@logstash ~]#