文件自动同步服务器

index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env node

const shell = require('shelljs')
const chokidar = require('chokidar')
const home = require('user-home')
const path = require('path')

const WORKSPACE_PATH = path.resolve(home, 'Workspace');

const expectPath = path.join(__dirname, './expect.exp')
const to = 'root@144.xxx.xxx.92:/home/backup'

chokidar
.watch('./index.js', {
ignored: (path) => path.includes('node_modules') || path.includes('.git')
})
.on('all', (event, path) => {
console.log(event, path);
shell.exec(`expect ${expectPath} ${path} ${to}`)
});

expect.exp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#expect 不能写错
#!/usr/bin/expect

set from [lindex $argv 0]
set to [lindex $argv 1]

set timeout 30

spawn bash -c "scp $from $to"

# 特殊符号需要转移 回车符号不需要
# \ 需转义为 \\\
# } 需转义为 \}
# [ 需转义为 \[
# $ 需转义为 \\\$
# ` 需转义为 \`
# " 需转义为 \\\"
# ( 需转义为 \(


expect {
"*password:" { send -- "j\(S7?xx#ky6\[0n=R\r" }
}

# 必须要加这一句,在执行完毕后把持交互状态,把控制台,这时候就可以进行你想要进行的操作了。如果没有这一句,在登陆完成之后就会退出,而不是留在远程终端上。
interact
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2025 SunZhiqi

此时无声胜有声!

支付宝
微信