mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-08-08 19:02:06 +03:00
examples: Reformat sshnetcat.c
Signed-off-by: Jakub Jelen <jjelen@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
committed by
Andreas Schneider
parent
1229ad650b
commit
f039edd85d
@@ -40,46 +40,49 @@ clients must be made or how a client should react.
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
char *host;
|
char *host;
|
||||||
const char *desthost="localhost";
|
const char *desthost = "localhost";
|
||||||
const char *port="22";
|
const char *port = "22";
|
||||||
|
|
||||||
#ifdef WITH_PCAP
|
#ifdef WITH_PCAP
|
||||||
#include <libssh/pcap.h>
|
#include <libssh/pcap.h>
|
||||||
char *pcap_file=NULL;
|
char *pcap_file = NULL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void usage(void)
|
static void usage(void)
|
||||||
{
|
{
|
||||||
fprintf(stderr,"Usage : sshnetcat [user@]host forwarded_host forwarded_port\n");
|
fprintf(stderr,
|
||||||
|
"Usage : sshnetcat [user@]host forwarded_host forwarded_port\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int opts(int argc, char **argv){
|
static int opts(int argc, char **argv)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
while((i=getopt(argc,argv,"P:"))!=-1){
|
while ((i = getopt(argc, argv, "P:")) != -1) {
|
||||||
switch(i){
|
switch (i) {
|
||||||
#ifdef WITH_PCAP
|
#ifdef WITH_PCAP
|
||||||
case 'P':
|
case 'P':
|
||||||
pcap_file=optarg;
|
pcap_file = optarg;
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
fprintf(stderr,"unknown option %c\n",optopt);
|
fprintf(stderr, "unknown option %c\n", optopt);
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(optind < argc)
|
if (optind < argc)
|
||||||
host=argv[optind++];
|
host = argv[optind++];
|
||||||
if(optind < argc)
|
if (optind < argc)
|
||||||
desthost=argv[optind++];
|
desthost = argv[optind++];
|
||||||
if(optind < argc)
|
if (optind < argc)
|
||||||
port=argv[optind++];
|
port = argv[optind++];
|
||||||
if(host==NULL)
|
if (host == NULL)
|
||||||
usage();
|
usage();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void select_loop(ssh_session session,ssh_channel channel){
|
static void select_loop(ssh_session session, ssh_channel channel)
|
||||||
|
{
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
struct timeval timeout;
|
struct timeval timeout;
|
||||||
char buffer[BUF_SIZE];
|
char buffer[BUF_SIZE];
|
||||||
@@ -88,133 +91,141 @@ static void select_loop(ssh_session session,ssh_channel channel){
|
|||||||
*/
|
*/
|
||||||
ssh_channel channels[2], outchannels[2];
|
ssh_channel channels[2], outchannels[2];
|
||||||
int lus;
|
int lus;
|
||||||
int eof=0;
|
int eof = 0;
|
||||||
int maxfd;
|
int maxfd;
|
||||||
int ret;
|
int ret;
|
||||||
while(channel){
|
while (channel) {
|
||||||
do{
|
do {
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
ZERO_STRUCT(fds);
|
ZERO_STRUCT(fds);
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
if(!eof)
|
if (!eof)
|
||||||
FD_SET(0,&fds);
|
FD_SET(0, &fds);
|
||||||
timeout.tv_sec=30;
|
timeout.tv_sec = 30;
|
||||||
timeout.tv_usec=0;
|
timeout.tv_usec = 0;
|
||||||
|
|
||||||
fd = ssh_get_fd(session);
|
fd = ssh_get_fd(session);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
fprintf(stderr, "Error getting the session file descriptor: %s\n",
|
fprintf(stderr,
|
||||||
|
"Error getting the session file descriptor: %s\n",
|
||||||
ssh_get_error(session));
|
ssh_get_error(session));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
FD_SET(fd, &fds);
|
FD_SET(fd, &fds);
|
||||||
maxfd = fd + 1;
|
maxfd = fd + 1;
|
||||||
|
|
||||||
channels[0]=channel; // set the first channel we want to read from
|
channels[0] = channel; // set the first channel we want to read from
|
||||||
channels[1]=NULL;
|
channels[1] = NULL;
|
||||||
ret=ssh_select(channels,outchannels,maxfd,&fds,&timeout);
|
ret = ssh_select(channels, outchannels, maxfd, &fds, &timeout);
|
||||||
if(ret==EINTR)
|
if (ret == EINTR)
|
||||||
continue;
|
continue;
|
||||||
if(FD_ISSET(0,&fds)){
|
if (FD_ISSET(0, &fds)) {
|
||||||
lus=read(0,buffer,sizeof(buffer));
|
lus = read(0, buffer, sizeof(buffer));
|
||||||
if(lus)
|
if (lus)
|
||||||
ssh_channel_write(channel,buffer,lus);
|
ssh_channel_write(channel, buffer, lus);
|
||||||
else {
|
else {
|
||||||
eof=1;
|
eof = 1;
|
||||||
ssh_channel_send_eof(channel);
|
ssh_channel_send_eof(channel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(channel && ssh_channel_is_closed(channel)){
|
if (channel && ssh_channel_is_closed(channel)) {
|
||||||
ssh_channel_free(channel);
|
ssh_channel_free(channel);
|
||||||
channel=NULL;
|
channel = NULL;
|
||||||
channels[0]=NULL;
|
channels[0] = NULL;
|
||||||
}
|
}
|
||||||
if(outchannels[0]){
|
if (outchannels[0]) {
|
||||||
while(channel && ssh_channel_is_open(channel) && ssh_channel_poll(channel,0)){
|
while (channel && ssh_channel_is_open(channel) &&
|
||||||
lus = ssh_channel_read(channel,buffer,sizeof(buffer),0);
|
ssh_channel_poll(channel, 0)) {
|
||||||
if(lus==-1){
|
lus = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
|
||||||
fprintf(stderr, "Error reading channel: %s\n",
|
if (lus == -1) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"Error reading channel: %s\n",
|
||||||
ssh_get_error(session));
|
ssh_get_error(session));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(lus==0){
|
if (lus == 0) {
|
||||||
ssh_channel_free(channel);
|
ssh_channel_free(channel);
|
||||||
channel=channels[0]=NULL;
|
channel = channels[0] = NULL;
|
||||||
} else {
|
} else {
|
||||||
ret = write(1, buffer, lus);
|
ret = write(1, buffer, lus);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
fprintf(stderr, "Error writing to stdin: %s",
|
fprintf(stderr,
|
||||||
|
"Error writing to stdin: %s",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while(channel && ssh_channel_is_open(channel) && ssh_channel_poll(channel,1)){ /* stderr */
|
while (channel && ssh_channel_is_open(channel) &&
|
||||||
|
ssh_channel_poll(channel, 1)) { /* stderr */
|
||||||
lus = ssh_channel_read(channel, buffer, sizeof(buffer), 1);
|
lus = ssh_channel_read(channel, buffer, sizeof(buffer), 1);
|
||||||
if(lus==-1){
|
if (lus == -1) {
|
||||||
fprintf(stderr, "Error reading channel: %s\n",
|
fprintf(stderr,
|
||||||
|
"Error reading channel: %s\n",
|
||||||
ssh_get_error(session));
|
ssh_get_error(session));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(lus==0){
|
if (lus == 0) {
|
||||||
ssh_channel_free(channel);
|
ssh_channel_free(channel);
|
||||||
channel=channels[0]=NULL;
|
channel = channels[0] = NULL;
|
||||||
} else {
|
} else {
|
||||||
ret = write(2, buffer, lus);
|
ret = write(2, buffer, lus);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
fprintf(stderr, "Error writing to stderr: %s",
|
fprintf(stderr,
|
||||||
|
"Error writing to stderr: %s",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(channel && ssh_channel_is_closed(channel)){
|
if (channel && ssh_channel_is_closed(channel)) {
|
||||||
ssh_channel_free(channel);
|
ssh_channel_free(channel);
|
||||||
channel=NULL;
|
channel = NULL;
|
||||||
}
|
}
|
||||||
} while (ret==EINTR || ret==SSH_EINTR);
|
} while (ret == EINTR || ret == SSH_EINTR);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void forwarding(ssh_session session){
|
static void forwarding(ssh_session session)
|
||||||
|
{
|
||||||
ssh_channel channel;
|
ssh_channel channel;
|
||||||
int r;
|
int r;
|
||||||
channel = ssh_channel_new(session);
|
channel = ssh_channel_new(session);
|
||||||
r = ssh_channel_open_forward(channel, desthost, atoi(port), "localhost", 22);
|
r = ssh_channel_open_forward(channel, desthost, atoi(port), "localhost", 22);
|
||||||
if(r<0) {
|
if (r < 0) {
|
||||||
printf("error forwarding port : %s\n",ssh_get_error(session));
|
printf("error forwarding port : %s\n", ssh_get_error(session));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
select_loop(session,channel);
|
select_loop(session, channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int client(ssh_session session){
|
static int client(ssh_session session)
|
||||||
int auth=0;
|
{
|
||||||
|
int auth = 0;
|
||||||
char *banner;
|
char *banner;
|
||||||
int state;
|
int state;
|
||||||
|
|
||||||
if (ssh_options_set(session, SSH_OPTIONS_HOST ,host) < 0)
|
if (ssh_options_set(session, SSH_OPTIONS_HOST, host) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
ssh_options_parse_config(session, NULL);
|
ssh_options_parse_config(session, NULL);
|
||||||
|
|
||||||
if(ssh_connect(session)){
|
if (ssh_connect(session)) {
|
||||||
fprintf(stderr,"Connection failed : %s\n",ssh_get_error(session));
|
fprintf(stderr, "Connection failed : %s\n", ssh_get_error(session));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
state=verify_knownhost(session);
|
state = verify_knownhost(session);
|
||||||
if (state != 0)
|
if (state != 0)
|
||||||
return -1;
|
return -1;
|
||||||
ssh_userauth_none(session, NULL);
|
ssh_userauth_none(session, NULL);
|
||||||
banner=ssh_get_issue_banner(session);
|
banner = ssh_get_issue_banner(session);
|
||||||
if(banner){
|
if (banner) {
|
||||||
printf("%s\n",banner);
|
printf("%s\n", banner);
|
||||||
free(banner);
|
free(banner);
|
||||||
}
|
}
|
||||||
auth=authenticate_console(session);
|
auth = authenticate_console(session);
|
||||||
if(auth != SSH_AUTH_SUCCESS){
|
if (auth != SSH_AUTH_SUCCESS) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
forwarding(session);
|
forwarding(session);
|
||||||
@@ -224,17 +235,18 @@ static int client(ssh_session session){
|
|||||||
#ifdef WITH_PCAP
|
#ifdef WITH_PCAP
|
||||||
ssh_pcap_file pcap;
|
ssh_pcap_file pcap;
|
||||||
void set_pcap(ssh_session session);
|
void set_pcap(ssh_session session);
|
||||||
void set_pcap(ssh_session session){
|
void set_pcap(ssh_session session)
|
||||||
if(!pcap_file)
|
{
|
||||||
|
if (!pcap_file)
|
||||||
return;
|
return;
|
||||||
pcap=ssh_pcap_file_new();
|
pcap = ssh_pcap_file_new();
|
||||||
if(ssh_pcap_file_open(pcap,pcap_file) == SSH_ERROR){
|
if (ssh_pcap_file_open(pcap, pcap_file) == SSH_ERROR) {
|
||||||
printf("Error opening pcap file\n");
|
printf("Error opening pcap file\n");
|
||||||
ssh_pcap_file_free(pcap);
|
ssh_pcap_file_free(pcap);
|
||||||
pcap=NULL;
|
pcap = NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ssh_set_pcap_file(session,pcap);
|
ssh_set_pcap_file(session, pcap);
|
||||||
}
|
}
|
||||||
|
|
||||||
void cleanup_pcap(void);
|
void cleanup_pcap(void);
|
||||||
@@ -245,17 +257,19 @@ void cleanup_pcap(void)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int main(int argc, char **argv){
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
ssh_session session;
|
ssh_session session;
|
||||||
|
|
||||||
session = ssh_new();
|
session = ssh_new();
|
||||||
|
|
||||||
if(ssh_options_getopt(session, &argc, argv)) {
|
if (ssh_options_getopt(session, &argc, argv)) {
|
||||||
fprintf(stderr, "error parsing command line :%s\n",
|
fprintf(stderr,
|
||||||
|
"error parsing command line :%s\n",
|
||||||
ssh_get_error(session));
|
ssh_get_error(session));
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
opts(argc,argv);
|
opts(argc, argv);
|
||||||
#ifdef WITH_PCAP
|
#ifdef WITH_PCAP
|
||||||
set_pcap(session);
|
set_pcap(session);
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user