简单命令执行SSH远程执行命令的格式为:
ssh [用户名]@[远程主机名或 IP] [命令或脚本]
比如我们要获取远程主机运行信息,可以使用uptime:
ssh chongchong uptime
结果:
11:23:55 up 28 days, 23:41, 0 users, load average: 2.46, 1.16, 0.49
比如我们需要获取远程主机的磁盘情况,可以使用
ssh chongchong df -h
文章插图
执行多条命令其他命令也类似,如果命令较长或者涉及多个命令需要把命令部分用引号括住:
比如要获取主机情况和主机磁盘情况:
ssh chongchong "uptime && df -h"
文章插图
或者使用:
ssh chongchong "uptime ; df -h"
远程抓包设想一个更复杂的例子,我们需要对远程主机用抓包,然后再本地用Wireshark分析:
ssh root@someserver 'tcpdump -c 1000 -nn -w - not port 1234' | wireshark -k -i -
当命令行下需要使用tshark的操作 。
ssh root@someserver 'tcpdump -c 1000 -nn -w - not port 1234' | wireshark -i -
结果:
…
3.759005 112.215.162.105 -> 112.252.251.70 TCP 78 [TCP Dup ACK 840#2] outlaws > 65522 [ACK] Seq=49 Ack=90193 Win=126 Len=0 TSval=2402288138 TSecr=2506305501 SLE=91561 SRE=94297
3.759022 112.252.251.70 -> 112.215.162.105 TCP 1434 65522 > outlaws [ACK] Seq=95665 Ack=49 Win=309 Len=1368 TSval=2506305583 TSecr=2402288138
3.761937 fe80::6d94:f636:7715:26dc -> ff02::1:2 DHCPv6 150 Solicit XID: 0x58568d CID: 000100011dd4af3b00155d6d3404
3.762006 Cisco_d3:c7:bf -> Broadcast ARP 60 Who has 103.218.185.171? Tell 103.218.185.1
3.762374 121.50.168.101 -> 121.50.168.255 NBNS 92 Name query NB XENNO.INFO<00>
3.768515 169.254.2.31 -> 169.254.255.255 NBNS 92 Name query NB FSIGNS.DUBA.NET<00>
3.780159 Cisco_d3:c7:bf -> Broadcast ARP 60 Who has 202.74.234.238? Tell 202.74.234.1
3.796134 Cisco_d3:c7:bf -> Broadcast ARP 60 Who has 112.252.251.93? Tell 112.252.251.1
3.801334 112.215.162.105 -> 112.252.251.70 TCP 78 [TCP Dup ACK 840#3] outlaws > 65522 [ACK] Seq=49 Ack=90193 Win=126 Len=0 TSval=2402288179 TSecr=2506305501 SLE=91561 SRE=95665
3.801394 112.252.251.70 -> 112.215.162.105 TCP 1434 [TCP Fast Retransmission] 65522 > outlaws [ACK] Seq=90193 Ack=49 Win=309 Len=1368 TSval=2506305625 TSecr=2402288179
3.804767 185.216.140.36 -> 157.119.69.59 TCP 60 51426 > 52622 [SYN] Seq=0 Win=1024 Len=0
3.806149 Cisco_d3:c7:bf -> Broadcast ARP 60 Who has 103.248.220.121? Tell 103.248.220.1
3.807153 Cisco_d3:c7:bf -> Broadcast ARP 60 Who has 103.248.221.171? Tell 103.248.221.1
3.809116 fe80::a563:2c5c:97df:13ca -> ff02::1:2 DHCPv6 148 Solicit XID: 0x5c67c7 CID: 000100011dd4af3b00155d6d3404
3.811726 Cisco_d3:c7:bf -> Broadcast ARP 60 Who has 202.74.232.203? Tell 202.74.232.1
3.812418 Cisco_d3:c7:bf -> Broadcast ARP 60 Who has 157.119.71.127? Tell 157.119.71.1
3.819393 fe80::910c:1871:e52f:9b82 -> ff02::1:2 DHCPv6 152 Solicit XID: 0x8a263a CID: 00010001205a760c00155df47d05
3.838355 Cisco_d3:c7:bf -> Broadcast ARP 60 Who has 202.74.234.147? Tell 202.74.234.1
3.840331 45.249.181.172 -> 239.255.255.250 SSDP 216 M-SEARCH * HTTP/1.1
3.840570 58.215.162.105 -> 142.252.251.70 TCP 66 outlaws > 65522 [ACK] Seq=49 Ack=95665 Win=117 Len=0 TSval=2402288220 TSecr=2506305625
3.840608 142.252.251.70 -> 58.215.162.105 TCP 1434 65522 > outlaws [ACK] Seq=97033 Ack=49 Win=309 Len=1368 TSval=2506305664 TSecr=2402288220
…
执行脚本我们先写一个简单脚本获取远程主机一些信息,包括主机名、uptime,内存,磁盘和内核信息:
#!/bin/bash
echo "---------主机名--------------------------------------------"
hostnamectl
echo -e "n"
echo "---------系统信息------------------------------------------"
uptime
echo -e "n"
echo "---------可用内存------------------------------------------"
free -m
echo -e "n"
echo "---------磁盘信息------------------------------------------"
df -h
echo -e "n"
echo "---------内核版本-------------------------------------------"
uname -a
echo -e "n"
echo "------------------------------------------------------------"
文章插图
然后使用下面语句执行:
ssh chongchong 'bash -s' < host-info.sh
文章插图
也可以使用管道形式,但是会有提示信息:
cat host-info.sh | ssh chongchong
- 腾讯代理都救不过来?全球收入第一网游,收入百亿国内却没人玩?
- 【爱历史】1980年村民意外挖到石椁,专家激动地赶到现场,开棺后却被吓一跳
- 【游戏世界】JDG止步四强Uzi却热度飙升!网友喊话JDG管理层!
- 家长们注意啦,自家宝贝出现腹泻情况需重视,这些应对措施很有效
- 【十二星座】“说话最伤人”的3种生肖女,说话不经大脑,但却很爱丈夫
- 腐团儿晒长腿晒豪车,月收入五十万,颜值却输给了LOL手游魅儿?
- 【游戏世界】没摇号却中签?《最终幻想14》房屋摇号乌龙 SE:调查中
- 【游戏迷】369打进TES内部?全队开启克隆模式,却多了一个“奇怪”的人
- 【星座控】颜值超高,却超级难追的4个星座女,素颜女神,倾国倾城!
- 【星座控】这三大生肖女虽然长得不好,但却十分旺夫