\n not working in the output - python

Hi I currently have an output of:
'root:$6$aYGtvxKp/bl6Fv2y$sdZ3FbdJYQlP8VcfFZT.Y67We5EQmqcHW4I9Gl/3pXp8v4.nu9qMIEkmOcdRuD0lBTvEtnMHosEo7OEaYgG4E0::0:99999:7:::\nbin::17110:0:99999:7:::\ndaemon::17110:0:99999:7:::\nadm::17110:0:99999:7:::\nlp::17110:0:99999:7:::\nsync::17110:0:99999:7:::\nshutdown::17110:0:99999:7:::\nhalt::17110:0:99999:7:::\nmail::17110:0:99999:7:::\noperator::17110:0:99999:7:::\ngames::17110:0:99999:7:::\nftp::17110:0:99999:7:::\nnobody::17110:0:99999:7:::\nsystemd-bus-proxy:!!:17572::::::\nsystemd-network:!!:17572::::::\ndbus:!!:17572::::::\npolkitd:!!:17572::::::\ntss:!!:17572::::::\nsshd:!!:17572::::::\npostfix:!!:17572::::::\nchrony:!!:17572::::::\funky:$1$EgZiG263$4W/wMljYzhOqnupg9cJ7W/:17599:0:99999:7:::\n'
From my code:
command = "cat /etc/shadow "
process = os.popen(command)
results = str(process.read())
I'm trying to make it look like the one in the command prompt wherein it is in a table form but for some reason when I transfer it to python it does do the new line function "\n" does not work. What is wrong with my code?

You should probably just read the file directly:
filename = '/etc/shadow'
with open(filename) as shadowfile:
content = shadowfile.read()
# or possibly lines = shadowfile.readlines()
Did you try printing the output or did you just see the contents of the results variable in the interpreter? In the latter case, the line breaks will be shown as \p, while print(results) will produce the results you expect.

l = 'root:$6$aYGtvxKp/bl6Fv2y$sdZ3FbdJYQlP8VcfFZT.Y67We5EQmqcHW4I9Gl/3pXp8v4.nu9qMIEkmOcdRuD0lBTvEtnMHosEo7OEaYgG4E0::0:99999:7:::\nbin::17110:0:99999:7:::\ndaemon::17110:0:99999:7:::\nadm::17110:0:99999:7:::\nlp::17110:0:99999:7:::\nsync::17110:0:99999:7:::\nshutdown::17110:0:99999:7:::\nhalt::17110:0:99999:7:::\nmail::17110:0:99999:7:::\noperator::17110:0:99999:7:::\ngames::17110:0:99999:7:::\nftp::17110:0:99999:7:::\nnobody::17110:0:99999:7:::\nsystemd-bus-proxy:!!:17572::::::\nsystemd-network:!!:17572::::::\ndbus:!!:17572::::::\npolkitd:!!:17572::::::\ntss:!!:17572::::::\nsshd:!!:17572::::::\npostfix:!!:17572::::::\nchrony:!!:17572::::::\funky:$1$EgZiG263$4W/wMljYzhOqnupg9cJ7W/:17599:0:99999:7:::\n'
for i in l.split('\n'):
print(i)
Output:
root:$6$aYGtvxKp/bl6Fv2y$sdZ3FbdJYQlP8VcfFZT.Y67We5EQmqcHW4I9Gl/3pXp8v4.nu9qMIEkmOcdRuD0lBTvEtnMHosEo7OEaYgG4E0::0:99999:7:::
bin::17110:0:99999:7:::
daemon::17110:0:99999:7:::
adm::17110:0:99999:7:::
lp::17110:0:99999:7:::
sync::17110:0:99999:7:::
shutdown::17110:0:99999:7:::
halt::17110:0:99999:7:::
mail::17110:0:99999:7:::
operator::17110:0:99999:7:::
games::17110:0:99999:7:::
ftp::17110:0:99999:7:::
nobody::17110:0:99999:7:::
systemd-bus-proxy:!!:17572::::::
systemd-network:!!:17572::::::
dbus:!!:17572::::::
polkitd:!!:17572::::::
tss:!!:17572::::::
sshd:!!:17572::::::
postfix:!!:17572::::::
chrony:!!:17572:::::: unky:$1$EgZiG263$4W/wMljYzhOqnupg9cJ7W/:17599:0:99999:7:::

Related

extract a block of lines from a command output in python

I have a command output like following which has block of encrypted codes. How to extract and assign the contents below block: > to a variable. Space before all the lines has to be maintained for the variable. As I am new to python, not sure how to proceed.
HlOJsN8A2NSOQGAmhKy30qi7qiIE6qponUZmwJ6fUzFDckBbOMydCui6uY51o91bE4XM8ci+sGqrB6Hvl6JWXASkT7rTLHzpFUTk3ii86aR209bZb8nkEsMT4KQEISSHeJ+NAkGZkbnvISWCwWLVv0wLxAW22DA11C/9cV/fOIrO6VZGzIM69rguMSGObeUKLgDledCf1mFii8WhCA8EChnN3hh4M5RxyCeZhsVSpy3AbkRnMRN2e4T2ISQqblH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Hb7i4+TU0DoVc4aSnCNlgm4pKz
vbnbnbjfbjfbjbjfjkhdjhjkfhjkfjkhfjkhjkdhjkhjshwhwjhjkwhjkwhh
t/q6gBBA9Y/R7C0tFSl2FxfDUa/I]"
In python, you should use """multi_line_str""" if you dont want to see "\n" or "\t" in your string:
origin_str = """block: >
ENC[PKCS7,MIIBeQYJKoZIhvcNAQcDoIIBajCCAWYCAQAxggEhMIIBHQIBADAFMAACAQEw
DQYJKoZIhvcNAQEBBQAEggEAtcju4aRfjIng9zC/aXQTWYPF/6L8LZufO9GP
fjhjfhjhjfhjfhjhfjhjhjhfjhjfhjfhjhcagfjgfjfhjkhE6qponUZmwJ6f
UzFDckBbOMydCui6uY51o91bE4XM8ci+sGqrB6Hvl6JWXASkT7rTLHzpFUTk
3ii86aR209bZb8nkEsMT4KQEISSHeJ+NAkGZkbnvISWCwWLVv0wLxAW22DA1
fgjfkhjkfhkjhfjkhjkfhjkhfjkhjkfljlkfhfhkjfhjkhfjkfhjkhfjkhfh
M5RxyCeZhsVSpy3AbkRnMRN2e4T2ISQqblHb7i4+TU0DoVc4aSnCNlgm4pKz
vbnbnbjfbjfbjbjfjkhdjhjkfhjkfjkhfjkhjkdhjkhjshwhwjhjkwhjkwhh
t/q6gBBA9Y/R7C0tFSl2FxfDUa/I]"""
what_you_want_str = origin_str.split("block: >\n")[1].strip()
what_you_want_str
'ENC[PKCS7,MIIBeQYJKoZIhvcNAQcDoIIBajCCAWYCAQAxggEhMIIBHQIBADAFMAACAQEw\n DQYJKoZIhvcNAQEBBQAEggEAtcju4aRfjIng9zC/aXQTWYPF/6L8LZufO9GP\n fjhjfhjhjfhjfhjhfjhjhjhfjhjfhjfhjhcagfjgfjfhjkhE6qponUZmwJ6f\n UzFDckBbOMydCui6uY51o91bE4XM8ci+sGqrB6Hvl6JWXASkT7rTLHzpFUTk\n 3ii86aR209bZb8nkEsMT4KQEISSHeJ+NAkGZkbnvISWCwWLVv0wLxAW22DA1\n fgjfkhjkfhkjhfjkhjkfhjkhfjkhjkfljlkfhfhkjfhjkhfjkfhjkhfjkhfh\n M5RxyCeZhsVSpy3AbkRnMRN2e4T2ISQqblHb7i4+TU0DoVc4aSnCNlgm4pKz\n vbnbnbjfbjfbjbjfjkhdjhjkfhjkfjkhfjkhjkdhjkhjshwhwjhjkwhjkwhh\n t/q6gBBA9Y/R7C0tFSl2FxfDUa/I]'
print(what_you_want_str)
output:
ENC[PKCS7,MIIBeQYJKoZIhvcNAQcDoIIBajCCAWYCAQAxggEhMIIBHQIBADAFMAACAQEw
DQYJKoZIhvcNAQEBBQAEggEAtcju4aRfjIng9zC/aXQTWYPF/6L8LZufO9GP
fjhjfhjhjfhjfhjhfjhjhjhfjhjfhjfhjhcagfjgfjfhjkhE6qponUZmwJ6f
UzFDckBbOMydCui6uY51o91bE4XM8ci+sGqrB6Hvl6JWXASkT7rTLHzpFUTk
3ii86aR209bZb8nkEsMT4KQEISSHeJ+NAkGZkbnvISWCwWLVv0wLxAW22DA1
fgjfkhjkfhkjhfjkhjkfhjkhfjkhjkfljlkfhfhkjfhjkhfjkfhjkhfjkhfh
M5RxyCeZhsVSpy3AbkRnMRN2e4T2ISQqblHb7i4+TU0DoVc4aSnCNlgm4pKz
vbnbnbjfbjfbjbjfjkhdjhjkfhjkfjkhfjkhjkdhjkhjshwhwjhjkwhjkwhh
t/q6gBBA9Y/R7C0tFSl2FxfDUa/I]

python nesting loops

I am trying perform a nested loop to combine data into a line by using matched MAC Addresses in both files.
I am able to pull the loop fine without the regex, however, when using the search regex below, it will only loop through the MAC_Lines once and print the correct results using the first entry in the MAC_Lines and stop. I'm unsure how to make the MAC_Lines go to the next line and repeat the process for all of the entries in the MAC_Lines.
try:
for mac in MAC_Lines:
MAC_address = re.search(r'([a-fA-F0-9]{2}[:|\-]?){6}', mac, re.I)
MAC_address_final = MAC_address.group()
for arp in ARP_Lines:
ARP_address = re.search(r'([a-fA-F0-9]{2}[:|\-]?){6}', arp, re.I)
ARP_address_final = ARP_address.group()
if MAC_address_final == ARP_address_final:
print mac + arp
continue
except Exception:
print 'completed.'
Results:
13,64,00:0c:29:36:9f:02,giga-swx 0/213,172.20.13.70, 00:0c:29:36:9f:02, vlan 64
completed.
I learned that the issue was how I opened the file. I should have used the 'open':'as' keywords when opening both files to allow the files to properly close and reopen for the next loop. Below is the code I was looking for.
Below is the code:
with open('MAC_List.txt', 'r') as read0:for items0 in read0:
MAC_address = re.search(r'([a-fA-F0-9]{2}[:|\-]?){6}', items0, re.I)
if MAC_address:
mac_addy = MAC_address.group().upper()
with open('ARP_List.txt', 'r') as read1:
for items1 in read1:
ARP_address = re.search(r'([a-fA-F0-9]{2}[:|\-]?){6}', items1, re.I)
if ARP_address:
arp_addy = ARP_address.group()
if mac_addy == arp_addy:
print(items0.strip() + ' ' + items1.strip())

Python: Having trouble replacing lines from file

I'm trying to build a translator using deepl for subtitles but it isn't running perfectly. I managed to translate the subtitles and most of the part I'm having problems replacing the lines. I can see that the lines are translated because it prints them but it doesn't replace them. Whenever I run the program it is the same as the original file.
This is the code responsible for:
def translate(input, output, languagef, languaget):
file = open(input, 'r').read()
fileresp = open(output,'r+')
subs = list(srt.parse(file))
for sub in subs:
try:
linefromsub = sub.content
translationSentence = pydeepl.translate(linefromsub, languaget.upper(), languagef.upper())
print(str(sub.index) + ' ' + translationSentence)
for line in fileresp.readlines():
newline = fileresp.write(line.replace(linefromsub,translationSentence))
except IndexError:
print("Error parsing data from deepl")
This is the how the file looks:
1
00:00:02,470 --> 00:00:04,570
- Yes, I do.
- (laughs)
2
00:00:04,605 --> 00:00:07,906
My mom doesn't want
to babysit everyday
3
00:00:07,942 --> 00:00:09,274
or any day.
4
00:00:09,310 --> 00:00:11,977
But I need
my mom's help sometimes.
5
00:00:12,013 --> 00:00:14,046
She's just gonna
have to be grandma today.
Help will be appreaciated :)
Thanks.
You are opening fileresp with r+ mode. When you call readlines(), the file's position will be set to the end of the file. Subsequent calls to write() will then append to the file. If you want to overwrite the original contents as opposed to append, you should try this instead:
allLines = fileresp.readlines()
fileresp.seek(0) # Set position to the beginning
fileresp.truncate() # Delete the contents
for line in allLines:
fileresp.write(...)
Update
It's difficult to see what you're trying to accomplish with r+ mode here but it seems you have two separate input and output files. If that's the case consider:
def translate(input, output, languagef, languaget):
file = open(input, 'r').read()
fileresp = open(output, 'w') # Use w mode instead
subs = list(srt.parse(file))
for sub in subs:
try:
linefromsub = sub.content
translationSentence = pydeepl.translate(linefromsub, languaget.upper(), languagef.upper())
print(str(sub.index) + ' ' + translationSentence)
fileresp.write(translationSentence) # Write the translated sentence
except IndexError:
print("Error parsing data from deepl")

How to remove brackets and the contents inside from a file

I have a file named sample.txt which looks like below
ServiceProfile.SharediFCList[1].DefaultHandling=1
ServiceProfile.SharediFCList[1].ServiceInformation=
ServiceProfile.SharediFCList[1].IncludeRegisterRequest=n
ServiceProfile.SharediFCList[1].IncludeRegisterResponse=n
Here my requirement is to remove the brackets and the integer and enter os commands with that
ServiceProfile.SharediFCList.DefaultHandling=1
ServiceProfile.SharediFCList.ServiceInformation=
ServiceProfile.SharediFCList.IncludeRegisterRequest=n
ServiceProfile.SharediFCList.IncludeRegisterResponse=n
I am quite a newbie in Python. This is my first attempt. I have used these codes to remove the brackets:
#!/usr/bin/python
import re
import os
import sys
f = os.open("sample.txt", os.O_RDWR)
ret = os.read(f, 10000)
os.close(f)
print ret
var1 = re.sub("[\(\[].*?[\)\]]", "", ret)
print var1f = open("removed.cfg", "w+")
f.write(var1)
f.close()
After this using the file as input I want to form application specific commands which looks like this:
cmcli INS "DefaultHandling=1 ServiceInformation="
and the next set as
cmcli INS "IncludeRegisterRequest=n IncludeRegisterRequest=y"
so basically now I want the all the output to be bunched to a set of two for me to execute the commands on the operating system.
Is there any way that I could bunch them up as set of two?
Reading 10,000 bytes of text into a string is really not necessary when your file is line-oriented text, and isn't scalable either. And you need a very good reason to be using os.open() instead of open().
So, treat your data as the lines of text that it is, and every two lines, compose a single line of output.
from __future__ import print_function
import re
command = [None,None]
cmd_id = 1
bracket_re = re.compile(r".+\[\d\]\.(.+)")
# This doesn't just remove the brackets: what you actually seem to want is
# to pick out everything after [1]. and ignore the rest.
with open("removed_cfg","w") as outfile:
with open("sample.txt") as infile:
for line in infile:
m = bracket_re.match(line)
cmd_id = 1 - cmd_id # gives 0, 1, 0, 1
command[cmd_id] = m.group(1)
if cmd_id == 1: # we have a pair
output_line = """cmcli INS "{0} {1}" """.format(*command)
print (output_line, file=outfile)
This gives the output
cmcli INS "DefaultHandling=1 ServiceInformation="
cmcli INS "IncludeRegisterRequest=n IncludeRegisterResponse=n"
The second line doesn't correspond to your sample output. I don't know how the input IncludeRegisterResponse=n is supposed to become the output IncludeRegisterRequest=y. I assume that's a mistake.
Note that this code depends on your input data being precisely as you describe it and has no error checking whatsoever. So if the format of the input is in reality more variable than that, then you will need to add some validation.

python reading lines in file and joining them

I have this code
with open ('ip.txt') as ip :
ips = ip.readlines()
with open ('user.txt') as user :
usrs = user.readlines()
with open ('pass.txt') as passwd :
passwds = passwd.readlines()
with open ('prefix.txt') as pfx :
pfxes = pfx.readlines()
with open ('time.txt') as timer :
timeout = timer.readline()
with open ('phone.txt') as num :
number = num.readline()
which open all those files and join them in this shape
result = ('Server:{0} # U:{1} # P:{2} # Pre:{3} # Tel:{4}\n{5}\n'.format(b,c,d,a,number,ctime))
print (result)
cmd = ("{0}{1}#{2}".format(a,number,b))
print (cmd)
I supposed it will print like this
Server:x.x.x.x # U:882 # P:882 # Pre:900 # Tel:456123456789
900456123456789#x.x.x.x
but the output was like this
Server:x.x.x.x
# U:882 # P:882 # Pre:900
# Tel:456123456789
900
456123456789#187.191.45.228
New output :-
Server:x.x.x.x # U:882 # P:882 # Pre:900 # Tel:['456123456789']
900['456123456789']#x.x.x.x
how i can solve this ?
may be you should remove newline using strip()
Example
with open ('ip.txt') as ip :
ips = ip.readline().strip()
readline() will read one line at a time, where readlines() will read entire files as a list of lines
I am guessing from your limited example is that b has a newline embedded. That's because of readlines(). The python idiom to use here is: ip.read().splitlines() where ip is one of your file handles.
See more splitlines options at python docs
Apart from other great answers, for completeness sake here I am going to post an alternative answer using string.translate, which will cover in case of any \n or newline has been accidentally inserted into middle of your string, like '123\n456\n78', which will cover the corner cases from using rstrip or strip.
Server:x.x.x.x # U:882 # P:882 # Pre:900 # Tel:['456123456789']
900['456123456789']#x.x.x.x
You have this is because you're printing a list, to resolve this, you need to join the string in your list number
Altogether, solution will be something Like this:
import string
# prepare for string translation to get rid of new lines
tbl = string.maketrans("","")
result = ('Server:{0} # U:{1} # P:{2} # Pre:{3} # Tel:{4}\n{5}\n'.format(b,c,d,a,''.join(number),ctime))
# this will translate all new lines to ""
print (result.translate(tbl, "\n"))
cmd = ("{0}{1}#{2}".format(a,''.join(number),b))
print (cmd.translate(tbl, "\n"))

Categories

Resources