Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

child_process.spawn 无法交互式执行命令ssh-keygen、ssh-copy-id

$
0
0

期望

通过node执行ssh-keygen无需交互式输入密钥,因为要做自动化,密码是事先知道的,无需再次通过命令行输入。 不要通过参数-N进行输入密码,因为不是每个命令都会有-N这个参数,我想知道针对ssh-keygen和ssh-copy-id这样的命令如何进行交互式输入。

问题

通过childprocess.spawn 执行ssh-keygen和ssh-copy-id命令时,每次等到输入密码时,stdout就监听不到数据了。 ssh-keygen命令好像在执行密码的时候,不是在子进程进行输出的。

测试代码

const {
	spawn
} = require('child_process');
const child = spawn('ssh-keygen');

child.stdout.on('data', (data) => {
	let content = data.toString();
	console.log('child.stdout: \r\n'+ data.toString())
	console.log('wirte begin:\r\n')
	if (content.indexOf('Enter file') >= 0) {
		child.stdin.write("id_rsd\r\n");
	} else {
		child.stdin.write("empty\r\n");
	}
	console.log('write end \r\n');
});

打印内容

root@iZ1193ih9wgZ:~/test/nssh# node shell.js
child.stdout:
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
wirte begin:

Enter passphrase (empty for no passphrase): write end

Viewing all articles
Browse latest Browse all 14821

Trending Articles