• Linux ssh的的用法


    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

  • 相关阅读:
    ACM题目————食物链
    ACM题目————Find them, Catch them
    hdu 1255 覆盖的面积 (线段树处理面积覆盖问题(模板))
    poj 3373 Changing Digits (DFS + 记忆化剪枝+鸽巢原理思想)
    hdu 3303 Harmony Forever (线段树 + 抽屉原理)
    hdu 2665 Kth number(划分树模板)
    poj 1348 Computing (四个数的加减乘除四则运算)
    hdu 1021 Fibonacci Again(找规律)
    HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
    hdu 1560 DNA sequence(搜索)
  • 原文地址:https://www.cnblogs.com/gaoyuanzhi/p/10256557.html
Copyright © 2020-2023  润新知