ssh连接服务器后执行多条命令

参考网址:

http://mp.weixin.qq.com/s?__biz=Mzg4NDA1MTAyOQ==&mid=2247486040&idx=1&sn=19c3d0169cbc066f628f42f36510b03b&chksm=cfbf5762f8c8de743ac21c61726759dfeda7254f47a07efea73cd1e459a38b0dbbe5dc84856b&mpshare=1&scene=24&srcid=1010oJrh1GgqZ1SLbeeuavbV&sharer_sharetime=1665380161162&sharer_shareid=a139fd462e16cb08ad1dc90045048a03#rd

1 使用分号隔开

使用 分号 ;来隔开命令

  • 附带1条命令

    ssh User@Host 'source /etc/profile'
    
  • 附带多条命令

    ssh User@Host 'source /etc/profile ; uptime'
    

2 使用管道符号隔开

使用管道|来隔开命令

  • 附带1条命令

    ssh User@Host 'source /etc/profile'
    
  • 附带多条命令

    ssh User@Host 'source /etc/profile | uptime'
    

3 使用写EOF的方式

同样适用于一条 / 多条命令

ssh User@Host << EOF
> ls -al
> source /etc/profile
> EOF

4 使用脚本的方式

使用脚本的方式花样就更多了,例如有一个脚本myinit.sh/home/admin/code/ 下面

myinit.sh
#!/bin/bash

source /etc/profile
ls -al

远程连接服务器

ssh User@Host 'bash -s' < /home/admin/code/myinit.sh
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值