shell远程执行命令

1、先要配置免密登陆,查看上一篇免密传输内容

2、命令行执行少量命令:ssh ip "command1;command2"。例:ssh 172.1.1.1 "cd /home;ls"

3、脚本批量执行命令:

  #!/bin/bash

  ssh ip <<  remotessh

       cd /home

  ls

  remotessh

  注:第一条命令和结束命令需顶格写,不然貌似要报错,实践得出。。。。

  例:

  #/bin/bash

  ssh ip <

  cd /home

  pid=\$(ps -ef | grep tomcat | grep -v grep | awk {print\$2})

  kill -9 \$pid

  remotessh

  注:$字符需要用\转义,不然识别不了,实践得出。。。。。

4、远程运行时,可能会出现找不到命令,比如说jar命令:

  原因:

  /etc/profile: 当用户登录时,该文件被执行. 
  /etc/bashrc: 当bash shell被打开时,该文件被执行.

  ssh作为non-login方式进入,当然就无法触发/etc/profile的执行了。 
  所以应该设置到/etc/bashrc里面去. 

转载于:https://www.cnblogs.com/heiboy/p/11288479.html

你可能感兴趣的:(shell远程执行命令)