#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <libssh/libssh.h>
int main() {
ssh_session my_ssh_session = ssh_new();
if (my_ssh_session == NULL) {
fprintf(stderr, "Error creating SSH session: %s", ssh_get_error(my_ssh_session));
exit(EXIT_FAILURE);
}
// 设置SSH会话选项
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "192.168.109.129");
ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "root");
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
// 连接到远程服务器
int rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK) {
fprintf(stderr, "Error connecting to remote host: %s", ssh_get_error(my_ssh_session));
ssh_free(my_ssh_session);
exit(EXIT_FAILURE);
}
// 验证服务器的公钥
rc = ssh_is_server_known(my_ssh_session);
if (rc == SSH_SERVER_KNOWN_OK) {
printf("Server is known and trusted.");
} else if (rc == SSH_SERVER_KNOWN_CHANGED) {
printf("Server key changed.");
// 这里可以添加处理服务器密钥更改的逻辑
} else {
printf("Server is not known.");
// 这里可以添加处理未知服务器的逻辑
}
// 进行身份验证
rc = ssh_userauth_password(my_ssh_session, NULL, "11185730waq");
if (rc != SSH_AUTH_SUCCESS) {
fprintf(stderr, "Authentication failed: %s", ssh_get_error(my_ssh_session));
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(EXIT_FAILURE);
}
// 执行远程命令
ssh_channel channel = ssh_channel_new(my_ssh_session);
if (channel == NULL) {
fprintf(stderr, "Error creating SSH channel: %s", ssh_get_error(my_ssh_session));
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(EXIT_FAILURE);
}
rc = ssh_channel_open_session(channel);
if (rc != SSH_OK) {
fprintf(stderr, "Error opening SSH channel: %s", ssh_get_error(my_ssh_session));
ssh_channel_free(channel);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(EXIT_FAILURE);
}
rc = ssh_channel_request_exec(channel, "your_remote_command");
if (rc != SSH_OK) {
fprintf(stderr, "Error executing remote command: %s", ssh_get_error(my_ssh_session));
ssh_channel_close(channel);
ssh_channel_free(channel);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(EXIT_FAILURE);
}
// 读取远程命令的输出
char buffer[256];
int nbytes;
while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) {
fwrite(buffer, 1, nbytes, stdout);
}
// 关闭通道和会话
ssh_channel_send_eof(channel);
ssh_channel_close(channel);
ssh_channel_free(channel);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return 0;
}
// #include <ssh/libssh.h>
// int main(int argc, char **argv) {
// ssh_session my_ssh_session = ssh_new();
// if (my_ssh_session == NULL) {
// printf("Failed to create new SSH session\n");
// return 1;
// }
// int rc = ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "192.168.109.129");
// if (rc != SSH_OK) {
// printf("Error setting remote host\n");
// ssh_free(my_ssh_session);
// return 1;
// }
// rc = ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "root");
// if (rc != SSH_OK) {
// printf("Error setting username\n");
// ssh_free(my_ssh_session);
// return 1;
// }
// // Optional: Set port number if it's different from the default (22)
// // rc = ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
// rc = ssh_connect(my_ssh_session);
// if (rc != SSH_OK) {
// printf("Error connecting to SSH session\n");
// ssh_free(my_ssh_session);
// return 1;
// }
// // Authenticate with password or public key
// rc = ssh_userauth_password(my_ssh_session, NULL, "11185730waq");
// if (rc != SSH_AUTH_SUCCESS) {
// printf("Error authenticating with password\n");
// ssh_disconnect(my_ssh_session);
// ssh_free(my_ssh_session);
// return 1;
// }
// printf("SSH connection established\n");
// // Execute commands or do other work with the session
// ssh_disconnect(my_ssh_session);
// ssh_free(my_ssh_session);
// return 0;
// }
// #include<stdio.h>
// #include<stdlib.h>
// #include<conio.h>
// #include<string.h>
// int main(){
// char str[20];
// char ip[16],c;
// int temp=0;
// char a[5],b[5];
// strcpy(b,"yes");
// while(1){
// printf("是否进入系统yes or no\n");//如果输入1
// scanf("%s",&a);
// //判断用户选择
// if((strcmp(b,a))==0){
// printf("请输入IP\n");
// while(1){
// c=_getch();//实时获取键盘输入的值
// if(c=='\r')
// break;//获取回车直接跳出循环
// else{
// ip[temp]=c;//输入的IP储存
// printf("%c",c);
// }
// temp++;//IP长度
// }
// printf("\n%s",ip);
// printf("\n服务连接中....\n");
// sprintf(str,"ssh root@%s",ip);//将IP和命令连接成一个字符串
// if((system(str))==0){
// system(str);
// }
// else{
// printf("请重新输入IP\n");
// break;
// }
// }
// else{
// break;
// }
// }
// return 0;
// }
// #include <stdio.h>
// #include <stdlib.h>
// #include <string.h>
// #include <termios.h>
// #include <unistd.h> // 添加这一行
// int getch() {
// struct termios oldt, newt;
// int ch;
// tcgetattr(STDIN_FILENO, &oldt);
// newt = oldt;
// newt.c_lflag &= ~(ICANON | ECHO);
// tcsetattr(STDIN_FILENO, TCSANOW, &newt);
// ch = getchar();
// tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
// return ch;
// }
// int main() {
// char str[20];
// char ip[16], c;
// int temp = 0;
// char a[5], b[5];
// strcpy(b, "yes");
// while (1) {
// printf("是否进入系统yes or no");
// scanf("%s", &a);
// if (strcmp(b, a) == 0) {
// printf("请输入IP");
// while (1) {
// c = getch();
// if (c == '\r')
// break;
// else {
// ip[temp] = c;
// printf("%c", c);
// }
// temp++;
// }
// printf("%s", ip);
// printf("服务连接中....");
// sprintf(str, "ssh root@%s", ip);
// system(str);
// } else {
// break;
// }
// }
// return 0;
// }
// #include <stdio.h>
// #include <stdlib.h>
// #include <string.h>
// #include <termios.h>
// #include <unistd.h>
// int getch() {
// struct termios oldt, newt;
// int ch;
// tcgetattr(STDIN_FILENO, &oldt);
// newt = oldt;
// newt.c_lflag &= ~(ICANON | ECHO);
// tcsetattr(STDIN_FILENO, TCSANOW, &newt);
// ch = getchar();
// tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
// return ch;
// }
// int main() {
// char str[20];
// char ip[16], c;
// int temp = 0;
// char a[5], b[5];
// char password[20]; // 添加这一行
// strcpy(b, "yes");
// strcpy(password, "11185730waq"); // 设置正确的密码
// while (1) {
// printf("是否进入系统yes or no");
// scanf("%s", &a);
// if (strcmp(b, a) == 0) {
// printf("请输入IP");
// while (1) {
// c = getch();
// if (c == '\r')
// break;
// else {
// ip[temp] = c;
// printf("%c", c);
// }
// temp++;
// }
// printf("%s", ip);
// printf("请输入密码:");
// temp = 0; // 重置temp变量
// while (1) {
// c = getch();
// if (c == '\r')
// break;
// else {
// password[temp] = c;
// printf("*"); // 隐藏密码输入
// }
// temp++;
// }
// printf("");
// if (strcmp(password, "11185730waq") == 0) { // 修改这一行,将"your_password"替换为实际密码
// printf("服务连接中....");
// sprintf(str, "ssh root@%s", ip);
// system(str);
// } else {
// printf("密码错误!退出程序。");
// break;
// }
// } else {
// break;
// }
// }
// return 0;
// }