需求描述

  在实际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 % /
tmpfs 939M 939M % /dev/shm
/dev/sda1 477M 32M 420M % /boot
[mysql@redhat6 ~]$

备注:可以看到ssh通过发送命令,返回了远程主机的文件系统使用情况,但是还是在redhat6这个主机,mysql用户的shell下。

2.通过ssh发送多个命令

[mysql@redhat6 ~]$ ssh oracle@standby ls -l /tmp;date
total
drwxr-xr-x. oracle oinstall Nov : CVU_11.2.0..0_oracle
drwxr-xr-x root root Mar : hadoop-root
drwxr-xr-x root root Feb : hbase-root
drwxr-xr-x root root Feb : Jetty_0_0_0_0_16010_master____.6nvknp
drwxr-xr-x root root Feb : Jetty_0_0_0_0_16012_master____4xio8d
drwxr-xr-x root root Feb : Jetty_0_0_0_0_16013_master____.osc8b6
srwxrwxrwx mysql mysql Mar : mysql.sock
-rw-------. root root Nov : yum.log
-rw-------. root root Jan : yum_save_tx-----59DFUlOW.yumtx
-rw-------. root root Jan : yum_save_tx-----003Cg43n.yumtx
Mon Mar :: CST
[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中

文档创建时间:2018年3月12日14:31:47

ssh在本地调用远程主机上的命令,不登录远程主机shell的更多相关文章

  1. ansible 批量在远程主机上执行命令

    ansible 和 saltstack 都是为了同时在多台主机上执行相同的命令, 但是 salt配置麻烦,ansible基本不用配置, ansible 通过ssh来连接并控制被控节点 1. 安装 第一 ...

  2. ssh远程执行目标机器上的命令

    一句话: ssh -t -p 端口号 用户名@远程机器IP '远程机器上的命令完整路径' 例如: ssh -t -p 22 yangjunming@dev '/opt/app/deploy.sh' 注 ...

  3. 通过paramiko模块在远程主机上执行命令

    安装paramiko模块 /usr/local/python36/bin/pip3 install paramiko 1.获取cpu使用率 #!/usr/bin/python #coding=utf8 ...

  4. linux命令行下使用反斜杠“\”调用别名的原命令

    经常看到linux软件的作者在介绍安装软件时,在命令前面加一个反斜杠,不解其意,后来查阅资料才知道,这是为了取消别名的调用,使用原命令. alias是shell的内置命令,可以用来设置命令的别名,如使 ...

  5. shell脚本中判断上一个命令是否执行成功

    shell脚本中判断上一个命令是否执行成功 shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 示例 ...

  6. [shell]上一个命令执行完成,才执行下一个操作 | shell脚本中判断上一个命令是否执行成功

    shell脚本中判断上一个命令是否执行成功  shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 场 ...

  7. Telnet命令检测远程主机上的端口是否开启

    ping命令不能检测端口,只能检测你和相应IP是否能连通. 本地虚拟机里安装了一个Ubuntu,使用Putty连接22端口操作时提示失败,于是查看对应端口是否开启. Windows下要检测远程主机上的 ...

  8. SSH客户端提示 用户密钥未在远程主机上注册

    今天在一台使用已久的内网服务器上面帮一位新同事添加账户,添加完成之后就把账号交付于他,过了10分钟他告诉我说无法登陆,觉得很诧异 这么轻车熟路的 这么会 登陆不上去了,自己也用Xshell 登陆了一下 ...

  9. 配置github的SSH key及GitHub项目上传方式一——使用终端命令行

    GitHub是一个开源的大仓库,我们经常从github上下载项目进行学习和研究,下面是一个完整的步骤——往GitHub上传一个新项目. 一.注册GitHub账号 1.注册GitHub账号,地址:htt ...

随机推荐

  1. C++ function pointer and type cast

    http://www.cprogramming.com/tutorial/function-pointers.html http://www.cplusplus.com/doc/tutorial/ty ...

  2. canvas.drawBitmap(bitmap, src, dst, paint)

    // GameView.drawImage(canvas, mBitDestTop, miDTX, mBitQQ.getHeight(), mBitDestTop.getWidth(), mBitDe ...

  3. 每日英语:Cyclists Live Six Years Longer

    Cycling does the body good. New data from Tour de France cyclists finds that those athletes live an ...

  4. 每日英语:Why Are Items Pricier in China?

    In China, consumers pay nearly $1 more for a latte at Starbucks than their U.S. counterparts. A Cadi ...

  5. ny520 最大素因子 筛选法求素数

    最大素因子时间限制:1000 ms  |  内存限制:65535 KB难度:2 描述 GreyAnts最近正在学习数论中的素数,但是现在他遇到了一个难题:给定一个整数n,要求我们求出n的最大素因子的序 ...

  6. GPIO实验(一)

    目标:点亮LED1.看原理图,找到对应的引脚和寄存器2.a.配置寄存器为输入/出引脚    GPFCON[9:8]=0b01  b.设置输出高/低电平    GPDAT[4]=0b0 1.预处理2.编 ...

  7. LeetCode: Wildcard Matching 解题报告

    Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...

  8. activemq用户手册

    1 JMS 在介绍ActiveMQ之前,首先简要介绍一下JMS规范. 1.1 JMS的基本构件 1.1.1 连接工厂 连接工厂是客户用来创建连接的对象,例如ActiveMQ提供的ActiveMQCon ...

  9. 搭建一个单纯学习hibernate的项目

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  10. nginx 405 not allowed问题的解决

    转载自:  http://www.linuxidc.com/Linux/2012-07/66761.htm Apache.IIS.Nginx等绝大多数web服务器,都不允许静态文件响应POST请求,否 ...