-
Notifications
You must be signed in to change notification settings - Fork 695
Description
I want to fill a function like a ssh command
"ssh -N -o "ServerAliveInterval=15" -o "ServerAliveCountMax=1" -R $port:localhost:22 user@host"
map the ssh port of localhost to remote
So I use the code generate by AI:
function createReverseTunnel(sshPort, remoteHost, remotePort) {
const conn = new Client();
conn.on('ready', () => {
console.log('SSH connection established');
conn.forwardOut(
'0.0.0.0',
sshPort,
remoteHost,
remotePort,
(err, stream) => {
if (err) {
console.error('Forwarding failed:', err);
return;
}
console.log(`Reverse tunnel established: ${remoteHost}:${remotePort} -> localhost:${sshPort}`);
stream.on('close', () => {
console.log('TCP :: CLOSED');
}).on('error', (err)=>{
console.log('TCP :: ERROR', err);
}).on('data', (data) => {
console.log('TCP :: DATA: ' + data);
})
stream.write('GET / HTTP/1.1\r\n' +
'Host: aliyun.com\r\n' +
'Connection: close\r\n' +
'\r\n');
} );
}).on('error', (err) => {
console.error('SSH connection error:', err);
}).on('end', () => {
console.log('SSH connection ended');
}).on('close', (had_error) => {
console.log(SSH connection closed${had_error ? ' due to error' : ''}
);
});
conn.connect({
host: remoteHost,
port: 22,
username: 'root',
password: 'password',
keepaliveInterval: 15000,
keepaliveCountMax: 1,
});
}
createReverseTunnel( 22222, 'aliyun', 80 );
Program run ok,got the response from aliyun:80
But I want to get the same function like "ssh -N -o "ServerAliveInterval=15" -o "ServerAliveCountMax=1" -R $port:localhost:22 user@host", I can not find which port is listening both local and remote.
Who can help me to fullfil the function, thanks