Need some advises on how I can extract values from output execution of command. Snippet output below. The output generates so much info, but I just need to extract the value of machine, state and address as per snippet below.
I would like to have the output that have list of machine, state and address
machine state address
0 started 1.9.10.34
0/kvm/0 started 1.9.10.21
xxxxx xxxxxxx xxxxxxx
This is the code I used.
for line in stdout:
line = line.strip()
if not line:
continue
#if line.startswith("0"):
machine_id, state, instance_id = line.split()[0:3]
print(f"Machine ID: {machine_id}, State: {state}, Address: {address}")
f.write(f"Machine ID: {machine_id}, State: {state}, Address:
{address}\n")
Please advise how can I only extract the info related to machine, state and address only. Thank you.
Related
I need to display the ip address after address: . Everything else needs to be trimmed. What code will most optimally solve my problem?
show interface PPPoE0
id: PPPoE0
index: 0
type: PPPoE
description: Internet (NetFriend)
interface-name: PPPoE0
link: up
connected: yes
state: up
mtu: 1400
tx-queue: 1000
address: 46.42.50.121
mask: 255.255.255.255
global: yes
defaultgw: yes
priority: 1000
security-level: public
auth-type: PAP, CHAP, MS-CHAP, MS-CHAPv2
remote: 46.42.48.1
uptime: 45562
session-id: 23430
fail: no
via: GigabitEthernet0/Vlan2
last-change: 45562.183918
(config)> exit
Core::Configurator: Bye.
You can use a simple Regular Expression to search for all matching IPs in a string.
The regex query for an IP Adress would be : (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})If you want to learn what the RegEx query does you can see the full explanation here.
The code would look something like
import re
text = """
show interface PPPoE0
id: PPPoE0
index: 0
type: PPPoE
description: Internet (NetFriend)
interface-name: PPPoE0
link: up
connected: yes
state: up
mtu: 1400
tx-queue: 1000
address: 46.42.50.121
mask: 255.255.255.255
..................
"""
regex_pattern_ip = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
ip = regex_pattern_ip.search(text)[0]
print(ip)
Hope this helped
I don't know about most optimal, but one way to do it is to split by lines, find the one containing address, and extract the text from it:
def getIp(text):
lines = text.split("\n")
for line in lines:
line = line.replace(" ").replace("\t")#in case it has newlines or spaces in front, not sure based off question
if line.startswith("address:"):
return line[8:]
raise Exception("Address line not found")
Maybe not optimal, but will work:
print(s[s.find("address: ") + 9: s.find("mask")].strip())
The problem: how to fix this warning: [ Expert Info (Warning/Sequence): No response seen to ICMP request]
The story: I'm playing with scapy. I have two tools: A is the sender ( which sends a echo-request ) and B is the recipient ( which sends a echo-reply AFTER it sniffs a echo-request ). The code of A:
....
pinger = IP(dst=x.x.x.x)/ICMP(id=0x7ccb, seq=1)/text
....
The code of B:
.....
sniff(filter=f"src host x.x.x.x and dst host x.x.x.x and icmp",
iface="XX", count=1)
send(IP(dst=x.x.x.x)/ICMP(type="echo-reply", id=0x7ccb, seq=1)/textback)
....
The result:
enter image description here
It appears that if the payloads ( text of A and textback of B ) are the same, this warning does not get displayed. However, for my goal, the text shall be different. So, how do I get rid of this warning while keeping both payloads not the same?
The data received in the echo message must be returned in the echo reply message.
I am trying to mint a token in cardano mainnet. I have built a block and staking pool. I am working to mint a token and i am running into an error "unexpected '2', expecting space, "+" or end of input.
Here is the linux code I'm running:
cardano-cli transaction build-raw --shelley-era --fee $fee --tx-in $txhash#$txix --tx-out $address+$output+"$tokenamount $policyid.$tokenname1" --mint="$tokenamount $policyid.$tokenname1" --minting-script-file policy/policy.script --out-file matx.raw
Here is the error:
option --tx-out:
unexpected '2'
expecting space, "+" or end of input
Inputs:
I have tried different outputs of 10000000, 5000000, and 0.
$tokenamount="10000000"
$address=$(cat payment.addr)
$tokenname1="CpoolTest"
https://developers.cardano.org/docs/native-tokens/minting/
Please help
I guess i found the error.
Check $policyid whats inside. It should contain only 1x adress.
Try echo $policyid. It should only display 1 address
If its not the case, yo can try:
to delete your policyID:
rm -rf policy/policyID
After deleting create a brand new one:
cardano-cli transaction policyid --scriptfile ./policy/policy.script >> policy/policyID
Now set the variable:
policyid=$(cat policy/policyID)
Echo it
echo $policyid
There should be exactly 1 address displayed. Your code should work now
So, a brief description of what I want, what my issue is, and what I have tried.
I want to declare and use a dictionary variable for my tests in pyrest, specifically for the [url, body] section so that I can conduct my POST tests targeting a specific endpoint and with a preformatted body.
Here is how mytest.yml file is structured:
- data:
- id: 63
- rate: 25
... a sizable set of field for reasons ...
- type: lab_test_authorization
- modified_at: ansible_date_time.datetime # Useful way to generate
- test:
- url: "some-valid-url/{the_url_question}" # data['special_key']
- method: 'POST'
- headers : {etc..etc}
- body: '{ "data": ${the_body_question} }' # data (the content)
Now the problem starts in my lack of understanding why (if true) does pyrest does not have support for dictionary mappings. I understand yaml supports these feature but am not sure if pyrest can parse through it. Knowing how to call and use dictionary variable in my url and body tags would be significantly helpful.
As of right now, if I try to convert my data Sequence into a data Dictionary, I will get an error stating:
yaml.parser.ParserError: while parsing a block mapping
in "<unicode string>", line 4, column 1:
data:
^
expected <block end>, but found '-'
in "<unicode string>", line 36, column 1:
- config:
I'm pretty sure there are gaps in my knowledge regarding how yaml and pyresttest interact with each other, so any insight would be greatly appreciated.
I wrote a program using c# that list all running process in window, i want to list all running process in window, and in each process, i want to list all running thread (both name and id). i can't find any function on Window Api to list thread name, how can i do it ?
Example: plz look at this picture:
lh4.googleusercontent.com/HwP6dpts5uRPJIElH7DgUd3x95aQKO36tynkfsaDMBbM=w607-h553-no
in the image, i want to list
FireFox ID: 123
Google Chorme ID 456
...
Explorer ID 789
Documents ID 654
Temp ID 231
...
Thankyou !
You can use the Systems.Diagnostic namespace and then use:
Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist){
Console.WriteLine(“Process: {0} ID: {1}”, theprocess.ProcessName, theprocess.Id);
}
Source
More info