我试图传输编译后的可执行文件(编译后的代码c++,打印“Hello!”)使用库libssh(我不能使用openssh的终端scp )来操作linux服务器。我知道,libssh有一个scp函数,可以将文件从远程ssh服务器传输到远程ssh服务器。然而,也有一些错误。这是scp的代码:
void ext(const char*c){
std::cout<<c;
exit(-5);
}
int scp_do(ssh_session session);
int scp_copy(ssh_session session, ssh_scp scp);
int main(){
int p = 22;
ssh_session session = ssh_new();
if (session == NULL)
exit(-1);
ssh_options_set(session, SSH_OPTIONS_HOST, "192.168.0.106");
ssh_options_set(session, SSH_OPTIONS_PORT, &p);
ssh_options_set(session, SSH_OPTIONS_USER, "user");
int rc = ssh_connect(session);
if (rc != SSH_OK){ //if not connected - exit
ssh_disconnect(session);
ext("Error connecting");
}
rc = ssh_userauth_password(session, NULL, "passwd");
if(rc != SSH_AUTH_SUCCESS){ //if not authed - exit
ssh_disconnect(session);
ssh_free(session);
return 0;
}
scp_do(session); //copying file, main task of this code
ssh_disconnect(session);
ssh_free(session);
return 0 ;
}
int scp_do(ssh_session session){ //copying file, main task of this code
ssh_scp scp;
int rc;
scp = ssh_scp_new(session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, ".");
if (scp == NULL)
ext("Error allocating scp session");
rc = ssh_scp_init(scp);
if (rc != SSH_OK)
ext("Error initializing scp session");
char buffer[1024];
std::ifstream fin("hello"); // compiled executable file('hello world'), that I want to sent to server
rc = ssh_scp_push_file(scp, "hello", 1024, S_IRUSR | S_IWUSR); //creating server's file to copy there
if (rc != SSH_OK)
ext("Can't open remote file");
fin.get(buffer, 1024); //reading from client's file
rc = ssh_scp_write(scp, buffer, 1024); //copying all compiled code to server file
if (rc != SSH_OK)
ext("Can't write to remote file");
ssh_scp_close(scp);
ssh_scp_free(scp);
return SSH_OK;
}当我试图在服务器上执行一个传输的文件时,首先,我发现它是不可执行的。所以我正在为文件做chmod +x。其次,当我执行文件时,我会收到错误:
然而,当我使用openssh的scp传输文件时,没有错误,而且它是可执行的。我能做些什么来解决我的问题?
发布于 2022-05-08 12:13:27
文件的大小类似于
4 62 17168 hello我以为它会少1024字节,bcs --这只是个你好的世界)。所以,我只需要发送17168字节。
https://stackoverflow.com/questions/72160610
复制相似问题