zoukankan      html  css  js  c++  java
  • ssh远程执行命令并自动退出(已测试通过)

    转自:http://blog.csdn.net/fdipzone/article/details/23000201

    ssh命令格式如下:

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]  
    2.            [-D [bind_address:]port] [-e escape_char] [-F configfile]  
    3.            [-I pkcs11] [-i identity_file]  
    4.            [-L [bind_address:]port:host:hostport]  
    5.            [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]  
    6.            [-R [bind_address:]port:host:hostport] [-S ctl_path]  
    7.            [-W host:port] [-w local_tun[:remote_tun]]  
    8.            [user@]hostname [command]  


    主要参数说明:

    -l 指定登入用户

    -p 设置端口号

    -f 后台运行,并推荐加上 -n 参数

    -n 将标准输入重定向到 /dev/null,防止读取标准输入

    -N 不执行远程命令,只做端口转发

    -q 安静模式,忽略一切对话和错误提示

    -T 禁用伪终端配置

    ssh 执行远程命令格式:

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. ssh [options][remote host][command]  


    假设远程服务器IP是192.168.110.34

    例:查看远程服务器的cpu信息

    ssh -l www-online 192.168.110.34 "cat /proc/cpuinfo"

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. www-online@onlinedev01:~$ ssh -l www-online 192.168.110.34 "cat /proc/cpuinfo"  
    2. www-online@192.168.110.34's password:  
    3. processor       : 0  
    4. vendor_id       : GenuineIntel  
    5. cpu family      : 6  
    6. model           : 26  
    7. model name      : Intel(R) Xeon(R) CPU           E5506  @ 2.13GHz  
    8. stepping        : 5  
    9. cpu MHz         : 2128.000  
    10. cache size      : 4096 KB  
    11. fpu             : yes  
    12. fpu_exception   : yes  
    13. cpuid level     : 11  
    14. wp              : yes  
    15. flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology tsc_reliable nonstop_tsc aperfmperf pni ssse3 cx16 sse4_1 sse4_2 popcnt hypervisor lahf_lm  
    16. bogomips        : 4256.00  
    17. clflush size    : 64  
    18. cache_alignment : 64  
    19. address sizes   : 40 bits physical, 48 bits virtual  
    20. power management:  
    21.   
    22. processor       : 1  
    23. vendor_id       : GenuineIntel  
    24. cpu family      : 6  
    25. model           : 26  
    26. model name      : Intel(R) Xeon(R) CPU           E5506  @ 2.13GHz  
    27. stepping        : 5  
    28. cpu MHz         : 2128.000  
    29. cache size      : 4096 KB  
    30. fpu             : yes  
    31. fpu_exception   : yes  
    32. cpuid level     : 11  
    33. wp              : yes  
    34. flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology tsc_reliable nonstop_tsc aperfmperf pni ssse3 cx16 sse4_1 sse4_2 popcnt hypervisor lahf_lm  
    35. bogomips        : 4260.80  
    36. clflush size    : 64  
    37. cache_alignment : 64  
    38. address sizes   : 40 bits physical, 48 bits virtual  
    39. power management:  


    例:执行远程服务器的sh文件

    首先在远程服务器的/home/www-online/下创建一个uptimelog.sh脚本

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #!/bin/bash  
    2.   
    3. uptime >> 'uptime.log'  
    4.   
    5. exit 0  

    使用chmod增加可执行权限

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. chmod u+x uptimelog.sh  

    在本地调用远程的uptimelog.sh

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. ssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh"  

    执行完成后,在远程服务器的/home/www-online/中会看到uptime.log文件,显示uptime内容

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. www-online@nmgwww34:~$ tail -f uptime.log  
    2. 21:07:34 up 288 days,  8:07,  1 user,  load average: 0.05, 0.19, 0.31  


    例:执行远程后台运行sh

    首先把uptimelog.sh修改一下,修改成循环执行的命令。作用是每一秒把uptime写入uptime.log

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #!/bin/bash  
    2.   
    3. while :  
    4. do  
    5.   uptime >> 'uptime.log'  
    6.   sleep 1  
    7. done  
    8.   
    9. exit 0  

    我们需要这个sh在远程服务器以后台方式运行,命令如下:

    ssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh &"

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. www-online@onlinedev01:~$ ssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh &"  
    2. www-online@192.168.110.34's password:  

    输入密码后,发现一直停住了,而在远程服务器可以看到,程序已经以后台方式运行了。

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. www-online@nmgwww34:~$ ps aux|grep uptimelog.sh  
    2. 1007     20791  0.0  0.0  10720  1432 ?        S    21:25   0:00 /bin/bash /home/www-online/uptimelog.sh  

    原因是因为uptimelog.sh一直在运行,并没有任何返回,因此调用方一直处于等待状态。

    我们先kill掉远程服务器的uptimelog.sh进程,然后对应此问题进行解决。

    ssh 调用远程命令后不能自动退出解决方法

    可以将标准输出与标准错误输出重定向到/dev/null,这样就不会一直处于等待状态。

    ssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh > /dev/null 2>&1 &"

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. www-online@onlinedev01:~$ ssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh > /dev/null 2>&1 &"  
    2. www-online@192.168.110.34's password:  
    3. www-online@onlinedev01:~$  

    但这个ssh进程会一直运行在后台,浪费资源,因此我们需要自动清理这些进程。

    实际上,想ssh退出,我们可以在ssh执行完成后kill掉ssh这个进程来实现。

    首先,创建一个sh执行ssh的命令,这里需要用到ssh的 -f 与 -n 参数,因为我们需要ssh也以后台方式运行,这样才可以获取到进程号进行kill操作。

    创建ssh_uptimelog.sh,脚本如下

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #!/bin/bash  
    2.   
    3. ssh -f -n -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh &" # 后台运行ssh  
    4.   
    5. pid=$(ps aux | grep "ssh -f -n -l www-online 192.168.110.34 /home/www-online/uptimelog.sh" | awk '{print $2}' | sort -n | head -n 1) # 获取进程号  
    6.   
    7. echo "ssh command is running, pid:${pid}"  
    8.   
    9. sleep 3 && kill ${pid} && echo "ssh command is complete" # 延迟3秒后执行kill命令,关闭ssh进程,延迟时间可以根据调用的命令不同调整  
    10.   
    11. exit 0  

    可以看到,3秒后会自动退出

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. www-online@onlinedev01:~$ ./ssh_uptimelog.sh  
    2. www-online@192.168.110.34's password:  
    3. ssh command is running, pid:10141  
    4. ssh command is complete  
    5. www-online@onlinedev01:~$  

    然后查看远程服务器,可以见到uptimelog.sh 在后台正常执行。

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. www-online@nmgwww34:~$ ps aux|grep uptime  
    2. 1007     28061  0.1  0.0  10720  1432 ?        S    22:05   0:00 /bin/bash /home/www-online/uptimelog.sh  

    查看uptime.log,每秒都有uptime数据写入。

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. www-online@nmgwww34:~$ tail -f uptime.log  
    2. 22:05:44 up 288 days,  9:05,  1 user,  load average: 0.01, 0.03, 0.08  
    3. 22:05:45 up 288 days,  9:05,  1 user,  load average: 0.01, 0.03, 0.08  
    4. 22:05:46 up 288 days,  9:05,  1 user,  load average: 0.01, 0.03, 0.08  
    5. 22:05:47 up 288 days,  9:05,  1 user,  load average: 0.01, 0.03, 0.08  
    6. 22:05:48 up 288 days,  9:05,  1 user,  load average: 0.01, 0.03, 0.08  
  • 相关阅读:
    Codeforces Gym 100571A A. Cursed Query 离线
    codeforces Gym 100500 J. Bye Bye Russia
    codeforces Gym 100500H H. ICPC Quest 水题
    codeforces Gym 100500H A. Potion of Immortality 简单DP
    Codeforces Gym 100500F Problem F. Door Lock 二分
    codeforces Gym 100500C D.Hall of Fame 排序
    spring data jpa 创建方法名进行简单查询
    Spring集成JPA提示Not an managed type
    hibernate配置文件中的catalog属性
    SonarLint插件的安装与使用
  • 原文地址:https://www.cnblogs.com/onmyway20xx/p/4238063.html
Copyright © 2011-2022 走看看