LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-20-2021, 04:51 AM   #1
kaempfer1968
LQ Newbie
 
Registered: Sep 2021
Posts: 10

Rep: Reputation: Disabled
check this for mistake


In the web I have found a script called "Autosuspend".

I'm no programmer. But it is my understanding that the part of the script below looks for active clients on port 9981 and 9982.
Though I have one or more active clients on port 9982 the variable "$active_clients" never gets TRUE.
Can someone tell me if there is a mistake in this part of the script.

The script is running on Linux Mint 20.



Here is the part of the script that is not working

TVHEADEND_IP=$(echo ${TVHEADEND_IP:-$(hostname -I)} | tr -d [:space:])
TVHEADEND_HTTP_PORT=${TVHEADEND_HTTP_PORT:-9981}
TVHEADEND_HTSP_PORT=${TVHEADEND_HTSP_PORT:-9982}

TVHEADEND_PORTS="$TVHEADEND_HTTP_PORT $TVHEADEND_HTSP_PORT"
LANG=C
active_clients=()
for port in $TVHEADEND_PORTS; do
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))
done
if [ $active_clients ]; then
logit "Tvheadend has active clients: ${active_clients[@]}"
return 1
fi
 
Old 10-20-2021, 07:19 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by kaempfer1968 View Post
Code:
TVHEADEND_IP=$(echo ${TVHEADEND_IP:-$(hostname -I)} | tr -d [:space:])
hostname -I may return more than one IP address. Provided TVHEADEND_IP is not set elsewhere, this code won't work in that case.

Last edited by shruggy; 10-20-2021 at 07:21 AM.
 
1 members found this post helpful.
Old 10-20-2021, 07:28 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
looks like the same problem as your other post. What I don't really understand: how do you want to solve it if you are not a programmer. [How] Do you think without any knowledge a script named autosuspend will do what you need? How do you know that part of the code which is not working?

You can debug your code with set -xv.
hostname -I may return more than one IP addresses, in that case the script cannot work.

By the way, would be nice to post your code in code tags.
 
2 members found this post helpful.
Old 10-21-2021, 06:00 AM   #4
kaempfer1968
LQ Newbie
 
Registered: Sep 2021
Posts: 10

Original Poster
Rep: Reputation: Disabled
@ pan64
When I was younger I programmed a little bit (Basic, Turbo Pascal, C++).

In principle I understand in which way the script "autosuspend" is working. I already checked some parts of the script. I checked e.g. that the "FOR DO" loop is running two times. And I understand that my pc is not running down when "$active_clients" is TRUE. But " $active_clients" never gets TRUE. That's why I try do understand this line
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))

Can you check if there is a mistake in this line?

I understand that this part of the script should check if there is traffic on port 9981 and 9982 of the localhost. If there is traffic "$active_clients" gets TRUE.
 
Old 10-21-2021, 06:32 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Quote:
Originally Posted by kaempfer1968 View Post
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))

Can you check if there is a mistake in this line?
yes, I can check and I think that line is correct. what we found is the very first line (containing hostname -I). But without details hard to say more
 
1 members found this post helpful.
Old 10-21-2021, 08:23 AM   #6
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,597

Rep: Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545

https://www.shellcheck.net/

 
1 members found this post helpful.
Old 10-21-2021, 03:30 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by kaempfer1968 View Post
In the web I have found a script called "Autosuspend".
...
Here is the part of the script that is not working
  1. Assuming the script came from someone or some site. Is there any way you can contact the author? They'd be the ideal person to ask about what they wrote.
  2. As suggested you should post more, if not the whole script.
  3. Also as said before you can use "set -xv" to enable debug and the part where you wrote "Here is the part of the script that is not working", what you should do is provide any output that you see indicating why that part doesn't work. Debug output would be helpful.
  4. While we get that the diagnosis of this is above your current talents, people need more details as opposed to the clip provided.
 
Old 10-21-2021, 04:15 PM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by kaempfer1968 View Post
That's why I try do understand this line
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))
netstat is a legacy tool, nowadays many would rather prefer ss. Something like
Code:
...$(ss -Htun state established "sport = :$port" src "$TVHEADEND_IP"|
awk -F'[ :]' '$0=$(NF-1)')
Other than that the command looks OK.
 
Old 10-22-2021, 03:14 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,781

Rep: Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199
I think the prefix
IFS=$'\n'
has no effect, can be omitted.

The prefix sets an environment for a forked shell, such as a command.
But an assignment = or += is not a command; no fork happens.
The environment is only set for the $( ) sub shell where it has no effect, neither in netstat nor in grep.

TVHEADEND_IP=$(echo ${TVHEADEND_IP:-$(hostname -I)} | tr -d [:space:])
should be
[ "$TVHEADEND_IP" ] || read TVHEADEND_IP junk < <(hostname -i)
or
[ "$TVHEADEND_IP" ] || TVHEADEND_IP=( $(hostname -i) )

The script should be entirely rewritten...
 
Old 10-23-2021, 02:58 AM   #10
kaempfer1968
LQ Newbie
 
Registered: Sep 2021
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thank you for all this hints here. But I still don't understand how to change this line now.
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))

I tested again a little bit.
When I have NO active client and write in the terminal this
netstat -n | grep -oP 192.168.178.60:9982
I get as result "nothing".

When I have AN active client and write in the terminal this
netstat -n | grep -oP 192.168.178.60:9982
I get this line as result
192.168.178.60:9982

So my script needs to do something like this:
active_clients=()
if [ (netstat -n | grep -oP 192.168.178.60:9982) = (192.168.178.60:9982) ]; then
logit "Tvheadend has active clients: ${active_clients[@]}"
return 1
fi

It is my understanding that "return 1" means that "active_clients=1" right?
Can this work? What do I have to change in my script?

Nice weekend!
 
Old 10-23-2021, 04:07 AM   #11
lovemeslk
Member
 
Registered: Feb 2020
Location: Rantoul IL
Distribution: Slackware
Posts: 350

Rep: Reputation: 72
Quote:
Originally Posted by kaempfer1968 View Post
In the web I have found a script called "Autosuspend".

I'm no programmer. But it is my understanding that the part of the script below looks for active clients on port 9981 and 9982.
Though I have one or more active clients on port 9982 the variable "$active_clients" never gets TRUE.
Can someone tell me if there is a mistake in this part of the script.

The script is running on Linux Mint 20.



Here is the part of the script that is not working

TVHEADEND_IP=$(echo ${TVHEADEND_IP:-$(hostname -I)} | tr -d [:space:])
TVHEADEND_HTTP_PORT=${TVHEADEND_HTTP_PORT:-9981}
TVHEADEND_HTSP_PORT=${TVHEADEND_HTSP_PORT:-9982}

TVHEADEND_PORTS="$TVHEADEND_HTTP_PORT $TVHEADEND_HTSP_PORT"
LANG=C
active_clients=()
for port in $TVHEADEND_PORTS; do
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))
done
if [ $active_clients ]; then
logit "Tvheadend has active clients: ${active_clients[@]}"
return 1
fi
could you be so kind as to click the advance and then click code then insert it in the code brackets Thank you
 
Old 10-23-2021, 06:04 AM   #12
kaempfer1968
LQ Newbie
 
Registered: Sep 2021
Posts: 10

Original Poster
Rep: Reputation: Disabled
Code:
TVHEADEND_IP=$(echo ${TVHEADEND_IP:-$(hostname -I)} | tr -d [:space:])
TVHEADEND_HTTP_PORT=${TVHEADEND_HTTP_PORT:-9981}
TVHEADEND_HTSP_PORT=${TVHEADEND_HTSP_PORT:-9982}

TVHEADEND_PORTS="$TVHEADEND_HTTP_PORT $TVHEADEND_HTSP_PORT"
LANG=C
active_clients=()
for port in $TVHEADEND_PORTS; do
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))
done
if [ $active_clients ]; then
logit "Tvheadend has active clients: ${active_clients[@]}"
return 1
fi
 
Old 10-23-2021, 11:10 AM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,680

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
On my Mint 20 VM since there is a line in /etc/hosts i.e. 127.0.1.1 <hostname> and there are no other virtual ports besides lo, hostname -I is the correct syntax. However, since I don't know exactly how your system is configured make sure the output is as expected. Run the command from a terminal.

hostname -I

There are a few statements that could be rewritten but the partial script "as is" works on my Mint 20 VM. I used ssh port 22 as a test instead of 9981,9982. I assume logit is a function and I did not need the return statement.

Code:
if [ $active_clients ]; then
I believe since $active_clients is an array the if statement checks if there an element 0. If there is the statement is true.

There could be other problems with the script.
 
Old 10-24-2021, 12:47 PM   #14
kaempfer1968
LQ Newbie
 
Registered: Sep 2021
Posts: 10

Original Poster
Rep: Reputation: Disabled
See my terminal output
Code:
oliver@oliver-desktop:~$ hostname -I
192.168.178.60 2002:d9e8:b018:0:49de:fb6e:a6bf:4c89 2002:d9e8:b018:0:499e:1edb:e92f:4117 
oliver@oliver-desktop:~$ hostname -i
127.0.1.1
oliver@oliver-desktop:~$
 
Old 10-24-2021, 12:53 PM   #15
kaempfer1968
LQ Newbie
 
Registered: Sep 2021
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by kaempfer1968 View Post
Thank you for all this hints here. But I still don't understand how to change this line now.
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))

I tested again a little bit.
When I have NO active client and write in the terminal this
netstat -n | grep -oP 192.168.178.60:9982
I get as result "nothing".

When I have AN active client and write in the terminal this
netstat -n | grep -oP 192.168.178.60:9982
I get this line as result
192.168.178.60:9982

So my script needs to do something like this:
active_clients=()
if [ (netstat -n | grep -oP 192.168.178.60:9982) = (192.168.178.60:9982) ]; then
logit "Tvheadend has active clients: ${active_clients[@]}"
return 1
fi

It is my understanding that "return 1" means that "active_clients=1" right?
Can this work? What do I have to change in my script?
I hoped that it is an easy and good idea what I have written here. No one an idea how this part of the script should look like?
 
  


Reply



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
LXer: Big mistake, Google. Big mistake: Chrome OS to be 'folded into Android' LXer Syndicated Linux News 0 10-29-2015 11:33 PM
Partition check, check double check Vincentius Linux - General 0 12-25-2004 05:47 AM
Boot disk; check. CD in drive; check. Doesn't work; check. Hal DamnSmallLinux 7 02-04-2004 02:10 AM
spelling mistake verigoth LQ Suggestions & Feedback 2 06-02-2002 09:46 PM
Linux Mandrake 8.0 -- HELP I made a mistake during install and don't know how to fix! Avatar Linux - Software 9 07-27-2001 10:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:15 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
Open Source Consulting | Domain Registration