[alt.sources] play with telnet via socket - telnet 02/02

wht@tridom.uucp (Warren Tucker) (07/21/89)

#!/bin/sh
# shar:	Shell Archiver  (v1.22)
#
#	Run the following text with /bin/sh to create:
#	  t_apply.c
#	  unixlogin.c
#	  s_apply.c
#	  stratlogin.c
#	  hexdump.c
#
if test -f t_apply.c; then echo "File t_apply.c exists"; else
echo "x - extracting t_apply.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > t_apply.c &&
X/* CHK=0xADD8 */
X/*+-------------------------------------------------------------------------
X	t_apply.c - sample application for 'teleplay' - Pyramid BSD UNIX login
X	...!gatech!emory!tridom!wht
X
X  Defined functions:
X	application()
X	application_hangup(code)
X	application_init(argc,argv)
X	get_opt(argc,argv,opts)
X	get_opt_ERR(s,c)
X
X--------------------------------------------------------------------------*/
X/*+:EDITS:*/
X/*:07-20-1989-16:38-wht-flush edits - rev. x1.0  */
X
X#include <stdio.h>
X
Xextern char *index();
X
Xextern char *hostname;
X
X#if defined(DEBUG)
Xextern int debug;
Xchar *options = "h:Dd";
X#else
Xchar *options = "h:";
X#endif
X
Xstatic int optind = 1;
Xstatic int optopt;
Xstatic char *optarg;
X
X/*+-------------------------------------------------------------------------
X	get_opt_ERR(s,c)
X--------------------------------------------------------------------------*/
Xstatic void
Xget_opt_ERR(s,c)
Xchar *s;
Xchar c;
X{
X	fputs(s,stderr);
X	fputc(c,stderr);
X	fputc('\n',stderr);
X	exit(255);
X}	/* end of get_opt_ERR */
X
X/*+-------------------------------------------------------------------------
X	get_opt(argc,argv,opts) - almost standard, but ...
X--------------------------------------------------------------------------*/
Xstatic int
Xget_opt(argc,argv,opts)
Xint argc;
Xchar **argv;
Xchar *opts;
X{
Xstatic int sp = 1;
Xregister c;
Xregister char *cp;
Xchar errbuf[2];
X
X	if(sp == 1)
X	{
X		if(optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
X			return(EOF);
X		else if(!strcmp(argv[optind],"--"))
X		{
X			optind++;
X			return(EOF);
X		}
X	}
X	optopt = c = argv[optind][sp];
X	if(c == ':' || (cp=index(opts,c)) == NULL)
X	{
X		get_opt_ERR(": unknown option, -",c);
X		if(argv[optind][++sp] == '\0')
X		{
X			optind++;
X			sp = 1;
X		}
X		return('?');
X	}
X	if(*++cp == ':')
X	{
X		if(argv[optind][sp+1] != '\0')
X			optarg = &argv[optind++][sp+1];
X		else if(++optind >= argc)
X		{
X			get_opt_ERR(": argument missing for -",c);
X			sp = 1;
X			return('?');
X		}
X		else
X			optarg = argv[optind++];
X		sp = 1;
X	}
X	else 
X	{
X		if(argv[optind][++sp] == '\0')
X		{
X			sp = 1;
X			optind++;
X		}
X		optarg = NULL;
X	}
X	return(c);
X}	/* end of get_opt */
X
X/*+-------------------------------------------------------------------------
X	application_init(argc,argv) - initialize application
X
X1.  process argv (terminate program if unsatisfactory)
X2.  initialize application environment
X--------------------------------------------------------------------------*/
Xapplication_init(argc,argv)
Xint argc;
Xchar **argv;
X{
Xregister itmp = 0;
Xextern int eol_type;	/* 0 = strip 0x0D, LF terminator */
X						/* 1 = 0x0D+0x00 == 0x0A */
X
X	eol_type = 0;
X
X	hostname = (char *)0;
X
X	while(itmp != EOF)
X	{
X		switch(itmp = get_opt(argc,argv,options))
X		{
X			case 'D':
X				debug = 3;
X				break;
X			case 'd':
X				debug++;
X				break;
X			case 'h':
X				hostname = optarg;
X				break;
X		}
X	}
X
X#if defined(pyr)		/* you probably want to change this */
X	if(!hostname)
X		hostname = "tridom";
X#endif
X
X}	/* end of application_init */
X
X/*+-------------------------------------------------------------------------
X	application()
X--------------------------------------------------------------------------*/
Xvoid
Xapplication()
X{
X	login("wht","testpwd","dumb",1);
X	telputs("ps -au\r");
X	telsearch(100,"% ",15,"% ",1);
X	telputs("logout\r");	/* moot, really ... */
X	sleep(2);				/* ... but just for form */
X	puts("");
X}	/* end of application */
X
X/*+-------------------------------------------------------------------------
X	application_hangup(code)
X--------------------------------------------------------------------------*/
Xvoid
Xapplication_hangup(code)
Xint code;
X{
X	;	/* not needed for this application */
X}	/* end of application_hangup */
X
X/* vi: set tabstop=4 shiftwidth=4: */
X/* end of t_apply.c */
SHAR_EOF
chmod 0644 t_apply.c || echo "restore of t_apply.c fails"
fi
if test -f unixlogin.c; then echo "File unixlogin.c exists"; else
echo "x - extracting unixlogin.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > unixlogin.c &&
X/* CHK=0x2A1B */
X/*+-------------------------------------------------------------------------
X	unixlogin.c - teleplay UNIX login functions
X	...!gatech!emory!tridom!wht
X
X  Defined Functions:
X	login_failure(reason)
X	login(username,password,termtype,showit)
X
X--------------------------------------------------------------------------*/
X/*+:EDITS:*/
X/*:07-20-1989-16:39-wht-flush edits - rev. x1.0  */
X
X#include <stdio.h>
X
X/*+-------------------------------------------------------------------------
X	login_failure(reason)
X--------------------------------------------------------------------------*/
Xvoid
Xlogin_failure(reason)
Xchar *reason;
X{
X	fputs("===> LOGIN FAILURE",stdout);
X	if(*reason)
X		printf(": %s\n",stdout);
X	else
X		puts("");
X	hangup(1);
X}	/* end of login_failure */
X
X/*+-------------------------------------------------------------------------
X	login(username,password,termtype,showit) - given extant session, login
X
Xlogin to remote UNIX system using 'username' and 'password'
Xif 'termtype' != 0 and non-null, look for tset prompt and supply termtype
X
XNote: login expected to present csh prompt ending with "% "
Xafter optional tset sequence
X--------------------------------------------------------------------------*/
Xvoid
Xlogin(username,password,termtype,showit)
Xchar *username;
Xchar *password;
Xchar *termtype;
Xint showit;
X{
Xint maxlines;
Xchar buf[256];
Xchar *searchstr;
Xchar *delim;
Xint send_termtype;
X
X/* ------- login --------------------------------------------- */
X	if(!telsearch(10,"login:",20,"in: ",1))
X		login_failure("search for 'login: ' failed");
X	telputs(username); telputc('\r');
X
X/* ------- password ------------------------------------------ */
X	if(!telsearch(5,"word:",20,"rd:",1))
X		login_failure("search for 'Password:' failed");
X	telputs(password); telputc('\r');
X
X/* ------- check for incorrect login -- set up for tset ------ */
X	if(send_termtype = (termtype && *termtype))
X	{
X		searchstr = "TERM";
X		delim = ") ";
X	}
X	else
X		searchstr = delim = "% ";
X
X	maxlines = 40;		/* plenty (wordy motd's do happen) */
X	while(maxlines--)
X	{
X		telgets(buf,15,delim,1);
X		if(showit)
X			fputs(buf,stdout);
X		if(issubstr(buf,searchstr))
X			break;
X		if(issubstr(buf,"incorrect"))
X			login_failure("");
X	}
X	if(!maxlines)
X		login_failure( (send_termtype)
X			? "tset request not seen" : "'% ' prompt not seen");
X
X/* ------- if tset requested, do it now ---------------------- */
X	if(!send_termtype)
X		return;
X	telputs(termtype); telputc('\r');
X	if(!telsearch(10,"% ",20,"% ",1))
X		login_failure();
X
X}	/* end of login */
X
X/* vi: set tabstop=4 shiftwidth=4: */
X/* end of unixlogin.c */
SHAR_EOF
chmod 0644 unixlogin.c || echo "restore of unixlogin.c fails"
fi
if test -f s_apply.c; then echo "File s_apply.c exists"; else
echo "x - extracting s_apply.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > s_apply.c &&
X/*+-------------------------------------------------------------------------
X	s_apply.c - sample application for 'teleplay' - Stratus login
X	...!gatech!emory!tridom!wht
X
X  Defined functions:
X	application()
X	application_hangup(code)
X	application_init(argc,argv)
X	get_opt(argc,argv,opts)
X	get_opt_ERR(s,c)
X
X--------------------------------------------------------------------------*/
X/*+:EDITS:*/
X/*:07-20-1989-16:38-wht-flush edits - rev. x1.0  */
X
X#include <stdio.h>
X
Xextern char *index();
X
Xextern char *hostname;
X
X#if defined(DEBUG)
Xextern int debug;
Xchar *options = "h:Dd";
X#else
Xchar *options = "h:";
X#endif
X
Xstatic int optind = 1;
Xstatic int optopt;
Xstatic char *optarg;
X
Xchar *target_dir = "%tridom#m1_d01>Development>wht";
X
X/*+-------------------------------------------------------------------------
X	get_opt_ERR(s,c)
X--------------------------------------------------------------------------*/
Xstatic void
Xget_opt_ERR(s,c)
Xchar *s;
Xchar c;
X{
X	fputs(s,stderr);
X	fputc(c,stderr);
X	fputc('\n',stderr);
X	exit(255);
X}	/* end of get_opt_ERR */
X
X/*+-------------------------------------------------------------------------
X	get_opt(argc,argv,opts) - almost standard, but ...
X--------------------------------------------------------------------------*/
Xstatic int
Xget_opt(argc,argv,opts)
Xint argc;
Xchar **argv;
Xchar *opts;
X{
Xstatic int sp = 1;
Xregister c;
Xregister char *cp;
Xchar errbuf[2];
X
X	if(sp == 1)
X	{
X		if(optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
X			return(EOF);
X		else if(!strcmp(argv[optind],"--"))
X		{
X			optind++;
X			return(EOF);
X		}
X	}
X	optopt = c = argv[optind][sp];
X	if(c == ':' || (cp=index(opts,c)) == NULL)
X	{
X		get_opt_ERR(": unknown option, -",c);
X		if(argv[optind][++sp] == '\0')
X		{
X			optind++;
X			sp = 1;
X		}
X		return('?');
X	}
X	if(*++cp == ':')
X	{
X		if(argv[optind][sp+1] != '\0')
X			optarg = &argv[optind++][sp+1];
X		else if(++optind >= argc)
X		{
X			get_opt_ERR(": argument missing for -",c);
X			sp = 1;
X			return('?');
X		}
X		else
X			optarg = argv[optind++];
X		sp = 1;
X	}
X	else 
X	{
X		if(argv[optind][++sp] == '\0')
X		{
X			sp = 1;
X			optind++;
X		}
X		optarg = NULL;
X	}
X	return(c);
X}	/* end of get_opt */
X
X/*+-------------------------------------------------------------------------
X	application_init(argc,argv) - initialize application
X
X1.  process argv (terminate program if unsatisfactory)
X2.  initialize application environment
X--------------------------------------------------------------------------*/
Xapplication_init(argc,argv)
Xint argc;
Xchar **argv;
X{
Xregister itmp = 0;
Xextern int eol_type;	/* 0 = strip 0x0D, LF terminator */
X						/* 1 = 0x0D+0x00 == 0x0A */
X
X	eol_type = 1;
X
X	hostname = (char *)0;
X
X	while(itmp != EOF)
X	{
X		switch(itmp = get_opt(argc,argv,options))
X		{
X			case 'D':
X				debug += 3;
X				break;
X			case 'd':
X				debug++;
X				break;
X			case 'h':
X				hostname = optarg;
X				break;
X		}
X	}
X
X	if(!hostname)
X		hostname = "stratus";
X
X}	/* end of application_init */
X
X/*+-------------------------------------------------------------------------
X	application_failure(err1,err2,code)
X--------------------------------------------------------------------------*/
Xapplication_failure(err1,err2,code)
Xchar *err1;
Xchar *err2;
Xint code;
X{
X	puts(err1);
X	if(err2 && *err2)
X		puts(err2);
X	hangup(code);
X}	/* end of application_failure */
X
X/*+-------------------------------------------------------------------------
X	application()
X--------------------------------------------------------------------------*/
Xvoid
Xapplication()
X{
Xchar buf[128];
X
X	slogin("wht","testpwd",1);
X
X	telputs("set_terminal_parameters -pause_lines 0\n");
X	telgets(buf,10,"",0);	/* eat echoed command */
X	puts(buf);
X	telgets(buf,10,"",0);
X	if(!issubstr(buf,"ready"))
X		application_failure("cannot set terminal parameters",buf,2);
X
X/* select directory */
X	sprintf(buf,"change_current_dir %s\n",target_dir);
X	telputs(buf);
X	telgets(buf,10,"",0);	/* eat echoed command */
X	puts(buf);
X	telgets(buf,10,"",0);
X	if(!issubstr(buf,"ready"))
X		application_failure("cannot get to proper directory",buf,2);
X
X/* make sure proper directory selected */
X	telputs("display_current_dir\n");
X	telgets(buf,10,"",0);	/* eat echoed command */
X	puts(buf);
X	telgets(buf,10,"",0);	/* get directory */
X	puts(buf);
X	if(strcmp(buf,target_dir))
X		application_failure("dcd reported improper directory",buf,2);
X	telgets(buf,10,"",1);
X
X	telputs("display_terminal_parameters\n");
X	telsearch(100,"ready ",15,"",1);
X
X	telputs("logout\n");	/* moot, really ... */
X	telsearch(100,"logged ",15,"",1);
X	puts("");
X}	/* end of application */
X
X/*+-------------------------------------------------------------------------
X	application_hangup(code)
X--------------------------------------------------------------------------*/
Xvoid
Xapplication_hangup(code)
Xint code;
X{
X	;	/* not needed for this application */
X}	/* end of application_hangup */
X
X/* vi: set tabstop=4 shiftwidth=4: */
X/* end of s_apply.c */
SHAR_EOF
chmod 0644 s_apply.c || echo "restore of s_apply.c fails"
fi
if test -f stratlogin.c; then echo "File stratlogin.c exists"; else
echo "x - extracting stratlogin.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > stratlogin.c &&
X/*+-------------------------------------------------------------------------
X	stratlogin.c - teleplay Stratus login functions
X	...!gatech!emory!tridom!wht
X
X  Defined Functions:
X	slogin_failure(reason)
X	slogin(username,password,termtype,showit)
X
X--------------------------------------------------------------------------*/
X/*+:EDITS:*/
X/*:07-20-1989-16:38-wht-flush edits - rev. x1.0  */
X
X#include <stdio.h>
X
X/*+-------------------------------------------------------------------------
X	slogin_failure(reason)
X--------------------------------------------------------------------------*/
Xvoid
Xslogin_failure(reason)
Xchar *reason;
X{
X	fputs("===> LOGIN FAILURE",stdout);
X	if(*reason)
X		printf(": %s\n",stdout);
X	else
X		puts("");
X	hangup(1);
X}	/* end of slogin_failure */
X
X/*+-------------------------------------------------------------------------
X	slogin(username,password,showit) - login to stratus
X
Xlogin to remote Stratus system using 'username' and 'password'
X--------------------------------------------------------------------------*/
Xvoid
Xslogin(username,password,termtype,showit)
Xchar *username;
Xchar *password;
Xchar *termtype;
Xint showit;
X{
Xint maxlines;
Xchar buf[256];
Xchar *searchstr;
Xchar *delim;
X
X/* ------- login --------------------------------------------- */
X	if(!telsearch(60,"login",20,"",1))
X		slogin_failure("search for 'Please login' failed");
X	telputs("login ");
X	telputs(username);
X	telputc('\n');
X
X/* ------- password ------------------------------------------ */
X	if(!telsearch(3,"word?",20,"d?",1))
X		slogin_failure("search for 'Password?' failed");
X	telputs(password); telputc('\n');
X
X/* ------- check for incorrect login ------------------------- */
X	maxlines = 10;
X	while(maxlines--)
X	{
X		telgets(buf,15,"",1);
X		if(showit)
X			fputs(buf,stdout);
X		if(issubstr(buf,"ready  "))
X			break;
X		if(issubstr(buf,"denied"))
X			slogin_failure("");
X	}
X	if(!maxlines)
X		slogin_failure("streaming stratus?");
X
X}	/* end of slogin */
X
X/* vi: set tabstop=4 shiftwidth=4: */
X/* end of stratlogin.c */
SHAR_EOF
chmod 0644 stratlogin.c || echo "restore of stratlogin.c fails"
fi
if test -f hexdump.c; then echo "File hexdump.c exists"; else
echo "x - extracting hexdump.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > hexdump.c &&
X/*+-----------------------------------------------------------------------
X	hexdump.c  -- very generic hex/graphics dump development aid
X
X  Defined functions:
X	hex_dump(str,len,title,terse_flag)
X	hex_dump_fp(fp,str,len,title,terse_flag)
X	hex_dump16(int16)
X	hex_dump32(int32)
X	hex_dump4(int4)
X	hex_dump8(int8)
X
X------------------------------------------------------------------------*/
X/*+:EDITS:*/
X/*:07-20-1989-16:38-wht-flush edits - rev. x1.0  */
X
X#include <stdio.h>
X
X#ifndef uchar
X#define uchar unsigned char
X#define ushort unsigned short
X#define ulong unsigned long
X#endif
X
Xstatic FILE *dumpfp;
X
X#define dump_putc(ch)		fputc((ch),dumpfp)
X#define dump_puts(str)		fputs(str,dumpfp)
X
X/*+-----------------------------------------------------------------------
X	hex_dump#... subservient routines
X------------------------------------------------------------------------*/
Xvoid hex_dump4(int4)
Xuchar int4;
X{
X	int4 &= 15;
X	dump_putc((int4 >= 10) ? (int4 + 'A' - 10) : (int4 + '0'));
X}
X
Xvoid hex_dump8(int8)
Xuchar int8;
X{
X	hex_dump4(int8 >> 4);
X	hex_dump4(int8);
X}
X
Xvoid hex_dump16(int16)
Xushort int16;
X{
X	hex_dump8(int16 >> 8);
X	hex_dump8(int16);
X}
X
Xvoid hex_dump32(int32)
Xulong int32;
X{
X	hex_dump16(int32 >> 16);
X	hex_dump16(int32);
X}
X
X
X/*+-----------------------------------------------------------------
X	hex_dump_fp(fp,str,len,title,terse_flag)
X
X  if 'title' not NULL, title is printed... 'terse_flag'
X  controls whether or not the title is "conspicuous" with
X  hyphens before and after it making title line >70 chars long
X------------------------------------------------------------------*/
Xvoid
Xhex_dump_fp(fp,str,len,title,terse_flag)
XFILE *fp;
Xchar *str;
Xint len;
Xchar *title;
Xint terse_flag;
X{
Xint istr = 0;
Xregister ipos;
Xregister itmp;
X
X	dumpfp = fp;
X
X	if(strlen(title))
X	{
X		if(!terse_flag)
X		{
X			ipos = (73 - strlen(title)) / 2;
X			itmp = ipos;
X			while(itmp--)
X				dump_putc('-');
X			dump_putc(' ');
X		}
X		dump_puts(title);
X		if(!terse_flag)
X		{
X			dump_putc(' ');
X			while(ipos--)
X				dump_putc('-');
X		}
X		if(dumpfp == stderr)
X			dump_puts("\r\n");
X		else
X			dump_puts("\n");
X
X	}
X
X	while(istr < len)
X	{
X		hex_dump16(istr);
X		dump_putc(' ');
X		for(itmp = 0; itmp < 16; ++itmp)
X		{
X			ipos = istr + itmp;
X			if(ipos >= len)
X			{
X				if(!terse_flag)
X					dump_puts("   ");
X				continue;
X			}
X			dump_putc(' ');
X			hex_dump8(str[ipos]);
X		}
X		dump_puts(" | ");
X		for(itmp = 0; itmp < 16; ++itmp)
X		{
X			ipos = istr + itmp;
X			if( (ipos) >= len)
X			{
X				if(!terse_flag)
X					dump_putc(' ');
X			}
X			else
X			{
X				dump_putc((str[ipos] >= ' ' && str[ipos] < 0x7f)
X		 			? str[ipos] : '.' );
X			}
X		}
X		if(dumpfp == stderr)
X			dump_puts(" |\r\n");
X		else
X			dump_puts(" |\n");
X		istr += 16;
X	}   /* end of while(istr < len) */
X
X}	/* end of hex_dump_fp */
X
X/*+-------------------------------------------------------------------------
X	hex_dump(str,len,title,terse_flag)
X--------------------------------------------------------------------------*/
Xvoid
Xhex_dump(str,len,title,terse_flag)
Xchar *str;
Xint len;
Xchar *title;
Xint terse_flag;
X{
X	hex_dump_fp(stdout,str,len,title,terse_flag);
X}	/* end of hex_dump_fp */
X
X/* end of hexdump.c */
X/* vi: set tabstop=4 shiftwidth=4: */
SHAR_EOF
chmod 0644 hexdump.c || echo "restore of hexdump.c fails"
fi
exit 0
-- 
-------------------------------------------------------------------
Warren Tucker, Tridom Corporation       ...!gatech!emory!tridom!wht 
Ker-au'-lo-phon.  An 8-foot partial flue-stop, having metal pipes
surmounted by adjustable rings, and with a hole bored near the top
of each pipe, producing a soft and "reedy" tone.