You are not logged in.

#1 2023-11-11 07:58:38

jojo06
Member
Registered: 2023-11-04
Posts: 113

[SOLVED]How to build file.py

I want to do the same to a python file, what we do in 'go build file.go'

I wrote a very beginner and lame python program:

import subprocess

def execute_shell_command(command):
    result = subprocess.run(command, shell=True, capture_output=True, text=True)
    return result.stdout.strip()

def print_color(text, color):
    if color == 'green':
        print(f"\033[92m{text}\033[0m")  # Green color ANSI escape code
    elif color == 'red':
        print(f"\033[91m{text}\033[0m")  # Red color ANSI escape code
    else:
        print(text)

def check_for_match(output, word):
    if word in output:
        print_color(":Eth", 'green')
    else:
        print_color(":Error", 'red')

shell_command = 'ip link show enp3s0'

output = execute_shell_command(shell_command)

word_to_match = "state UP"

check_for_match(output, word_to_match)

Maybe the problem is my code. I want to add this to my i3blocks. But when i do that, its taking control of the mouse for marking, selecting. And cant be undone. I deleted the code from blocks with typing letter by letter and right clicking to check the spelling.

Last edited by jojo06 (2023-11-12 08:25:44)

Offline

#2 2023-11-11 09:06:59

seth
Member
Registered: 2012-09-03
Posts: 45,069

Re: [SOLVED]How to build file.py

Did you try to run the script?

python lame_python_program.py

There's no shebang (#!/usr/bin/python) and you need to "chmod +x" the file to make it executable and run it directly

./lame_python_program.py

This has probably more to do w/ your i3blocks config

Offline

#3 2023-11-11 09:17:18

jojo06
Member
Registered: 2023-11-04
Posts: 113

Re: [SOLVED]How to build file.py

I ran it, I already gave chmod authorization, it shows as match in the output. And i added it to blocks like other plugins.

[eth]
command=~/home/$USER/.config/scripts/file.py
LABEL=
interval=30

Could the problem be the interval or its value?

Edit: I just tried it in golang. There's nothing on the bar. So it's not a build or python problem. If I can access the i3status code, there is working code there. I can use it, but where is it?

Last edited by jojo06 (2023-11-11 09:25:24)

Offline

#4 2023-11-11 09:40:43

seth
Member
Registered: 2012-09-03
Posts: 45,069

Re: [SOLVED]How to build file.py

You may want to have a closer look at that path.
Eg. try to "stat" it.

Offline

#5 2023-11-11 11:03:59

jojo06
Member
Registered: 2023-11-04
Posts: 113

Re: [SOLVED]How to build file.py

If '~' is what you are talking about, I deleted it before the last edit and tried that too. The result is still the same. I also refreshed with mod+shift+r.

Offline

#6 2023-11-11 12:42:28

seth
Member
Registered: 2012-09-03
Posts: 45,069

Re: [SOLVED]How to build file.py

stat /home/$USER/.config/scripts/file.py

Then use the hardcoded path, in case i3blocks doesn't resolve $USER

I ran it

"how"? Since there's no shebang in the posted file, you'd have to explicitly invoke python here.

command=python /home/$USER/.config/scripts/file.py

Offline

#7 2023-11-11 13:26:58

jojo06
Member
Registered: 2023-11-04
Posts: 113

Re: [SOLVED]How to build file.py

I also tried replacing $USER with the one shown in the stat field (my own username). The result is the same, the others work this way. I can run go with ./file and python with python file.py from the terminal and get output successfully. I edited command=python, but it still doesn't show up in blocks.

If the file is not related to i3de or linux. If it is an operator problem (up to me), I can change the topic to solved. Let's not tire you out for a little luxury.

Offline

#8 2023-11-11 13:29:13

seth
Member
Registered: 2012-09-03
Posts: 45,069

Re: [SOLVED]How to build file.py

it still doesn't show up in blocks

Do you still get

its taking control of the mouse for marking, selecting. And cant be undone

?

Please post the entire script and your entire i3blocks config.

Offline

#9 2023-11-11 14:17:53

jojo06
Member
Registered: 2023-11-04
Posts: 113

Re: [SOLVED]How to build file.py

Yes, but fortunately I can exit immediately with control+x.
I improved the code a bit more and wrote it in go language:

package main

import (
	"fmt"
	"os/exec"
	"strings"
)

func executeShellCommand(command string) string {
	out, err := exec.Command("bash", "-c", command).Output()
	if err != nil {
		return fmt.Sprintf("Error: %s", err)
	}
	return strings.TrimSpace(string(out))
}

func checkForMatch(output, word string) {
	if strings.Contains(output, word) {
		if word == "tun1" {
			printColor("ETH_Vpn: ON", "green")
		} else if word == "state UP" {
			printColor("Wifi: "+"Connected", "green")
		} else {
			printColor("Error", "red")
		}
	}
}

func printColor(text, color string) {
	colorCode := map[string]string{
		"green": "\033[32m%s\033[0m",
		"red":   "\033[31m%s\033[0m",
	}

	if code, exists := colorCode[color]; exists {
		fmt.Printf(code, text)
	} else {
		fmt.Println(text)
	}
}

func main() {
	// Replace 'your_shell_command' with the command you want to execute in the shell
	shellCommand := "ip link"
	output := executeShellCommand(shellCommand)
	shellCommandWifi := "ip link show wlp0s20f0u6"
	outputwifi := executeShellCommand(shellCommandWifi)

	//eth := "enp3s0" // Replace 'specific_word' with the word you want to match
	wifi := "state UP"
	vpn := "tun1"

	//checkForMatch(output, eth)
	checkForMatch(output, vpn)
	checkForMatch(outputwifi, wifi)
}

i3blocks.conf:

# i3blocks configuration file
#
# The i3blocks man page describes the usage of the binary,
# and its website describes the configuration:
#
#     https://vivien.github.io/i3blocks


# Global properties
separator=true
separator_block_width=15

[internet]
command=/home/guns/.config/scripts/net
LABEL=
interval=30

[vol1]
command=/home/guns/.config/scripts/volume1
LABEL=♪ 
interval=once
signal=1
#STEP=5%
#MIXER=default
#SCONTROL=[determined automatically]
#NATURAL_MAPPING=0

[arch-update]
command=/home/$USER/.config/scripts/arch-update
interval=3600
markup=pango
LABEL=

[memory]
command=/home/$USER/.config/scripts/memory
label=
interval=30

[disk]
command=/home/$USER/.config/scripts/disk
LABEL=
#DIR=$HOME
#ALERT_LOW=10
interval=30

[disk2]
command=/home/$USER/.config/scripts/disk2
LABEL=
instance=/
#ALERT_LOW=10
interval=30

[time]
command=date '+%Y/%m/%d %H:%M'
interval=1

Edit personal: Also unfortunately the interface wlp0sblabla instead of a clean wlan0. I understand better why you insist that we have to install from the wiki, the interfaces mentioned on the internet are always wlan0. Pure! There is also the wifi problem, after searching on the internet for solution; I realized that I have to install not only network-manager but about 5or10(?) other things just for the internet installation. Is it true? iwctl, systemd-resolve, nmcli, iw dev and such...

Last edited by jojo06 (2023-11-11 14:23:20)

Offline

#10 2023-11-11 14:39:41

seth
Member
Registered: 2012-09-03
Posts: 45,069

Re: [SOLVED]How to build file.py

Also unfortunately the interface wlp0sblabla instead of a clean wlan0

https://wiki.archlinux.org/title/Networ … face_names

I realized that I have to install not only network-manager but about 5or10(?) other things just for the internet installation. Is it true?

No. iwctl is part of iwd, but NM defaults to wpa_supplicant, systemd-resolved is part of systemd, nmcli is a CLI tool to interact w/ NM, it's good to have but not stricly required. iw is likewise good to have but not required by NM.

There's still no shebang, "shellCommandWifi := "ip link show wlp0s20f0u6"" will only get you the output for that device, but you seem to test it against some eth0, tun and vpn devices.
This is kinda nonsense and only works because the generic "state UP".
"package main" will get you a syntax error, liekwise the import array - did you actually ever test the script you posted in #1 ?

Offline

#11 2023-11-11 15:05:13

jojo06
Member
Registered: 2023-11-04
Posts: 113

Re: [SOLVED]How to build file.py

Yes smile I know it sounds ridiculous, but I wanted something simple anyway. As long as State UP is up, it shows me that I am connected to the internet on the bottom right. There are a lot of codes on the internet that find it by pinging, dns test in it, etc. I chose the simplest and the one I wrote myself. It works normally. But then it wasn't usable. Or if it has something to do with 'shebang', I'll look it up on the internet and find out. If you say it's solved, than its solved chief?

Offline

#12 2023-11-11 15:25:27

seth
Member
Registered: 2012-09-03
Posts: 45,069

Re: [SOLVED]How to build file.py

As long as State UP is up, it shows me that I am connected to the internet

No, but that's really only tangential to the problem at hand.

If you say it's solved, than its solved chief?

What?

The "shebang" is the first line of the file that says which interpreter to use - in your case "#!/usr/bin/python"
Also, again, the script in #9 is bogus and will absolutely not work.

Offline

#13 2023-11-11 16:52:12

jojo06
Member
Registered: 2023-11-04
Posts: 113

Re: [SOLVED]How to build file.py

I understand, I mean, I said that if there is no problem when I fix these things, then it is solved. So when I find a proper code from the internet, there will be no problem, right?

Offline

#14 2023-11-11 19:33:23

seth
Member
Registered: 2012-09-03
Posts: 45,069

Re: [SOLVED]How to build file.py

"Find proper code from the internet" is among the famous last words before your system entered a botnet.
Don't run random stuff from the internet if you cannot gage the source or the actual code.

Again: the code in your OP would actually run and if you're not familiar enough with python, the function can easily be achieved w/ sed/grep/awk and/or a simplistic bash script.

Offline

#15 2023-11-11 20:56:52

jojo06
Member
Registered: 2023-11-04
Posts: 113

Re: [SOLVED]How to build file.py

Python, as you say, is quite easy, I can edit it. Then I have to work on the code.

Botnet ? Wow, I didn't know it could be written so easily and in short codes. Even when I copy it on the internet, of course I look at the code, to learn it and see what it says.
But it's good that you said that. I will look at it much more carefully from now on. I also like the fact that you know about this stuff and that there are people in the forum who know. Is it okay to talk about it in the forum?

Last edited by jojo06 (2023-11-11 20:57:17)

Offline

#16 2023-11-11 21:32:26

seth
Member
Registered: 2012-09-03
Posts: 45,069

Re: [SOLVED]How to build file.py

You can enter a botnet w/ a bad curl-to-bash/eval command and you can talk about any legal action concerning your arch system.
"Is it dangerous and could I become part of a botnet by executing random code from the internet" is a question with an obvious answer, but ok to ask.
"How do I write a linux botnet" is criminal solicitation and would be dustbinned immediately and you'd probably get warned once to not do that again and then banned on repetition.

Offline

#17 2023-11-12 07:41:24

jojo06
Member
Registered: 2023-11-04
Posts: 113

Re: [SOLVED]How to build file.py

I see, I guess I can start a thread about how the botnet works, how to implement it in Linux/Windows(eww) and how it can be misused?
I can deal with it as much as I want and ask questions as long as I want, except in a non-activated situation, right? I thought everything related to hacking was forbidden.
I mean, everything is allowed except how to write a botnet (asking for code and vulnerabilities) and/or 'examine how this botnet I wrote works'.

Offline

#18 2023-11-12 08:16:31

seth
Member
Registered: 2012-09-03
Posts: 45,069

Re: [SOLVED]How to build file.py

I see, I guess I can start a thread about how the botnet works, how to implement it in Linux/Windows(eww) and how it can be misused?

No.

except how to write a botnet

and the question needs to focus on your arch system and eg. whether your actions might endanger it.

Some mod will let you know if you stepped over bounds, but unless you engage in criminal efforts, you'll not get immediately banned for such.

Offline

#19 2023-11-12 08:25:24

jojo06
Member
Registered: 2023-11-04
Posts: 113

Re: [SOLVED]How to build file.py

Thanks, understood.

Offline

#20 2023-11-12 08:44:38

dogknowsnx
Member
Registered: 2021-04-12
Posts: 547

Re: [SOLVED]How to build file.py

seth wrote:

the question needs to focus on your arch system

OP does not have an arch system, afaik.

@jojo06 Posting (AI-generated) scripts from the internet and asking the community to translate them for you is not what these forums are about, especially since it's the opposite of DIY (do-it-yourself)...


Notifications for Arch Linux package updates
Wayland.

"We are eternal, all this pain is an illusion" - Maynard James Keenan

Offline

#21 2023-11-12 15:55:07

jojo06
Member
Registered: 2023-11-04
Posts: 113

Re: [SOLVED]How to build file.py

dogknowsnx wrote:
seth wrote:

the question needs to focus on your arch system

OP does not have an arch system, afaik.

@jojo06 Posting (AI-generated) scripts from the internet and asking the community to translate them for you is not what these forums are about, especially since it's the opposite of DIY (do-it-yourself)...

Why do you think I don't have an Arch system?

I wrote the code myself by searching the internet. Then I asked AI why it doesn't work (instead of running directly to the forum and starting a thread). And no, I didn't ask anyone to translate the code for me. I asked why it works in bash/terminal but not in i3block. But I'll pay more attention.

Offline

#23 2023-11-12 16:39:15

jojo06
Member
Registered: 2023-11-04
Posts: 113

Re: [SOLVED]How to build file.py

Yes, while I'm at it, I'd like to ask. I saw in the forum rules that "If you don't install as Arch install and from the wiki, you are not using Arch". How can this be? Same OS and same installation. What exactly does this mean?

I didn't use the quick install script, I didn't ask someone else to install it tho, but it's still forbidden to install from youtube. Since this is my third installation, I wanted to do it fully and completely. It's much better and more stable than last time. After I learn a bit more I will make a final and complete installation smile and I will install it completely myself. Unfortunately the wiki is very complicated and there are a lot of redirection links

Offline

#24 2023-11-12 16:46:18

seth
Member
Registered: 2012-09-03
Posts: 45,069

Re: [SOLVED]How to build file.py

Arch is about you understanding the system, not mindlessly following some dingbat on youtube.
There's no point in the latter - in the best of cases you end up with a system that boots, but that you cannot maintain or adjust.
You want some more prefabricated system like ubuntu in this case, where many decisions are constantly made for you.

Offline

#25 2023-11-12 16:49:27

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,564

Re: [SOLVED]How to build file.py

jojo06 wrote:

....and same installation. What exactly does this mean?

First off, many (most) third part install scripts are wrong or are out of date.

Second, If one does not follow the installation in the wiki, we have no idea how your system is configured.  And neither do you; for example the naming of your wireless NIC.  You clearly did not follow our installation.  As you did not understand the steps you were following, you don't know, and therefore you cannot tell us what you did or why.

As such, no one knows how your system is set up, and we are expected to play 20 questions to try to figure it out.  Again.

Edit: Snaked by Seth.  Again.

Last edited by ewaller (2023-11-12 16:50:16)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

Board footer

Powered by FluxBB