基于C语言调用ssh实现远程连接Linux服务器

一、前言

起初觉得在VMware上面使用Linux很不方便,当时又不知道FinalShell,在摸索下偶然发现可以使用DOS命令连接Linux,于是突发奇想用C语言进行简单的包装一下

二、DOS命令ssh

使用dos命令ssh 用户名@ip 就可以实现远程连接Linux了,前提是这个Linux在公网上或者在本地上才能进行连接,下面是我自己本地虚拟机里面的Linux

//ssh 用户名@ip
ssh root@192.168.88.130

如果ssh建立连接成功那么就会显示以下提示,输入你的用户密码就可以了

三、C语言代码实现

但是这么做的就会很麻烦,每次都需要输入用户和IP,于是使用C语言写一个代码生成一个可执行程序,这样每次想要连接的时候直接双击可执行程序就可以了

以下是源码,默认是root用户,当然你也可以使用两个sprintf拼接函数来实现指定用户登录

#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;
} 

代码运行过程:

*个人认为以上的难点就在于实时获取用户输入的IP并存入数组中

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在C语言实现SSH远程连接,你可以使用开源的libssh库。下面是一个简单的示例代码,演示如何连接到远程SSH服务器并执行命令: ```c #include <stdio.h> #include <stdlib.h> #include <libssh/libssh.h> int main() { ssh_session my_ssh_session; int rc; // 创建一个新的SSH会话对象 my_ssh_session = ssh_new(); if (my_ssh_session == NULL) { fprintf(stderr, "Failed to create SSH session\n"); exit(1); } // 设置SSH会话的选项 ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "remote_host"); ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "username"); // 连接到远程SSH服务器 rc = ssh_connect(my_ssh_session); if (rc != SSH_OK) { fprintf(stderr, "Error connecting to remote host: %s\n", ssh_get_error(my_ssh_session)); ssh_free(my_ssh_session); exit(1); } // 进行用户身份验证 rc = ssh_userauth_password(my_ssh_session, NULL, "password"); if (rc != SSH_AUTH_SUCCESS) { fprintf(stderr, "Error authenticating with password: %s\n", ssh_get_error(my_ssh_session)); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(1); } // 在远程服务器上执行命令 ssh_channel channel; char buffer[256]; int nbytes; channel = ssh_channel_new(my_ssh_session); if (channel == NULL) { fprintf(stderr, "Failed to create SSH channel\n"); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(1); } rc = ssh_channel_open_session(channel); if (rc != SSH_OK) { fprintf(stderr, "Error opening SSH channel: %s\n", ssh_get_error(my_ssh_session)); ssh_channel_free(channel); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(1); } rc = ssh_channel_request_exec(channel, "command_to_execute"); if (rc != SSH_OK) { fprintf(stderr, "Error executing command: %s\n", 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(1); } nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); while (nbytes > 0) { if (fwrite(buffer, 1, nbytes, stdout) != nbytes) { fprintf(stderr, "Error writing to stdout\n"); ssh_channel_close(channel); ssh_channel_free(channel); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(1); } nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); } if (nbytes < 0) { fprintf(stderr, "Error reading from SSH channel: %s\n", 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(1); } // 关闭SSH通道和会话 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; } ``` 请确保将 "remote_host" 替换为远程SSH服务器的主机名或IP地址,"username" 替换为您的SSH用户名,"password" 替换为您的SSH密码,"command_to_execute" 替换为要在远程服务器上执行的命令。 这只是一个基本示例,libssh库还提供了许多其他功能和选项,你可以根据需要进行进一步的定制和扩展。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值