Linux 常用命令
SSH远程执行命令
命令格式
ssh -p $port $user@$p 'cmd'
$port : ssh连接端口号
$user: ssh连接用户名
$ip:ssh连接的ip地址
cmd:远程服务器需要执行的操作
ssh的-t参数
-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
中文翻译一下:
就是可以提供一个远程服务器的虚拟tty终端,加上这个参数我们就可以在远程服务器的虚拟终端上输入自己的提权密码了,非常安全
脚本批量执行远程命令
#!/bin/bash
#变量定义
ip_array=("192.168.1.101" "192.168.1.102" "192.168.1.103" "192.168.1.104")
#远程命令,如删除日志,停止服务,重新启动服务
remote_cmd="cd web/ && find . -name '*.log' -delete && pm2 kill && pm2 start app.json"
#本地通过ssh执行远程服务器的脚本
for ip in ${ip_array[*]}
do
#特定IP对应特定端口的判断
if [ $ip = "192.168.1.101" ]; then
port="2222"
else
port="22"
fi
#特定IP对应特定用户名的判断
if [ $ip = "192.168.1.102" ]; then
user="willin"
else
user="root"
fi
#执行
ssh -t -p $port $user@$ip "$remote_cmd"
done
这个方法还是很方便的,-t虚拟出一个远程服务器的终端,在多台服务器同时部署时确实节约了不少时间啊!
递归删除日志
删除当前目录及子目录的.log
日志
find . -name '*.log' -delete