V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
Alain1995
V2EX  ›  Linux

UNIX 菜鸟请教 V 友, linux A 脚本 ssh 另外一台机器执行 A 机器的一个脚本

  •  
  •   Alain1995 · 2016-11-22 13:00:36 +08:00 · 2868 次点击
    这是一个创建于 2712 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最终目的

    • 脚本中获取服务器中一些状态,和开启某些服务
    • 状态生成 log 文件

    问题:

    • linux A 脚本无密码 ssh 另外一台机器执行 A 机器的一个脚本
    • 搜过使用 Expect 完成,有 linux 中缺省的工具或者命令吗?

    有两个想法

    • 在 Linux A 机器上通过命令的方式执行, log 生成在 Linux A 上,和 Linux B 不相干
    • 在 Linux A 上把文件传到 LinuxB 某一个目录下然后再执行生成 log 文件,再回传回来

    还有一点点小疑问

    更改完系统语言之后能做到不重启不注销就能显示更改之后的效果吗?

    跪谢= =

    10 条回复    2016-11-22 21:06:59 +08:00
    TheCure
        1
    TheCure  
       2016-11-22 13:06:19 +08:00
    ansible test -m script -a "test.sh"
    knightdf
        2
    knightdf  
       2016-11-22 13:06:27 +08:00
    在脚本中 echo/print 出来就行了
    例子:
    > ssh [email protected] "ping v2ex.com" >> test 2>&1
    > cat test
    > PING v2ex.com (14.152.44.135) 56(84) bytes of data.
    64 bytes from 14.152.44.135: icmp_seq=1 ttl=52 time=21.3 ms
    64 bytes from 14.152.44.135: icmp_seq=2 ttl=52 time=21.3 ms
    64 bytes from 14.152.44.135: icmp_seq=3 ttl=52 time=21.2 ms
    64 bytes from 14.152.44.135: icmp_seq=4 ttl=52 time=21.4 ms
    64 bytes from 14.152.44.135: icmp_seq=5 ttl=52 time=21.2 ms
    64 bytes from 14.152.44.135: icmp_seq=6 ttl=52 time=27.7 ms
    64 bytes from 14.152.44.135: icmp_seq=7 ttl=52 time=20.3 ms
    64 bytes from 14.152.44.135: icmp_seq=8 ttl=52 time=20.5 ms
    64 bytes from 14.152.44.135: icmp_seq=9 ttl=52 time=20.4 ms
    64 bytes from 14.152.44.135: icmp_seq=10 ttl=52 time=20.2 ms

    --- v2ex.com ping statistics ---
    10 packets transmitted, 10 received, 0% packet loss, time 9030ms
    rtt min/avg/max/mdev = 20.279/21.586/27.712/2.086 ms
    Alain1995
        3
    Alain1995  
    OP
       2016-11-22 13:12:07 +08:00
    @callofmx 想要那种缺省的命令及工具。= =
    Alain1995
        4
    Alain1995  
    OP
       2016-11-22 13:12:42 +08:00
    @knightdf 谢谢,大概知道了。我去试试 0.0
    ceyes
        5
    ceyes  
       2016-11-22 13:56:14 +08:00   ❤️ 1
    tips
    1. 可以用 ssh-copy-id 将本机的公钥拷贝到远程机器的 authorized_keys 文件中,实现无密码登录。可以在部署的时候就把公钥都装好,或用 tcl/expect 先完成公钥拷贝。
    2. ssh LinuxB 后面可以直接跟命令,远端的执行的结果就在 A 的标准输出上, ssh 的返回值就是远端的命令返回值。
    3. 还可以设置一个 NFS ,在 LinuxB 上 mount 作为 worksace ,执行脚本和 log 都在这个 workspace 中。
    Alain1995
        6
    Alain1995  
    OP
       2016-11-22 14:57:07 +08:00
    @ceyes 还有一点小问题,就是判断被执行端的 linux B 是否已经存在当前 linux A 的公钥?
    ceyes
        7
    ceyes  
       2016-11-22 15:26:50 +08:00
    @Alain1995 ssh <user>@<IP_of_LinuxB> exit && echo "have key" || echo "no key"
    gihnius
        8
    gihnius  
       2016-11-22 20:50:11 +08:00
    linux_A $ ssh you@linux_B 'bash -s' < your_local_script_on_A.sh sh-args...
    Alain1995
        9
    Alain1995  
    OP
       2016-11-22 21:06:37 +08:00
    @ceyes 谢谢
    Alain1995
        10
    Alain1995  
    OP
       2016-11-22 21:06:59 +08:00
    @gihnius 谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4062 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:24 · PVG 13:24 · LAX 22:24 · JFK 01:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.