Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED]execute a command from terminal a into terminal b
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Mx11r3
n00b
n00b


Joined: 16 Nov 2012
Posts: 6

PostPosted: Thu Mar 20, 2014 5:01 pm    Post subject: [SOLVED]execute a command from terminal a into terminal b Reply with quote

Hello there fellow compilers.

I want to make a script that reads a text file(device-config.cfg) of config lines and execute those lines in another terminal with a slight delay each line. Kinda like copy paste each line but automated...
The reason is because I am not able to paste 20000+ lines at once into the device via a ssh terminal.

I came up with this script but it only prints the line in the other terminal instead of executing it, at least it gives a good picture as to what i want:

Code:

#!/bin/bash
while read line
do
    echo -e $line > /dev/pts/2

    sleep 0.2
done < device-config.cfg



I have also read about a program called "echotty" but I think it's no longer available.
_________________
Minimalist


Last edited by Mx11r3 on Mon Mar 31, 2014 6:46 pm; edited 1 time in total
Back to top
View user's profile Send private message
krinn
Watchman
Watchman


Joined: 02 May 2003
Posts: 7470

PostPosted: Thu Mar 20, 2014 9:41 pm    Post subject: Reply with quote

I know 0 way to do that, in order to make them run, you must pass them not to the other terminal, but to the other shell of that terminal.

But you might take the problem from the wrong side maybe.
If you wish doing that i suppose it's because that terminal is running an ssh session and you wish all lines to be execute within that context.

I hope it will help you : ssh can run a command and disconnect. Something you can easy try with "ssh user@host ls ~"
This way you can have your goal met, read all lines of your file, and run "ssh user@host lines#"

The problem doing like that, is that you will have 20000 ssh connections/disconnections instead of your persistent ssh connection.
I tag this as a "problem" but ssh doesn't care, it's just to let you show the difference.

But doing that way, you will achieve your goal easy :
Here's pseudo code :
do read line
ssh user@host line
sleep slightdelay
until EOF
Back to top
View user's profile Send private message
Mx11r3
n00b
n00b


Joined: 16 Nov 2012
Posts: 6

PostPosted: Fri Mar 21, 2014 2:24 pm    Post subject: Reply with quote

Hmm I don't think I can make that happen because the device (firewall/router) uses menu entrys which will be forgotten when ssh session ends.


I think i have an idea by using xclip. When I have time to figure that out i will post the results for anyone interested.
_________________
Minimalist
Back to top
View user's profile Send private message
Sansavarous
n00b
n00b


Joined: 15 Feb 2004
Posts: 22
Location: My computer.

PostPosted: Fri Mar 21, 2014 4:58 pm    Post subject: Reply with quote

Are you looking for something like this?

http://unix.stackexchange.com/questions/33557/using-an-already-established-ssh-channel
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3131

PostPosted: Fri Mar 21, 2014 5:48 pm    Post subject: Reply with quote

Why won't you copy/paste those 2000 lines to file, prepend it with `#! /bin/bash` and execute the file?
Back to top
View user's profile Send private message
cboldt
Veteran
Veteran


Joined: 24 Aug 2005
Posts: 1046

PostPosted: Sat Mar 22, 2014 1:55 pm    Post subject: Reply with quote

You might try screen on the destination machine. I use a short routine to cause multiple host machines to "emerge sync" based on successful completion of a portage sync on a local portage tree server.

Code:
ssh destination-machine screen -p 1 -x -X stuff "emerge --sync^M"


If you want to replicate material from one terminal to another, on the same machine, skip the `ssh destination machine` part. Your two terminals could be part of one session of `screen`, with information passed from one [screen] terminal to another. This command stuffs a command to a second [screen] terminal after the `emerge --sync` command is completed.

Code:
screen -p 2 -X stuff "date;emerge -puDv --changed-use @world^M"


Seems to me a routine that reads a file line by line, and "stuffs" each line to a selected instance of screen/terminal might suit your purpose. One potential pitfall is lack of coordination between the sender and receiver. Sender keeps sending, without regard to the receiver's having completed the previous order. Another is that the "stuff" buffer isn't very big, according to `man screen`.

szatox suggested:

Quote:
Why won't you copy/paste those 2000 lines to file, prepend it with `#! /bin/bash` and execute the file?


You can "execute" the contents of a file by "sourcing" it, rather than making it executable. Either way, having the entire file on the destination at once resolves the line-to-line coordination issue. The destination coordinates/paces itself.
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21602

PostPosted: Sat Mar 22, 2014 3:38 pm    Post subject: Reply with quote

According to the OP, the destination is some stupid embedded device that offers a menu system over ssh, rather than a regular shell. As such, he probably cannot run screen/tmux on the device. It might be possible to force ssh to send the entire blob by using ssh device <file. Another choice is to use expect to interact with the menu system.
Back to top
View user's profile Send private message
cboldt
Veteran
Veteran


Joined: 24 Aug 2005
Posts: 1046

PostPosted: Sat Mar 22, 2014 5:00 pm    Post subject: Reply with quote

Whatever is being interacted with, it was said to be at a /dev/pts, and I took that as being it was opened or viewed in a shell in a terminal.

Maybe all that's missing is a carriage return at the end of the line, in which case the cure might be as simple as appending \r or \n to the `echo -e $line` command line.

If the device can be opened in a shell in a terminal (on the local machine), that shell and terminal can be inside of a local instance of screen. That is, open screen, ssh to the device in one screen window, open another screen window (^A-c), and read/send from the second window, to the first.
Back to top
View user's profile Send private message
Mx11r3
n00b
n00b


Joined: 16 Nov 2012
Posts: 6

PostPosted: Mon Mar 31, 2014 6:42 pm    Post subject: Reply with quote

Thank you all for thinking with me on this one and sorry for my late reply I was away on vacation.

It is indeed an embedded system, Fortigate and Cisco ASA to be exact.

I found a quick and dirty way to solve the issue. The shortest code for it, is this:

Code:

#!/bin/bash

#Give some time to click the desired terminal-window to paste the commands in.
sleep 3

while read line
do
    echo -e $line

    #Read each line in file device-config.cfg
    echo $line | xclip -selection c
    #Paste each line active terminal-window
    xdotool keydown Ctrl keydown Shift key v keyup Ctrl keyup Shift
    #Wait a while
    sleep 0.05
done < device-config.cfg

_________________
Minimalist
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum