LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2019, 10:53 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 217

Rep: Reputation: Disabled
Send command to another terminal and press enter


I am struggling here to do this over a remote shell , there are a few tricks that almost do the job , but when it turns to be the login shell they don't work .

I already tried ttyecho without success because ttyecho sends a new line with "-n" switch but login does not interpret that command as an "ENTER" .

I also tried :

Quote:
echo -e "root\n" > /dev/tty1
and i get the root there and the new line , but it does not work .

What i need is to send to a different tty the login and password over a remote shell , does anyone have an idea in how to do it ?
 
Old 08-27-2019, 11:23 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,844

Rep: Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320
Are you referring to physical terminals, or the emulated ones accessible via Ctrl+Alt+Fn keys on Linux?
 
Old 08-27-2019, 11:58 AM   #3
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 217

Original Poster
Rep: Reputation: Disabled
emulated terminals on a server .
my server does not go to a gui interface , i did not want it that way , it goes directly to a shell waiting for username and password , i also dont want to remove that .
What i need is to connect remotely to that server over ssh (witch i already do) and insert the username and password so i can start a network script on it .
Basically my server is right next to me , i could do that by connecting my keyborad by usb to it and do all this stuff , but after i done i have to remove the keyboard from server to connect again into the machine i usually work .
 
Old 08-27-2019, 12:15 PM   #4
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 2,263

Rep: Reputation: 499Reputation: 499Reputation: 499Reputation: 499Reputation: 499
Quote:
What i need is to connect remotely to that server over ssh (witch i already do) and insert the username and password so i can start a network script on it .
ssh into it and run the script

Code:
ssh 192.168.0.5
<enter password if used>
./myscript.sh
https://askubuntu.com/questions/8653...ng-ssh-session
https://stackoverflow.com/questions/...ch-the-session
https://www.tecmint.com/keep-remote-...disconnection/
 
Old 08-27-2019, 12:36 PM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 217

Original Poster
Rep: Reputation: Disabled
Quote:
ssh into it and run the script
Here it is the problem , i cant run the script over my ssh session because i will not be connected to the server much time , and when i disconnect my ssh session the script will stop .
The script i want to run must be running on tty1 witch is the main display of the server .

What i need is to send username and password to server so the main tty session can start .
This is all i need here .

Note : If it was simple then i already had done it .

Last edited by pedropt; 08-27-2019 at 12:38 PM.
 
Old 08-27-2019, 12:45 PM   #6
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
Try a terminal multiplexer like tmux or screen. That will allow you to start script and leave it running even after you disconnect. As a bonus when you re-attach you see all the output that happened while you were gone.

Even though I grew up on screen, I recommend tmux. There's so much you can do with it, that there is too much to even begin to describe.

The gist would be to start a new session:

Code:
tmux
Then to re-attach,

Code:
tmux a
Sessions and windows can even be given names to help identify them in scripts or just label them on the screen. So you can even make a script that lauches tmux and then launches your other script within it.
 
1 members found this post helpful.
Old 08-27-2019, 03:54 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,844

Rep: Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320
Perhaps openvt would be an option (ssh remotehost 'trap "" HUP; openvt someprogram &' or similar).

https://linux.die.net/man/1/openvt

Last edited by NevemTeve; 08-27-2019 at 03:55 PM.
 
Old 08-27-2019, 05:55 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,994

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
I second Turbocapitalist on that

my personal prefrence is screen, but I don't really remember why I didn't get on with tmux.. I may give it another try

Code:
alias a_remote='ssh -t  192.168.0.5 "screen -Ax || screen"'
that will try to reattach ( -x ) to an existing screen, if none then it starts a new one.
the -A adjusts the 'window' size, saves you having to do "^a F"
( ^a by default is mapped to Ctrl+a )

you 'detach' from screen with "^a d", this then terminates the ssh session.. but screen and any programs/scripts you have running are still running

if you are in the habit of using Ctrl+a to return to the begining of a line in a bash shell you will soon be double tapping a, even when not in screen

To open a new window, ^a c
the switch back and forth, ^a ^a ( not a typo, you do it twice )
too many things to list, so I will stop ( many I simply don't know )
I should probably read the manual

you can attach to another screen from in screen, but it gets confusing

below is my ~/.screenrc
I have bolded the bit laptap users may be interested in
it gives battery level, handy if you are on a console ( no X running )

Code:
termcapinfo xterm* ti@:te@
altscreen on
term screen-256color
backtick 1 60 60 /usr/bin/acpi
caption always
caption string "%{= kG}%3n %t | %1`%{g}"
hardstatus alwayslastline
hardstatus string '%{= kG}[%{B} %Y-%m-%d %{W}%c:%s %{g}] %{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}]'
you can find plenty of screenrc examples on the web

you can also start screen with systemd, and have it start things for you


Code:
[Unit]
Description=givemeaname

[Service]
Type=forking
User=<username>
ExecStart=/usr/bin/screen -s /bin/bash -dmS Name -t script /path/to/some/script.sh
ExecStartPost=/usr/bin/screen -S Name -X screen -t script2 /path/to/script2.sh
ExecStartPost=/usr/bin/screen -S Name -X screen -t script3 /path/to/script3.sh
ExecStop=<some cmd to stop script>
ExecStop=< .. to stop script2>
ExecStop=< stop script3>

WorkingDirectory=/home/username

[Install]
WantedBy=multi-user.target

yeah, screen or tmux does what you need
 
Old 08-27-2019, 06:16 PM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,994

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by pedropt View Post
The script i want to run must be running on tty1 witch is the main display of the server .

What i need is to send username and password to server so the main tty session can start .
This is all i need here .

Note : If it was simple then i already had done it .
oh, hold on I missed that, it *must* be run on tty1

I wonder if customising /etc/inittab will work for you.
https://www.tldp.org/LDP/sag/html/config-init.html

inittab 'runs' getty on tty1, which runs login
I guess, and is only a guess atm
You could replace getty with something else..
I imagine it is root the executes that, so you could have a wrapper script
Code:
su -u username /path/to/script.sh
you could use a VM to test that out
probably a good idea to remove the respawn while tesing, if it fails it could get way too spammy.
 
Old 08-27-2019, 06:21 PM   #10
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,635

Rep: Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205
Working from memory here, but doesn’t the -e switch suppress the return on echo? Try your echo without it.

Working in the same terminal I’ve always used nohup to run jobs in the background. It automagically redirects output to a file and keeps the job running even after the session ends.

(nohup == no hang up)

Last edited by scasey; 08-27-2019 at 06:22 PM.
 
Old 08-27-2019, 06:35 PM   #11
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,703

Rep: Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678
Quote:
Originally Posted by scasey View Post
Working from memory here, but doesnít the -e switch suppress the return on echo? Try your echo without it.

Working in the same terminal Iíve always used nohup to run jobs in the background. It automagically redirects output to a file and keeps the job running even after the session ends.

(nohup == no hang up)
-n suppresses nl -e interprets control codes ( \e[35;m and so on )
 
1 members found this post helpful.
Old 08-27-2019, 06:38 PM   #12
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,635

Rep: Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205
Quote:
Originally Posted by Keith Hedger View Post
-n suppresses nl -e interprets control codes ( \e[35;m and so on )
Thanks. I wasn't in a place to look that up when I posted earlier...but I wanted to put nohup in the discussion.
 
Old 08-27-2019, 06:44 PM   #13
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,994

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by scasey View Post
Working in the same terminal I’ve always used nohup to run jobs in the background. It automagically redirects output to a file and keeps the job running even after the session ends.

(nohup == no hang up)
yeah, that is what I used to do before I found gnu screen

but I don't think nohup, screen or tmux are a solution for pedropt.

I might be wrong but I belive they require output on the physical monitor screen

I wonder if screen or tmux can 'replace' getty in the inittab

actually, many years ago I created a 'live' cd for burn testing PCs
it would run mprime for each core/thread
it was most likely tmux I was using to split the screen into 2 or 4 depending on the CPU
Alt+F2 was tailing logs

I must have editied inittab to achive that.

Last edited by Firerat; 08-27-2019 at 06:46 PM.
 
Old 08-27-2019, 07:35 PM   #14
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,635

Rep: Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205
Quote:
Originally Posted by Firerat View Post
yeah, that is what I used to do before I found gnu screen

but I don't think nohup, screen or tmux are a solution for pedropt.

I might be wrong but I belive they require output on the physical monitor screen
I don't know squat about screen or tmux, but all the servers I've ever administered were headless. My only access has been via ssh (or telnet in the very old days)...so I don't agree. I think that nohup is the solution for the OP:

Code:
NAME
       nohup - run a command immune to hangups, with output to a non-tty

SYNOPSIS
       nohup COMMAND [ARG]...

DESCRIPTION
       Run COMMAND, ignoring hangup signals.
so
Code:
nohup /path/to/script
By default, STDOUT is redirected to a nohup.out file, although the script can be redirected (>) normally. see man nohup

So,
ssh to server
login
nohup script
exit

The script will continue to run.
 
Old 08-28-2019, 12:02 AM   #15
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
Quote:
Originally Posted by Firerat View Post
my personal prefrence is screen, but I don't really remember why I didn't get on with tmux.. I may give it another try
For one, by default it is easier to create, see, and switch between sessions and session windows and session window panes. I find tmux simpler but more flexible. Once you start configuring it and making a lot of customizations, you'll see a bigger difference. It does not have serial line support and it is a bit different to make a shared session. The former can be provided by other single-purpose programs (minicom, cu, etc) and the latter on file system permissions and system groups.

Quote:
Originally Posted by Firerat View Post

Code:
alias a_remote='ssh -t  192.168.0.5 "screen -Ax || screen"'
If I were using screen then I usually do like this instead:

Code:
alias a_remote='ssh -t  192.168.0.5 "screen -dR"'
That will attach to the existing session (while detaching from any other terminal) but create a new session if none already exists.

The same would be done in tmux in about the same way as you gave for screen above:

Code:
alias a_remote='ssh -t  192.168.0.5 "tmux a -d || tmux"'
I don't have much in .tmux.conf though, just a few global settings. Normally I just set the specifics in the run-time arguments. For example, I don't want 'remain-on-exit' set for all sessions, just in some. I want other sessions in different colors or visual-bell on or off or split windows. Some launch specific programs automatically. And so on, but without affecting the other sessions. For example:

Code:
tmux new-session -s 'logmonitor' -d \; new-window -n 'logs' \; split-window -h -t 'logs' \; \
        send-keys -t logmonitor:logs.0 '/tail -f /var/log/somelog.log' C-M \; \
        send-keys -t logmonitor:logs.1 '/tail -f /var/log/otherlog.log' C-M \; \
        set-option -t logmonitor status-style 'bg=colour8'; 
tmux attach-session -t logmonitor;
I really ought to look into the fancier stuff though.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot sequentially press Alt key then menu accelerator, must press both concurrently Woodsman Slackware 1 03-21-2013 03:21 PM
Unable to enter password in terminal to enter as root.. ANISHKUMAR.R Linux - Newbie 5 08-07-2010 07:46 AM
LXer: http://www.clusterresources.com/pages/press/press-releases.php LXer Syndicated Linux News 0 04-12-2006 07:21 AM
Boot Disk Failure, Insert System Disk and Press Enter!! novkhan Linux - Hardware 1 12-15-2005 07:39 PM
"Disk boot failure. Enter system disk and press enter" Fear58 Linux - Hardware 3 06-25-2004 05:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration