On HostA, I have a process, procA, that does I/O, like so:
#!/bin/bash
while sleep 1 ; do
# Consume input *if available*
if read -t 0.5 x; then # a timed-read
# If input is available, output it.
echo "HostA Input: $x"
fi
# Produce some output.
echo "HostA Output: $(date)"
done
On HostB, I have a very similar process, procB, with the only difference that procB tags its input/output with the prefix HostB (instead of the prefix HostA).
Now, I want to use the netcat program (/usr/bin/nc) to send procA's stdout to procB's stdin, and procB's stdout to procA's stdin. My expectation in all this is that HostA's output should appear on HostB, and HostB's output should appear on HostA... much as would happen in following canonical nc example of a peer-to-peer chat:
[HostB]$ nc -l 4000
[HostA]$ nc HostB 4000
But, when I tried to extend the above canonical example by replacing the human at both hosts with processes procA and procB as follows, it did not work:
On HostB:
$ mkfifo p
$ ./procB <p | nc -l 4000 >p
<WHY NO OUTPUT HERE??>
and, on HostA:
$ mkfifo p
$ ./procA <p | nc HostB 4000 >p
<WHY NO OUTPUT HERE??>
Notes:
I have verified that port 4000 is open on HostB.
Unidirectional communication, such as the following, seems to work:
On HostB:
$ mkfifo p
$ ./procB <p | nc -l 4000 >p
On HostA:
$ ./procA | nc HostB 4000
HostB Output: Wed Jan 11 13:53:37 IST 2012
HostB Output: Wed Jan 11 13:53:39 IST 2012
HostB Input: HostA Output: Wed Jan 11 13:55:02 IST 2012
HostB Output: Wed Jan 11 13:53:40 IST 2012
HostB Input: HostA Output: Wed Jan 11 13:55:04 IST 2012
HostB Output: Wed Jan 11 13:53:41 IST 2012
HostB Input: HostA Output: Wed Jan 11 13:55:05 IST 2012
HostB Output: Wed Jan 11 13:53:42 IST 2012
^C
Now, I know that nc comes with an INSECURE option -e that could somehow be employed if it's enabled in your nc binary, but assuming that I don't have -e option available, how can I connect 2 remote processes to each other via nc? Basically, where I'm going wrong above? Why there is no output in the places I have indicated above? Many thanks in advance.
The behavior described above is actually the right behavior, just that I did not realize that it was.
Wherever I was complaining about <WHY NO OUTPUT HERE??>, it was/is because the stdout's of the terminals have been redirected to the local fifos. (No wonder, you don't see anything on the terminal!)
Further, in Notes (2) above, it is not really true that the communication is unidirectional; it is very much bidirectional, just that you don't get to see any output on HostB, once again, because stdin and stdout have redirected from the terminal to the fifo. Thus, you correctly see procB's output on HostA, which is nothing but the output of procA on HostA sent to procB on HostB as procB's input!
Wow!
EDIT:
To actually see the output on each terminal, use tee, like so:
On HostB:
$ mkfifo p
$ ./procB <p | nc -l 4000 | tee p
On HostA:
$ mkfifo p
$ ./procA <p | nc HostB 4000 | tee p
Related
There are two scripts on HostB
# loop.sh
while true; do
date
sleep 2
done
and
# loop12.sh
optv=$1
while true; do
case $optv in
2)
echo "from stderr" >&2
date >&2
;;
*)
echo "from stdout"
date
;;
esac
sleep 2
done
I run ssh HostB './loop.sh &' on HostA, the Terminal prints the date every 2 seconds.
When I press Ctrl + c to stopssh HostB running on HostA, and check the process with ps -ef | grep loop on HostB, the process loop.sh is still there.
I run ssh HostB './loop12.sh &' on HostA, the Terminal prints the date every 2 seconds, too.
But when I press Ctrl + c to stopssh HostB running on HostA, and check the process with ps -ef | grep loop12 on HostB, the process loop12.sh is gone.
I just wonder Why the scripts are similar but they have different phenomenon?
My goal is to ssh into several host, check when the current branch was modified, and if older than 48 hours, mark the host as available. Eventually, I'll need to schedule this as a cron job but I haven't gotten to that level yet.
I have this:
hosts="rick morty beth jerry summer"
extra_vars="release_branch=$1"
show_deployed_branches() {
for host in $hosts
do
if [[ ! -z "$branch_dir" ]]; then
branchstamp=`$SHELL_AS ssh -o StrictHostKeyChecking=no $host cat $branch_dir/* 2> /dev/null`
printf " [*] %s:\n%s\n" $host "$branchstamp"
else
branchstamp=`$SHELL_AS ssh -o StrictHostKeyChecking=no $host cat $branch_file 2> /dev/null`
printf " [*] %s: %s\n" $host "$branchstamp"
fi
done
}
This will show the following:
[*] rick: GLIB-1111 on February 28 at 08:30 UTC by Birdman
[*] morty: BLEM-2371 on February 26 at 16:55 UTC by Birdman
[*] beth: AVAILABLE
[*] jerry: DOPE-12 on March 01 at 19:28 UTC by Birdman
[*] summer: MAIN on March 01 at 18:17 UTC by MrPoopyButthole
If I want to make Jerry available, I run the following:
release_box(jerry) {
$SHELL_AS ssh -o StrictHostKeyChecking=no $1 "echo 'AVAILABLE' > $branch_file" 2> /dev/null
echo " [*] Finished with $1"
I know this requires an if statement, but I'm stumped on next steps. I know an If statement is needed but don't know what will read the modified date and determine that its too old.
Working with Raspberry Pi2, ModelB with below configurations:
$ uname -a
Linux raspberrypi 4.9.59+ #1047 Sun Oct 29 11:47:10 GMT 2017 armv6l GNU/Linux
$ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
I have a MediaTek usb-adapter as below:
$ lsusb | grep 76
Bus 001 Device 004: ID 148f:7601 Ralink Technology, Corp. MT7601U Wireless Adapter
When trying to start "hostapd" for creating access-point, the below error is reported:
$ sudo /usr/local/bin/hostapd -ddd /etc/hostapd/hostapd.conf
random: Trying to read entropy from /dev/random
Configuration file: /etc/hostapd/hostapd.conf
Line 2: invalid/unknown driver 'mt7601u'
1 errors found in configuration file '/etc/hostapd/hostapd.conf'
However, the driver does exist,
$ ls /lib/modules/4.9.59+/kernel/drivers/net/wireless/mediatek/mt7601u
-rw-r--r-- 1 root root 126932 Oct 30 14:36 mt7601u.ko
Can someone please help point out what is going wrong here ? why is it not able to find the driver ?
backup.sh has :
#!/bin/bash
expect backup.exp
tr -d '\15\32' < backup.templog > backup.log
backup.exp has next content:
#!/usr/bin/expect -f
puts "----------------- [exec date +%Y.%m.%d\ %H:%M:%S] ------ exp start -----\n"
spawn ssh root#someIP2
expect "# "
send "su - postgres\r"
expect "\$ "
send "ls -lh db.dump.gz\r"
send exit\r
send exit\r
interact
when I run manually like next:
bash backup.sh >> backup.templog 2>&1
I get next, which I like:
spawn ssh root#someIP
Linux db01 3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u1 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Sep 2 11:06:02 2015 from someIP2
root#db01:~# su - postgres
postgres#db01:~$ ls -lh /var/lib/postgresql/db.dump.gz
-rw-r--r-- 1 postgres postgres 218M Sep 2 10:39 /var/lib/postgresql/db.dump.gz
postgres#db01:~$ exit
logout
root#db01:~# exit
logout
Connection to someIP2 closed.
but with cron (has a line):
* * * * * bash backup.sh >> backup.templog 2>&1
I get only next:
spawn ssh root#someIP2
Linux db01 3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u1 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Sep 2 11:04:13 2015 from someIP1
root#db01:~# su - postgres
postgres#db01:~$
Can I get the same with cron and manually?
Monit can not start redis-server properly.
Running Redis through init.d works correctly:
$ sudo su
$ /etc/init.d/redis_6379 start
$ #=> Starting Redis server...
$ ps aux | grep redis
$ #=> root 8980 0.0 0.0 42128 1964 ? Ssl 04:56 0:00 /etc/redis/src/redis-server *:6379
$ /etc/init.d/redis_6379 stop
$ #=> Stopping ...
$ #=> Redis stopped
$ #=> (ps aux| grep redis) There's no redis process.
Running Redis through Monit does not work correctly:
(I killed the Redis process and rm /var/run/redis_6379.pid)
$ sudo su
$ monit start redis
$ ps aux | grep redis
$ #=> root 9082 0.0 0.0 35076 1972 ? Ssl 05:08 0:00 /etc/redis/src/redis-server *:6379
monit.log:
[MSK Jan 6 05:08:14] info : 'redis' start on user request
[MSK Jan 6 05:08:14] info : monit daemon at 3947 awakened
[MSK Jan 6 05:08:14] info : Awakened by User defined signal 1
[MSK Jan 6 05:08:14] info : 'redis' start: /etc/init.d/redis_6379
[MSK Jan 6 05:08:44] error : 'redis' failed to start
[MSK Jan 6 05:08:44] info : 'redis' start action done
Stopping Redis through Monit does not works correclty also:
$ ps aux | grep redis
$ #=> root 9018 0.0 0.0 35076 1968 ? Ssl 05:02 0:00 /etc/redis/src/redis-server *:6379
$ monit stop redis
$ ps aux | grep redis
$ #=> root 9082 0.0 0.0 35076 1972 ? Ssl 05:08 0:00 /etc/redis/src/redis-server *:6379
monit.log
[MSK Jan 6 05:10:02] info : 'redis' stop on user request
[MSK Jan 6 05:10:02] info : monit daemon at 3947 awakened
[MSK Jan 6 05:10:02] info : Awakened by User defined signal 1
[MSK Jan 6 05:10:02] info : 'redis' stop action done
I have:
Ubuntu 12.04.3 LTS
redis-2.8.2
monit-5.3.2
redis instalation path /etc/redis
monit instalation path /etc/monit (installed from apt-get repo)
And following config files:
https://gist.github.com/itsNikolay/665112df34d2eae09330
I had the same problem, and there is no mutch talk about this situation around. I fixed it with another solution, it may concern someone else so I post it here.
in monit configuration file I had
start program = "/etc/init.d/redis start"
stop program = "/etc/init.d/redis stop"
Replacing by the following fixed the problem (with Ubuntu)
start program = "/usr/sbin/service redis start"
stop program = "/usr/sbin/service redis stop"
Just changes owner of /etc/redis dir
$ chown -R root /etc/redis
and restart monit
$ monit restart
The problem is gone. Strange. I wish it helps.