需求描述:
在实际shell脚本的编写过程中,需要通过ssh远程执行一个命令,并返回执行的结果
简单来说,就是将命令发送到远程的主机上进行执行,但是并没有实际的登录到远程主机上。即通过
ssh的方式本地调用远程的命令。
实现方法:
通过下面脚本测试通过ssh发送命令给远程主机,并且返回执行结果:
ssh username@hostname command测试过程:
1.通过ssh发送一个命令
[mysql@redhat6 ~]$ ssh oracle@standby df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_standby-lv_root
37G 20G 15G 58% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/sda1 477M 32M 420M 8% /boot
[mysql@redhat6 ~]$备注:可以看到ssh通过发送命令,返回了远程主机的文件系统使用情况,但是还是在redhat6这个主机,mysql用户的shell下。
2.通过ssh发送多个命令
[mysql@redhat6 ~]$ ssh oracle@standby ls -l /tmp;date
total 32
drwxr-xr-x. 3 oracle oinstall 4096 Nov 30 15:42 CVU_11.2.0.4.0_oracle
drwxr-xr-x 3 root root 4096 Mar 1 14:03 hadoop-root
drwxr-xr-x 4 root root 4096 Feb 28 09:56 hbase-root
drwxr-xr-x 4 root root 4096 Feb 28 18:36 Jetty_0_0_0_0_16010_master____.6nvknp
drwxr-xr-x 4 root root 4096 Feb 28 10:43 Jetty_0_0_0_0_16012_master____4xio8d
drwxr-xr-x 4 root root 4096 Feb 28 10:43 Jetty_0_0_0_0_16013_master____.osc8b6
srwxrwxrwx 1 mysql mysql 0 Mar 12 10:03 mysql.sock
-rw-------. 1 root root 0 Nov 29 10:11 yum.log
-rw-------. 1 root root 1581 Jan 23 14:59 yum_save_tx-2018-01-23-14-59DFUlOW.yumtx
-rw-------. 1 root root 4057 Jan 23 15:00 yum_save_tx-2018-01-23-15-003Cg43n.yumtx
Mon Mar 12 14:14:06 CST 2018
[mysql@redhat6 ~]$备注:如果要发送多个命令,那么各个命令之间通过分号进行分隔。这个就是与在shell下一次执性多个命令一样的,用分号分隔开来。
3.以交互式的方式执性远程命令
通过在ssh命令中指定-t参数,就可以进行交互式的执行远程命令,简单来说,就是执行的命令需要远程主机的shell中的停留等待用户交互,即远程shell之间的交互,
通过使用-t参数,ssh会保持一直登录到远程主机shell,直到退出交互命令。
举例:
未使用-t参数的top命令:
[mysql@redhat6 ~]$ ssh oracle@standby top
TERM environment variable not set使用-t参数的top命令:
[mysql@redhat6 ~]$ ssh -t oracle@standby top执行结果:
top - 14:28:30 up 4:25, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 92 total, 1 running, 91 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.1%us, 0.1%sy, 0.0%ni, 99.5%id, 0.2%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1922424k total, 223924k used, 1698500k free, 21528k buffers
Swap: 3597144k total, 0k used, 3597144k free, 77832k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 19232 1500 1220 S 0.0 0.1 0:00.71 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.01 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 stopper/0
6 root RT 0 0 0 0 S 0.0 0.0 0:00.01 watchdog/0
7 root 20 0 0 0 0 S 0.0 0.0 0:02.94 events/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cgroup
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khelper
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 netns
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 async/mgr
12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 pm
13 root 20 0 0 0 0 S 0.0 0.0 0:00.03 sync_supers
14 root 20 0 0 0 0 S 0.0 0.0 0:00.04 bdi-default
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kintegrityd/0
16 root 20 0 0 0 0 S 0.0 0.0 0:00.14 kblockd/0
17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpid
18 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_notify
19 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_hotplug
20 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ata_aux
21 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ata_sff/0
22 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksuspend_usbd
23 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khubd
24 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kseriod
25 root 20 0 0 0 0 S 0.0 0.0 0:00.00 md/0
26 root 20 0 0 0 0 S 0.0 0.0 0:00.00 md_misc/0
27 root 20 0 0 0 0 S 0.0 0.0 0:00.00 linkwatch
29 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd
30 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kswapd0
31 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd
32 root 39 19 0 0 0 S 0.0 0.0 0:00.04 khugepaged
33 root 20 0 0 0 0 S 0.0 0.0 0:00.00 aio/0
Connection to standby closed. #当通过Ctrl + C退出交互式命令时,就会断了与远程主机的连接。备注:通过以上的测试,使用-t选项,就是在发出命令之后,ssh还会保留在远程的shell上,等待命令执行完成或者说等待用户给出后续的命令,当用户发送
退出交互命令之后,与远程的shell断开,回到本地的shell中
















