最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Can we connect UNIX environment which has set to ask for Passcode to Authenticate through SSH or any other way? - Stac

programmeradmin0浏览0评论

I have followed the below procedure.

/

I have created public key and private key.. Pushed public key in UNIX Lower environment server say eg. QA. I am able to connect it..

The same process i am trying for Higher environment like UAT and unless QA environment it does not ask password and it will prompt for PASSCODE. (pushed public key in Bastion and UAT server both)

The process which i am trying is copy one file from my local to UNIX UAT server.

Any other way how to automate this process or is it not automatable through code.. I am trying NodeJs+ Typescript.

static async connectBastionServer(fileName:string) {
    // Configuration for connecting to the Bastion server

const username=process.env.username;
const privateKeyPath = 'C:\\Users\\'+username+'\\.ssh\\id_rsa';

 const bastionConfig = {
     host: 'XX.XXX.XXX.XX', // Bastion server's hostname or IP
     port: 22, // SSH port for Bastion server (default is 22)
     username: 'username', // Username for Bastion server login
     privateKey: fs.readFileSync(privateKeyPath), // Private key for Bastion authentication
 };
 // Configuration for connecting to the UAT server (via the bastion)
 const uatConfig = {
     host: 'XX.XXX.XXX.XX', // UAT server's hostname or IP
     port: 22, // SSH port for UAT server (default is 22)
     username: 'username', // Username for UAT server login
     privateKey: fs.readFileSync(privateKeyPath), // Private key for UAT authentication
 };
 // Local file path to upload
 const cwd = process.cwd();

 const localFilePath =path.join(cwd, fileName);;

 // Remote file path where the file will be uploaded on the UAT server
 const remoteFilePath = 'remotePath...';
 // Function to connect to the UAT server via the Bastion server and upload a file
 const connectThroughBastion = async () => {
     const bastionClient = new Client(); // Create an SSH client instance for the bastion server
     // Wrap the entire logic in a Promise for better async handling
     return new Promise<void>((resolve, reject) => {
         // Event: When the bastion client connects successfully
         bastionClient.on('ready', () => {
             console.log('Connected to Bastion server.');
             // Establish a tunnel (forwarding) from the bastion to the UAT server
             bastionClient.forwardOut(
                 '127.0.0.1', // Local address (dummy source, usually localhost)
                 0, // Source port (0 means the OS chooses an available ephemeral port)
                 uatConfig.host, // Destination: UAT server's hostname or IP
                 uatConfig.port, // Destination: UAT server's SSH port (default is 22)
                 async (err, stream) => {
                     if (err) {
                         bastionClient.end(); // End bastion connection on error
                         return reject(`Error establishing tunnel: ${err.message}`);
                     }
                     console.log('Tunnel to UAT server established.');
                     // Create an instance of the SFTP client
                     const sftp = new Client();
                     try {
                         // Connect to the UAT server using the forwarded stream
                         await sftp.connect({
                             sock: stream, // Use the tunnel stream created by forwardOut
                             username: uatConfig.username, // UAT server username
                             privateKey: uatConfig.privateKey, // UAT server private key
                         });
                         console.log('Connected to UAT server via tunnel.');
                         // Upload the local file to the UAT server
                         await sftp.put(localFilePath, remoteFilePath);
                         console.log(`File uploaded successfully to ${remoteFilePath} on UAT server.`);
                         // End the SFTP connection
                         await sftp.end();
                         // End the bastion connection
                         bastionClient.end();
                         // Resolve the promise, indicating success
                         resolve();
                     } catch (error) {
                         // Handle any SFTP or upload errors
                         sftp.end(); // Ensure the SFTP connection is closed
                         bastionClient.end(); // Close the bastion connection
                         reject(`SFTP error: ${error.message}`);
                     }
                 }
             );
         });
         // Event: If the bastion connection encounters an error
         bastionClient.on('error', (err) => {
             reject(`SSH error: ${err.message}`);
         });
         // Start the connection to the Bastion server
         bastionClient.connect(bastionConfig);
     });
 };

 // Execute the function
connectThroughBastion()
  .then(() => console.log('File transfer completed successfully.')) // Success message
  .catch((err) => console.error('Error:', err)); // Error handling

}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论