J
J
JDima2012-09-21 22:05:46
linux
JDima, 2012-09-21 22:05:46

Logging background ssh sessions?

Task: from a Linux server (centos 6) simultaneously establish ssh connections with several systems - and write to files (one per connection) everything that arrives from those systems without sending anything. Reconnect when disconnected. On the other side, the public key is imported, i.e. manual authentication is not required. The addresses of those to whom to connect can be parsed from a text file. Creating a separate script for the connection is not the point.
The obvious solution is to call something like "ssh [email protected]$IP >> log-$IP.txt &" inside the for loop. It does not work, the output of SSH running in the background does not fly to stdout, although everything is fine without the ampersand. "| tee file.txt &" doesn't work in the background either. As well as any other solutions based on the standard ssh client and stdout that I managed to google in a few hours.

Actually, a question. How to ensure that a client running in the background ssh (any, not only native) can keep a connection with that side indefinitely (until that side beats it off) and at the same time log everything heard to a file?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
J
JDima, 2012-09-22
@JDima

Won.
Running sh, bash and other scripts in the background does not work. With the exception of expect. Calling this script with a pipe and an ampersand correctly writes what it sees to a file:

#!/usr/bin/expect -f
set timeout -1
set user [lrange $argv 0 0]    
set ipaddr [lrange $argv 1 1]

while { true } {
spawn ssh [email protected]$ipaddr   
expect "impossible sequence"
sleep 10
}

Thanks to all.

A
Andrey Burov, 2012-09-21
@BuriK666

sh -c 'ssh [email protected] command' & > /path/to/output

E
EaS, 2012-09-22
@EaS

And if the launch of ssh and the redirect to the file are placed in the .sh file, and run from the loop?

A
Alexey Akulovich, 2012-09-22
@AterCattus

And so?
screen -Dms <screen_session_name> sh -c 'ssh [email protected] "do-smth" > /path/to/output'

S
Sergey, 2012-09-22
@bondbig

The connection goes to the ports of the terminal server, to which the consoles (COM ports) of various pieces of iron are connected. These pieces of iron periodically send messages to the console. These messages need to be recorded.
Wouldn't it be better then to send all this to a remote syslog server using syslogng?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question