Sponsored Content
Full Discussion: Inter-shell communication
Operating Systems OS X (Apple) Inter-shell communication Post 302442254 by agama on Tuesday 3rd of August 2010 11:30:03 PM
Old 08-04-2010
Here's a small script that will do what I think you need. It works by starting a telnet process and piping its stdin/out back to the script. There are some hoops to be jumpped through to deal with the login/password prompts and then goes into a loop reading 'command-file' and sending the commands with a sleep in between. You will see the user name echoed back to your tty before the prompt for password; I didn't fuss with disabling echo when starting telnet.

It makes a BIG assumption that the PS1 prompt at the other end has a known trailing character ($ is coded in the script), and that this character isn't in the MOTD or other output. I don't think it will matter if it is in other output, other than possibly clogging the TCP input buffer on the local end a bit.

All output from the remote end to the tty is ignored -- probably a good idea if each command in the input file redirects both stdin and stdout.

Code:
#!/usr/bin/env ksh

host=${1:-localhost}
port=${2}                       # default to nothing to let telnet default
remote_prompt='$'               # assume user prompt at remote end has a trailing $

telnet $host $port |&           # start telnet in a separate process with stdin/out piped to this shell

                                # -p causes read/print to use the pipe to telnet
read -d : -p prompt             # read login goo up to the first colon ... login:
printf "$prompt: "              # prompt user, read answer (login name) and send back to telnet
read answer 
print -p  $answer 

read -d : -p prompt             # read next goo up to : (will be an echo of the user name)
printf "$prompt: "              # show prompt and then read and send password
stty -echo                      # dont echo passwd back onto tty
read answer
stty echo                       # reset terminal to echo mode
print -p  $answer 
printf "\n"

read -d $remote_prompt -p answer        # assume last character of prompt is $ -- read up to and discard
#printf "$discard: $answer\n"           # uncomment to debug

while read command
do
        printf "sending: $command\n"
        print -p $command               # send command; command should probably redirect stdout/stderr
        read -d $remote_prompt -p answer        # assume to be everything up through the prompt
        #echo "$answer"                 # uncomment for debug
        sleep 2
done <command-file

I've tested this under Kshell; no clue as to whether or not bash supports reading/writing to a child process. If you need more info about the Kshell read and print commands have a look at: Korn's Kshell man page at AT&T

You may know this, but I do feel the need to say: telnet is not secure and there are a good many users and admins that avoid it like the plague. This sort of thing can easily be accomplished via ssh with a lot less complexity in the local script and about 5 minutes worth of work to install your key on the remote host.

Hope this is of some use.
 

10 More Discussions You Might Find Interesting

1. Programming

Inter Process Communication

unix IPC i would like to know the method of usage of semaphores on shared memory segments the topic seems very difficult to understand mainly when difrent proceses communicate instantly and how do i avaoid deadlock situation (2 Replies)
Discussion started by: kamathanil
2 Replies

2. HP-UX

Inter Process File Handling Problem

Hi All, i am running a oracle procedure which writes a file . The same file is picked up by another script which runs in a cron after every 5 minutes. Now the problem is that sometimes my script picks up a file while the procedure is still writing data in the file. is there is any way i... (4 Replies)
Discussion started by: saurabhjain
4 Replies

3. UNIX for Advanced & Expert Users

Inter-process communication:pipes,doors,etc.

Hi, I am thinking about writing a log daemon for a multi-processed ksh application (yes - I know that high-level language would be a better option). My question is as follows: If many processes (many scripts) will try writing to a single log file: print "message" > common.log Will it work or... (2 Replies)
Discussion started by: adderek
2 Replies

4. OS X (Apple)

Serial Communication from shell Mac OS X PHP script

Hi again, I spoke with Keyspan and they assured me the USB to Serial device was working if I could use the Screen tool. I asked why I was unable to redirect stdin/stdout to and from the /dev/tty. device and was told there was no reason I shouldn't be able to do so. I have tried: ... (1 Reply)
Discussion started by: cpfogarty
1 Replies

5. Programming

C program using IPC (inter process communication)

i want to write a C chat program that communicates over IPC(inter process communication), that could be run using 2 seperate terminal windows within the same computer. so that wat u type in one terminal window , should appear on the other and vice versa... could some one please help me with the... (2 Replies)
Discussion started by: localp
2 Replies

6. Programming

help with write-read locks inter-process

I need help!Many Thanks! Now,I try to manage the shared memory inter-process . Inevitably,I have to deal with the synchronous. I know the pthread_rwlock in posix,and I compile ,then run successfully in Red Hat Enterprise 4. I have a doubt about whether the Posix supports the system such as... (1 Reply)
Discussion started by: weizh
1 Replies

7. Programming

logic understanding for inter client chat server

hello everyone, i am making chat server in linux using c. i have made programs in which group chat can take place between multiple clients but i am not able to understand how to make 2 particular clients chat with each other. please help!!! (1 Reply)
Discussion started by: sweetbella
1 Replies

8. Shell Programming and Scripting

communication between shell and and a demon 'c' program

Hello, i have a demon 'c' program that have a dynamic table of logic registers ( 2000 variables ). exemple of registers: I1.34.5 M23.4.1 I want from shell acess to this table of registers. How can i do this? with something like for read I1.34.5: #cat... (3 Replies)
Discussion started by: rzyz
3 Replies

9. Shell Programming and Scripting

Communication between different hosts through shell scripts.

1) I have two scripts named as a.sh,b.sh running two different hosts host1 and host2 2) So now how to execute b.sh on host2 from a.sh on host1 3) Also I have few queries regarding the same as I want to execute few commands with sudo and with different user in script b.sh so how this can be... (2 Replies)
Discussion started by: RSC1985
2 Replies

10. IP Networking

Implement inter vlan routing with Linux

Hello. I want to Communicate 2 VLAN with router like this solution: http://8pic.ir/images/83m0ouih8mmm9s1sfl56.jpg For this purpose I'm configuring 2 Linux system as a switch and connect 4 host to them. Then a router is added to scenario. The configuration of the switches is: On DUT1(Linux):... (1 Reply)
Discussion started by: zsn
1 Replies
CPANPLUS::Shell(3pm)					 Perl Programmers Reference Guide				      CPANPLUS::Shell(3pm)

NAME
CPANPLUS::Shell - base class for CPANPLUS shells SYNOPSIS
use CPANPLUS::Shell; # load the shell indicated by your # config -- defaults to # CPANPLUS::Shell::Default use CPANPLUS::Shell qw[Classic] # load CPANPLUS::Shell::Classic; my $ui = CPANPLUS::Shell->new(); my $name = $ui->which; # Find out what shell you loaded $ui->shell; # run the ui shell DESCRIPTION
This module is the generic loading (and base class) for all "CPANPLUS" shells. Through this module you can load any installed "CPANPLUS" shell. Just about all the functionality is provided by the shell that you have loaded, and not by this class (which merely functions as a generic loading class), so please consult the documentation of your shell of choice. BUG REPORTS
Please report bugs or other issues to <bug-cpanplus@rt.cpan.org<gt>. AUTHOR
This module by Jos Boumans <kane@cpan.org>. COPYRIGHT
The CPAN++ interface (of which this module is a part of) is copyright (c) 2001 - 2007, Jos Boumans <kane@cpan.org>. All rights reserved. This library is free software; you may redistribute and/or modify it under the same terms as Perl itself. SEE ALSO
CPANPLUS::Shell::Default, CPANPLUS::Shell::Classic, cpanp perl v5.18.2 2014-01-06 CPANPLUS::Shell(3pm)
All times are GMT -4. The time now is 11:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy