c - SSH execute commands remotely -
i trying ssh remote server , execute simple command. using libssh library , following code,
ssh_key pkey; ssh_session session; ssh_channel channel; int rc = ssh_pki_import_privkey_file(pc_private_key_file, null, null, null, &pkey); char buffer[1024]; unsigned int nbytes; printf("session...\n"); session = ssh_new(); if (session == null) exit(-1); ssh_options_set(session, ssh_options_host, pc_host); ssh_options_set(session, ssh_options_user, pc_user); ssh_options_set(session, ssh_options_port, &pc_port); ssh_options_set(session, ssh_options_log_verbosity, &pc_verbosity); printf("connecting...\n"); rc = ssh_connect(session); if (rc != ssh_ok) error(session); printf("channel...\n"); channel = ssh_channel_new(session); if (channel == null) exit(-1); printf("opening...\n"); rc = ssh_channel_open_session(channel); if (rc != ssh_ok) error(session); printf("executing remote command...\n"); rc = ssh_channel_request_exec(channel, "ls -l"); if (rc != ssh_ok) error(session); printf("received:\n"); nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); while (nbytes > 0) { fwrite(buffer, 1, nbytes, stdout); nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); }
the ssh connect works successfully, when gets section of code creates channel , executes command these messages:
channel_open: creating channel 43 64000 window , 32768 max packet packet_send2: packet: wrote [len=44,padding=19,comp=24,payload=24] channel_open: sent ssh_msg_channel_open type session channel 43 ssh_socket_unbuffered_write: enabling pollout socket ssh_packet_socket_callback: packet: read type 3 [len=12,padding=6,comp=5,payload=5] ssh_packet_process: dispatching handler packet type 3 ssh_packet_unimplemented: received ssh_msg_unimplemented (sequence number 3)
i did researching , found question on superuser similar issue i'm having, confused top answer of question. also, question on stackoverflow, not have solution problem.
i'm not sure how resolve issue. appreciated.
Comments
Post a Comment