libite 2.6.1
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"

Go to the source code of this file.

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)
 
int telnet_close (sdbuf_t *ctx)
 
int telnet_expect (sdbuf_t *ctx, char *script[], FILE *output)
 
int telnet_session (int addr, short port, char *script[], FILE *output)
 

Detailed Description

Author
Joachim Wiberg
Date
2010-2021

Definition in file telnet.c.

Macro Definition Documentation

◆ EXPECT_DEBUG

#define EXPECT_DEBUG   0

Definition at line 34 of file telnet.c.

◆ EXPECT_POLL_TIMEOUT

#define EXPECT_POLL_TIMEOUT   3000 /* 3 sec timeout waiting for server. */

Definition at line 35 of file telnet.c.

◆ TELL

#define TELL ( fmt,
args... )

Definition at line 40 of file telnet.c.

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).

Definition at line 116 of file telnet.c.

◆ 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.

Definition at line 174 of file telnet.c.

◆ 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.

Definition at line 56 of file telnet.c.

◆ 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.

Definition at line 295 of file telnet.c.