libite
Classes | Macros | Functions
telnet.c File Reference
#include <errno.h>
#include <poll.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "lite.h"
Include dependency graph for telnet.c:

Macros

#define EXPECT_DEBUG   0
 
#define EXPECT_POLL_TIMEOUT   3000 /* 3 sec timeout waiting for server. */
 
#define TELL(fmt, args...)
 

Functions

sdbuf_ttelnet_open (int addr, short port)
 Open telnet connection to addr:port and connect. More...
 
int telnet_close (sdbuf_t *ctx)
 Close a telnet session previously opened with telnet_open() More...
 
int telnet_expect (sdbuf_t *ctx, char *script[], FILE *output)
 Poor man's telnet expect. More...
 
int telnet_session (int addr, short port, char *script[], FILE *output)
 Very simple expect-like implementation for telnet. More...
 

Detailed Description

Author
Joachim Wiberg
Date
2010-2021

Function Documentation

◆ telnet_close()

int telnet_close ( sdbuf_t ctx)

Close a telnet session previously opened with telnet_open()

Parameters
ctxAn sdbuf_t socket buffer context
Returns
Always returns POSIX OK(0).

◆ telnet_expect()

int telnet_expect ( sdbuf_t ctx,
char *  script[],
FILE *  output 
)

Poor man's telnet expect.

Parameters
ctxTelnet session context from telnet_open()
scriptNULL terminated list of expect and response strings
outputOptional output from session

Issues script sequence on telnet session specified in ctx, with optional output from session.

The script consists of strings of expect and response pairs. For example, expect "ogin: " followed by response "root\n", expect "assword: "with response "secret\n", concluded by NULL.

Returns
POSIX OK(0) or non-zero on error.

◆ telnet_open()

sdbuf_t* telnet_open ( int  addr,
short  port 
)

Open telnet connection to addr:port and connect.

Parameters
addrInteger encoded IPv4 address in network byte order
portInternet port number in network byte order
Returns
An sdbuf_t socket buffer context.

◆ telnet_session()

int telnet_session ( int  addr,
short  port,
char *  script[],
FILE *  output 
)

Very simple expect-like implementation for telnet.

Parameters
addrInteger encoded IPv4 address, in network byte order, use htonl().
portInternet port to connect to, in network byte order, use htons().
scriptExpect like script of paired 'expect', 'response' strings.
outputA FILE pointer to a tempfile() for output from the last command.

This is a very simple expect-like implementation for querying and operating daemons remotely over telnet.

The script is a query-response type of list of strings leading up to a final command, which output is then written to the given output file.

Returns
POSIX OK(0), or non-zero with errno set on error.