Linux ssh的的用法

ssh执行远程命令

1. 执行命令

1.执行单条命令

    ubuntu@node1:~$ ssh ubuntu@172.16.10.102 hostname
ubuntu@172.16.10.102's password:
node2

2.执行带参数的命令

    ssh ubuntu@172.16.10.102 grep root /etc/passwd
ubuntu@172.16.10.102's password:
root:x:0:0:root:/root:/bin/bash

3.执行多条命令

执行多条命令时,只要用分号把命令分割开就可以了,但是要把多条命令用引号引起来,否则分号后面的命令就是在本地执行的。

    # 不加分号
ubuntu@node1:~$ ssh ubuntu@172.16.10.102 cat /etc/hostname;hostname -I
node2
172.16.10.101
    # 加分号
ubuntu@node1:~$ ssh ubuntu@172.16.10.102 "cat /etc/hostname;hostname -I"
node2
172.16.10.102

4.执行多行命令

用单引号或双引号开头,然后写上几行命令,最后再用相同的引号来结束。

    ubuntu@node1:~$ ssh   ubuntu@172.16.10.102 "
> hostname -I
> uname -r
> uptime
> "
ubuntu@172.16.10.102's password:
172.16.10.102
4.4.0-31-generic
15:39:13 up 6:28, 2 users, load average: 0.00, 0.00, 0.00

5.执行交互命令

    ubuntu@node1:~$ ssh ubuntu@172.16.10.102 "sudo apt install nginx"
ubuntu@172.16.10.102's password:
sudo: no tty present and no askpass program specified

默认情况下,当你执行不带命令的 ssh 连接时,会为你分配一个 TTY。因为此时你应该是想要运行一个 shell 会话。

但是当你通过 ssh 在远程主机上执行命令时,并不会为这个远程会话分配 TTY。此时 ssh 会立即退出远程主机,所以需要交互的命令也随之结束。

这时需要用 -t 参数显式的告诉 ssh,我们需要一个 TTY 远程 shell 进行交互,添加 -t 参数后,ssh 会保持登录状态,直到你退出需要交互的命令。

    ubuntu@node1:~$ ssh -t  ubuntu@172.16.10.102 "sudo apt install nginx"
ubuntu@172.16.10.102's password:
[sudo] password for ubuntu:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
libjpeg-turbo8 libjpeg8 libtiff5 libvpx1 libxpm4 libxslt1.1 nginx-common
nginx-core
Suggested packages:
libgd-tools fcgiwrap nginx-doc
The following NEW packages will be installed:
fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
libjpeg-turbo8 libjpeg8 libtiff5 libvpx1 libxpm4 libxslt1.1 nginx
nginx-common nginx-core
0 upgraded, 14 newly installed, 0 to remove and 8 not upgraded.
Need to get 2,681 kB of archives.
After this operation, 9,073 kB of additional disk space will be used.
Do you want to continue? [Y/n] n
Abort.
Connection to 172.16.10.102 closed.

6.使用本地变量

    ubuntu@node1:~$ name="nginx"
ubuntu@node1:~$ ssh ubuntu@172.16.10.102 "echo "Install $name""
ubuntu@172.16.10.102's password:
Install nginx

2. ssh执行脚本

1.执行远程不带参数的脚本

    #远程脚本文件内容
ubuntu@node2:~$ cat hello.sh
#!/bin/bash
echo "hello world"
    ubuntu@node1:~$ ssh ubuntu@172.16.10.102 "$HOME/hello.sh"
ubuntu@172.16.10.102's password:
hello world

2.执行远程需要参数的脚本

    # 远程脚本文件内容
ubuntu@node2:~$ cat script.sh
#!/bin/bash
name=$1
echo "install $name"
echo "installed succeed!"
    ubuntu@node1:~$ name=nginx
ubuntu@node1:~$ ssh ubuntu@172.16.10.102 "$HOME/script.sh $name"
ubuntu@172.16.10.102's password:
install nginx
installed succeed!

3.执行本地不带参数的脚本

    ubuntu@node1:~$ cat hello.sh
#!/bin/bash
hostname
touch hello.txt
ls ubuntu@node1:~$ ssh ubuntu@172.16.10.102 < hello.sh
Pseudo-terminal will not be allocated because stdin is not a terminal.
ubuntu@172.16.10.102's password:
node2
hello.txt

4.执行本地需要参数的脚本文件

    # 脚本内容,需要传入一个参数
ubuntu@node1:~$ cat script.sh
#!/bin/bash
name=$1
echo "install $name"
echo "installed succeed!"
# 需要加上bash -s
ubuntu@node1:~$ ssh ubuntu@172.16.10.102 "bash -s" < script.sh nginx
ubuntu@172.16.10.102's password:
install nginx
installed succeed!

参考:https://mp.weixin.qq.com/s?__biz=MzAxNTcyNzAyOQ==&mid=2650960880&idx=2&sn=681aaca23fa2172e8f5a4cb49e580e94&chksm=8009732ab77efa3cc492c0ba71dfddca1909f464cbeb1c1ef866e95a8fd45f160f73493c1e76&mpshare=1&scene=1&srcid=0111Lfv6wByoEKM89PEa51XC&pass_ticket=pruDWttMGaDPNQ0TXs%2Bqm7cF%2FY3jXUHbqfiVaBDbdFoetVyrKuO0W0GJSBAx8Ggg#rd

最新文章

  1. 修复DapperExtension做Insert对象主键为Guid时不能赋值的问题
  2. HTML中添加背景音乐
  3. netcat命令
  4. 第三章 C#循环与方法
  5. 写给程序员和UI--Android的切图标准
  6. JSP基础--JAVA遇见HTML
  7. gradient color
  8. javascript学习-原生javascript的小特效(改变透明度效果)
  9. [__NSCFNumber length]: unrecognized selector sent to instance 0x8b3c310
  10. SQL Server与Oracle对比学习:权限管理(二) 一些有趣的比喻
  11. Zend framework重定向的方法
  12. 监控工具zabbix
  13. [ext4]磁盘布局 - inode bitmap &amp; table
  14. 全站 HTTPS 没你想象的那么简单
  15. FastDFS并发会有bug,其实我也不太信?- 一次并发问题的排查经历
  16. 移动App设计的十条建议
  17. 0 - Dao层(数据访问层设计)
  18. Qt之QComboBox定制(二)
  19. powerdesigner相关记录
  20. 【Codeforces 710F】String Set Queries

热门文章

  1. set unused
  2. BZOJ2588 树上静态第k大
  3. 【串线篇】spring boot全面接管springMvc
  4. JVM加载class文件原理
  5. [LeetCode] 238. 除自身以外数组的乘积 ☆☆☆(左积*右积)
  6. linux运维、架构之路-linux文件权限
  7. C# WinForm 中Label自动换行 解决方法
  8. python-zx笔记9-单元测试
  9. 前端每日实战:6# 视频演示如何用纯 CSS 绘制一颗闪闪发光的璀璨钻石
  10. 洛谷P2661 信息传递(最小环,并查集)