delete specific lines in txt file python - python

so I have text file file.txt e.g
daemon
nserver 1111
nserver 11111
nscache 65536
timeouts 1 5 30 60 180 1800 15 60
log /var/log/3proxy/log D
logformat "- +_L%t.%. %N.%p %E %U %C:%c %R:%r %O %I %h %T"
logformat "L%t.%. %N.%p %E %U %C:%c %R:%r %Q:%q %O %I %h %T %n "
rotate 360
#external 0.0.0.0
#internal 0.0.0.0
auth strong
########################################################################## Ttest-for-dev ##########################################################################
users ttest-for-dev:CL:d4ec7bf6
allow login,ttest-for-dev
proxy -n -a -p8989 -i194.150.75.50 -e194.150.75.50
socks -n -a -p43434 -i194.150.75.50 -e194.150.75.50
flush
########################################################################## specuser ##########################################################################
users specuser:CL:d4ec7bf6
allow login,specuser
proxy -n -a -p8989 -i194.150.75.50 -e194.150.75.50
socks -n -a -p43434 -i194.150.75.50 -e194.150.75.50
flush
deny *
maxconn 254
setgid 65534
setuid 65534
now how can i remove the lines so that the following is left
So the output file will be
daemon
nserver 1111
nserver 11111
nscache 65536
timeouts 1 5 30 60 180 1800 15 60
log /var/log/3proxy/log D
logformat "- +_L%t.%. %N.%p %E %U %C:%c %R:%r %O %I %h %T"
logformat "L%t.%. %N.%p %E %U %C:%c %R:%r %Q:%q %O %I %h %T %n "
rotate 360
#external 0.0.0.0
#internal 0.0.0.0
auth strong
########################################################################## Ttest-for-dev ##########################################################################
users ttest-for-dev:CL:d4ec7bf6
allow login,ttest-for-dev
proxy -n -a -p8989 -i194.150.75.50 -e194.150.75.50
socks -n -a -p43434 -i194.150.75.50 -e194.150.75.50
flush
deny *
maxconn 254
setgid 65534
setuid 65534
Thank you
I have a script that adds lines, but I have no idea how to remake it to fit the conditions in the question
import sys
import fileinput
nameuser = "user1"
passuser = "pass1"
file_name = '/usr/local/etc/3proxy.cfg'
for line in fileinput.FileInput(file_name,inplace=1):
if 'auth strong' in line:
line = line.strip()
line = line.replace(line, line+'\n########################################################################## '+ nameuser +' ##########################################################################'+'\nusers '+ nameuser +':CL:'+ psuser +'\nallow login,'+ nameuser +'\nflush')
print (line, end='')

Read the file line by line. When you get to the ###... specuser line, start a loop that keeps reading until the flush line. Otherwise, print the line.
import re
with fileinput.FileInput(file_name,inplace=1) as infile:
for line in infile:
if re.match('#+ specuser #+$', line):
for nextline in infile:
if nextline.strip() == 'flush':
break
continue
print(line, end='')

Related

Why am I getting "invalid input detected" for this tn.write("ip address 1.1.1.1 255.255.255.255") line?

So this script is meant to telnet into a router and change the IP address on the given interface. However, my script runs into errors and I'm not sure why. The line that errors out is line 44.
This is my python script:
import os
import sys
import telnetlib
if (len(sys.argv) != 3):
print "syntax: python hw06.py <device> <newip>"
sys.exit()
router = sys.argv[1]
newip = sys.argv[2]
interface = "Serial0/0" # must hard code the interface to avoid disaster
TIMEOUT = 3
password1 = "user"
password2 = "cisco"
cmd = "ip address 111.11.111.11 255.255.255.0"
# 1. create a telnet object
tn = telnetlib.Telnet(router, timeout=TIMEOUT)
# 2. login/telnet to the router
tn.read_until("Password: ", TIMEOUT)
tn.write(password1 + "\n")
# 3. enter into the privilege mode
tn.write("enable\n")
tn.read_until("Password:")
tn.write(password2 + "\n")
# 4. enter into the configuration mode
tn.write("configure terminal\n")
tn.read_until("(config)#", TIMEOUT)
# 5. enter into the interface configuration mode
tn.write("int" + interface + "\n")
tn.read_until("(config-if)#", TIMEOUT)
# 6. set the new IP address
tn.write(cmd + "\r\n")
# 7. exit
# exit from the interface configruaiton mode
tn.write("exit\n")
# exit from the configuraiotn mode
tn.write("exit\n")
# exit from the privilege mode
tn.write("exit\n")
print tn.read_all() # this line is required, but not sure why?
tn.close()
oid = ".1.3.6.1.2.1.4.20.1.1"
snmp = "snmpwalk -v2c -c public %s %s" % (router, oid)
# Verify the output via SNMP
fp = os.popen( snmp )
snmp = fp.read().splitlines() # split the outout into a list of "lines"
flag = 0
for line in snmp:
inline = line.rstrip('\n')
list = inline.split()
ip = list[3] # IP address is the 4th item on the list
if ip == newip:
print "The new IP address (%s) is successfully configured on Serial0/0 of %s" % (ip, router)
flag = 1
break
if flag == 0:
print "failed operation: %s is not configured on Serial0/0 of %s" % (newip, router)
Now when I run the script, i input "python script.py deviceIPaddress newInterfaceIPaddress" this is what i get:
ip address 111.11.111.11 255.255.255.0
^
% Invalid input detected at '^' marker.
Router4(config)#exit
Router4#exit
failed operation: 111.11.111.11 is not configured on Serial0/0 of <device>
Any idea why I'm getting that invalid input error?
Thank you in advanced!

Add multiple entries of text to specific lines of code in Python

I would like to add the flag
-c 4
to my line of code at
hostname = "216.58.223.3 -c 4"
The way it currently works:
#staticmethod
def valve_southafrica_two():
print("Pinging Valve South Africa 2")
hostname = "155.133.238.163"
response = os.system("ping " + hostname)
if response == 0:
pingstatus = "Active"
else:
pingstatus = "Error"
print("Ping Test Complete")
return pingstatus
and the way I want it to work is so:
#staticmethod
def valve_southafrica_two():
print("Pinging Valve South Africa 2")
hostname = "155.133.238.163 -c 4"
response = os.system("ping " + hostname)
if response == 0:
pingstatus = "Active"
else:
pingstatus = "Error"
print("Ping Test Complete")
return pingstatus
for Linux +/ MacOS Pinging, is there any way to bulk add the -c 4 flag next to the xxx.xxx.xxx.xxx (IP) or is the only solution to manually add the -c 4 next to each line?
Use subprocess,call
By which we can send the commands as a list of arguments so we just need to specify -c 4 as an argument
import subprocess
#import os
def valve_southafrica_two():
print("Pinging Valve South Africa 2")
hostname = "155.133.238.163"
response =subprocess.call(["ping",hostname,"-c 4"])
if response == 0:
pingstatus = "Active"
else:
pingstatus = "Error"
print("Ping Test Complete")
return pingstatus
valve_southafrica_two()
OUTPUT
Pinging Valve South Africa 2
PING 155.133.238.163 (155.133.238.163) 56(84) bytes of data.
64 bytes from 155.133.238.163: icmp_seq=1 ttl=47 time=327 ms
64 bytes from 155.133.238.163: icmp_seq=2 ttl=47 time=325 ms
64 bytes from 155.133.238.163: icmp_seq=3 ttl=47 time=326 ms
64 bytes from 155.133.238.163: icmp_seq=4 ttl=47 time=325 ms
--- 155.133.238.163 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 325.113/326.192/327.601/0.992 ms
Ping Test Complete
You can use string formatting or f-strings like below
flag = '-c 4'
# python 3.6
hostname = f'155.133.238.163 {flag}'
# python 2.7
hostname = '155.133.238.163 {0}'.format(flag)
Output
155.133.238.163 -c 4

Sending "random" traffic through Mininet network

I want to test a data center routing algorithm using Mininet. The traffic needs to conform to certain parameters:
It should consist of "files" of various sizes (note that these don't actually have to be files; traffic generated in, e.g., iperf is OK, as long as the size is controllable);
The file sizes should be drawn from a particular distribution;
The source/destination host pairs between which data is sent should be selected randomly for a given file;
The interval between when a file is sent and its successor is sent should be random; and
If a huge file gets sent between two hosts that takes a long time to transfer, it should still be possible to send data between other hosts in the network.
Points 1-4 are taken care of. I've been struggling with #5 for days and I can't get it working properly. My initial thought was to spawn subprocesses/threads to send iperf commands to the hosts:
while count < 10:
if (count % 2) == 0:
host_pair = net.get("h1", "h2")
else:
host_pair = net.get("h3", "h4")
p = multiprocessing.Process(target=test_custom_iperf, args=(net, host_pair, nbytes))
p.daemon = True
p.start()
time.sleep(random.uniform(0, 1))
The command test_custom_iperf is modeled after the Python Mininet API's version of iperf to include the -n transfer size parameter:
client, server = host_pair
print client, server
output( '*** Iperf: testing TCP bandwidth between',
client, 'and', server, '\n' )
server.sendCmd( 'iperf -s' )
if not waitListening( client, server.IP(), 5001 ):
raise Exception( 'Could not connect to iperf on port 5001' )
cliout = client.cmd( 'iperf -c ' + server.IP() + ' -n %d' % nbytes )
print cliout
server.sendInt()
servout = server.waitOutput()
debug( 'Server output: %s\n' % servout)
result = [ net._parseIperf( servout ), net._parseIperf( cliout ) ]
output( '*** Results: %s\n' % result )
Making this non-blocking has been extremely difficult. I need to be able to send the server.sendInt() command, for some reason, and to do this I need to wait for the client's command to finish.
I'd appreciate any advice on what I can try to make this work!
I took a hint from here and used Mininet's host.popen() module to send the data around. Hopefully this helps someone else:
def send_one_file(file_dir, host_pair, files):
src, dst = host_pair # a tuple of Mininet node objects
# choose a random file from files
rand_fname = random.sample(files, 1)[0]
rand_path = os.path.join(file_dir, rand_fname)
port = random.randint(1024, 65535)
# Start listening at the destination host
dst_cmd = 'nc -l %d > /home/mininet/sent/%s.out' % (port, rand_fname)
print os.path.getsize(rand_path)
dst.popen( dst_cmd, shell=True )
# Send file from the source host
src_cmd = 'nc %s %s < %s' % (dst.IP(), port, rand_path)
src.popen( src_cmd, shell=True )
Then the parent function calls send_one_file() at random intervals:
def test_netcat_subprocess_async(net, duration):
file_dir = "/home/mininet/sf_mininet_vm/data/MVI_0406_split"
files = os.listdir(file_dir)
start_time = time.time()
end_time = start_time + duration
# Transfer for the desired duration
while time.time() < end_time:
# Choose a pair of hosts
host_pair = random.sample(net.hosts, 2)
test_send_one_file_netcat(file_dir, host_pair, files)
interval = random.uniform(0.01, 0.1)
print "Initialized transfer; waiting %f seconds..." % interval
time.sleep(interval)
This works without any of the problems I experienced with multiprocessing or threading (breaking the network after the session is over, blocking when it shouldn't, etc.).

ssh command with argument execution in python

I need to execute a ssh command with arguments in python. I have been able to execute the ssh command.But, I cannot figure out, how to pass the arguments.
The command:
ssh -L 22222:localhost:5434 sayan#155.97.73.252
Here is the code :
ssh = paramiko.SSHClient();
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy());
ssh.connect("155.97.73.252", username="sayan", password="#####");
paramiko Example
class RunCommand(cmd.Cmd):
""" Simple shell to run a command on the host """
prompt = 'ssh > '
def __init__(self):
cmd.Cmd.__init__(self)
self.hosts = []
self.connections = []
def do_add_host(self, args):
"""add_host
Add the host to the host list"""
if args:
self.hosts.append(args.split(','))
else:
print "usage: host "
def do_connect(self, args):
"""Connect to all hosts in the hosts list"""
for host in self.hosts:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
client.connect(host[0],
username=host[1],
password=host[2])
self.connections.append(client)
def do_run(self, command):
"""run
Execute this command on all hosts in the list"""
if command:
for host, conn in zip(self.hosts, self.connections):
stdin, stdout, stderr = conn.exec_command(command)
stdin.close()
for line in stdout.read().splitlines():
print 'host: %s: %s' % (host[0], line)
else:
print "usage: run "
def do_close(self, args):
for conn in self.connections:
conn.close()
if __name__ == '__main__':
RunCommand().cmdloop()
Example output:
ssh > add_host 127.0.0.1,jesse,lol
ssh > connect
ssh > run uptime
host: 127.0.0.1: 14:49 up 11 days, 4:27, 8 users,
load averages: 0.36 0.25 0.19
ssh > close
fabric example
from fabric import tasks
env.hosts = ['localhost', 'sunflower.heliotropic.us']
pattern = re.compile(r'up (\d+) days')
# No need to decorate this function with #task
def uptime():
res = run('uptime')
match = pattern.search(res)
if match:
days = int(match.group(1))
env['uts'].append(days)
def main():
env['uts'] = []
tasks.execute(uptime)
uts_list = env['uts']
if not uts_list:
return # Perhaps we should print a notice here?
avg = sum(uts_list) / float(len(uts_list))
print '-' * 80
print 'Average uptime: %s days' % avg
print '-' * 80
if __name__ == '__main__':
main()

Setting date/time on a unix based server with Python (Paramiko module)

Okay, can someone tell what I'm doing wrong with this simple request to change the time? I'm on a win 7 machine, trying to change the time on a linux box. I can login, search logs and run other commands, of course adjusting my code below. But this simple command is not changing the date/time. I must be overlooking something?
datetime_string = raw_input("Enter date and time in format 11/1/2011 1600")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(iP_address, username='root', password='******')
apath = '/'
apattern = datetime_string
rawcommand = 'date -s' + datetime_string
command1 = rawcommand.format(pattern=apattern)
stdin, stdout, stderr = ssh.exec_command(command1)
dateresult = stdout.read().splitlines()
Try changing this:
rawcommand = 'date -s' + datetime_string
To this:
rawcommand = 'date -s "%s"' % datetime_string
And im not positive, but I dont think rawcommand.format(pattern=apattern) is necessary:
datetime_string = raw_input("Enter date and time in format 11/1/2011 1600")
command1 = 'date -s "%s"' % datetime_string
stdin, stdout, stderr = ssh.exec_command(command1)
dateresult = stdout.read().splitlines()
You should validate user input. Especially if it might be passed unescaped to the shell.
#!/usr/bin/env python
from datetime import datetime
import paramiko
# read new date from stdin
datetime_format = "%m/%d/%Y %H%M"
newdate_string = raw_input("Enter date and time in format 11/1/2011 1600")
# validate that newdate string is in datetime_format
newdate = datetime.strptime(newdate_string, datetime_format)
# print date (change it to `-s` to set the date)
command = "date -d '%s'" % newdate.strftime(datetime_format)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("localhost") # use ssh keys to authenticate
# run it
stdin, stdout, stderr = ssh.exec_command(command)
stdin.close()
# get output of the command
print
print "stdout: %r" % (stdout.read(),)
print '*'*79
print "stderr: %r" % (stderr.read(),)
Output
$ echo 1/11/2011 1600 | python set-date.py
Enter date and time in format 11/1/2011 1600
stdout: 'Tue Jan 11 16:00:00 EST 2011\n'
*******************************************************************************
stderr: ''

Categories

Resources