Traps in a file, in smnpd not using the default port

INTRO

well for this I am going to write this in English because it was difficult to get this to work properly

the business rule is as follows:

let's get some traps from a device to our machine to port 8080 and we have to stored in a file somewhere.

Install the snmpd im using ubuntu so:


apt-get install snmpd

Save the traps in a file:


we need to modify the file /etc/default/snmpd and change the variables to this:

TRAPDRUN=yes

TRAPDOPTS='-A -Lf /var/log/snmptrapd.log -p /var/run/snmptrapd.pid’
the traps are stored in /var/log/snmptrapd.log

Configure the "community string" and the port for listen:


we need to open the file /etc/snmp/snmptrapd.conf and write this.

snmpTrapdAddr udp:8080
authCommunity log public

we are listen in the 8080 via udp package.
"public" is the community string ,change this to match with your case scenario.

Restart the service

service snmpd restart

Simulate traps

we can do this with this command if we are in the machine(localhost).

snmptrap -v 1 -c public 127.0.0.1:8080 .1.3.6.1 localhost 6 17 '' .1.3.6.1 s "Just a test"

you can change the 127.0.0.1 for the address of the machine if you are trying to test from another machine

Troubleshooting


check if indeed it is listening for the corresponding port:

netstat -lnp | grep snmp

and we get something like this:

udp 0 0 0.0.0.0:42385 0.0.0.0:* 957/snmpd
udp 0 0 127.0.0.1:161 0.0.0.0:* 957/snmpd
udp 0 0 0.0.0.0:164 0.0.0.0:* 962/snmptrapd
unix 2 [ ACC ] STREAM LISTENING 9632 957/snmpd /var/agentx/master


other way is using nmap

nmap -sU localhost

the output is something like this:


Starting Nmap 5.21 ( http://nmap.org ) at 2015-12-21 01:51 CLT
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0000080s latency).
Not shown: 997 closed ports
PORT STATE SERVICE
68/udp open|filtered dhcpc
161/udp open snmp
162/udp open|filtered snmptrap

Nmap done: 1 IP address (1 host up) scanned in 1.24 seconds


anyway you can hear the port to see if it reaches something and verify that the community string is the same, we lost so much time because of a bad string they gave us


fuente1
fuente2

Average in Bash

Problem

Given N integers, Show the average of N integers with 3 decimal places.

Input Format
La primera linea contiene The first line contain N integers.N lines with only one number.

Output Fromat
The integer with 3 decimal places.

Example


Input

4
1
2
9
8

Output

5.000


Code

read a
b=0
for (( c=1; c<=$a; c++ ))
do
read d
b=$(($b+$d))
done
echo $(printf %.3f $(echo "scale=4;($b/$a)" | bc))

Odd numbers in bash using a loop

Lately I've been kind of bored so instead of playing I've been entertaining myself by solving programming problems, one very easy this one.

Show the odd numbers from 1 until 99 using bash

code

for i in {1..99}
do
if(( $i % 2 != 0 ))
then
echo "$i"
fi
done

Using Tmux

tmux lo conoci en una charla que vi en youtube y expandí mis conocimientos usando un libro he aquí un resumen para que les sea útil.


Comandos


Nueva sesión
tmux new -s miSesion

Ver las distintas sesiones
tmux ls

Crear una nueva sesión pero que quede funcionando por detrás
tmux new -s miSesion -d

Volver a una sesión
tmux attach -t miSesion

Matar una sesión
tmux kill-session -t miSesion

Crea una nueva sesión con un nombre en la ventana
tmux new -s miSesion -n miVentana

Salir o salir ventana
exit

Hotkeys


El prefijo por defecto es Ctrl + b sin embargo se puede cambiar.

Crea una nueva ventana en tmux
prefix + c

Renombrar la ventana
prefix + ,

Ir a la ventana previa
prefix + p

Ir a la ventana siguiente
prefix + n

Ir a la ventana nº 1
prefix + 1

Salir de la ventana
prefix + &

Dettach
prefix + d

Divide la pantalla Vertical
prefix + %

Divide la pantalla Horizontal
prefix + "

Salta al panel según la dirección
prefix +{← ↑ → ↓}

Cambiamos de posición los paneles dependiendo la configuración por defecto
prefix + space

Cierra el panel que estamos ocupando
prefix + x

Pasar al modo comando
prefix + :

Mostrar toda las combinaciones de botones
prefix + ?

Inicio automatizado


Para usar una configuración para llamarlo de alguna forma se debe usar el siguiente comando:
tmux -f miConfig.conf attach

la siguiente configuración lo que hace es crear 4 ventanas con sus respectivos nombres ya que asi tenia disponible las vistas,modelos y controladores de una pagina web la cual estaba construyendo.


source-file ~/.tmux.conf
new-session -s admin -n model
new-window -n controller -t admin
new-window -n view -t admin
new-window -n config -t admin
send-keys -t model 'cd /home/ubuntu/ad/app/Model' C-m
send-keys -t controller 'cd /home/ubuntu/ad/app/Controller' C-m
send-keys -t view 'cd /home/ubuntu/ad/app/View' C-m
send-keys -t config 'cd /home/ubuntu/ad/app/Config' C-m


Mi configuración


La configuración de tmux se puede configurar colocando algunas cosas en el archivo .tmux.conf por ejemplo yo cambie el prefijo a Ctrl + a como se usa en "screen", para que tome los cambios se necesita hacer en el modo comando source-file ~/.tmux.conf o en otro archivo de configuración como en el ejemplo anterior.

.tmux.conf

#Prefix es Ctrl-a
set -g prefix C-a
bind C-a send-prefix
#Sacamos Ctrl-b
unbind C-b
#Cambiamos el Delay
set -sg escape-time 1
#Cambiamos el inicio de las ventanas a 1
set -g base-index 1
#Cambiamos el inicio para los paneles
setw -g pane-base-index 1
#Recargar nuestra configuracion a tmux
bind r source-file ~/.tmux.conf \; display "Reloaded!"
#Paneles
bind | split-window -h
bind - split-window -v
#Mouse works as expected
setw -g mode-mouse on
set -g mouse-select-pane on
set -g mouse-resize-pane on
set -g mouse-select-window on

setw -g monitor-activity on
set -g visual-activity on

set -g mode-keys vi
set -g history-limit 10000

# y and p as in vim
bind Escape copy-mode
unbind p
bind p paste-buffer
bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-selection
bind -t vi-copy 'Space' halfpage-down
bind -t vi-copy 'Bspace' halfpage-up

# extra commands for interacting with the ICCCM clipboard
bind C-c run "tmux save-buffer - | xclip -i -sel clipboard"
bind C-v run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer"
#set -g default-command "reattach-to-user-namespace -l /bin/bash"
#bind C-c run "tmux save-buffer - | reattach-to-user-namespace pbcopy"
#bind C-v run "tmux set-buffer $(reattach-to-user-namespace pbpaste);tmux paste-buffer"

# easy-to-remember split pane commands
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %

# moving between panes with vim movement keys
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# moving between windows with vim movement keys
bind -r C-h select-window -t :-
bind -r C-l select-window -t :+
# resize panes with vim movement keys
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5
# Highlight active window
setw -g window-status-fg white
setw -g window-status-bg black
setw -g window-status-attr bright
# Color para los paneles
set -g pane-border-fg green
set -g pane-border-bg black
set -g pane-active-border-fg white
set -g pane-active-border-bg yellow
# Modo vi para moverse por el buffer
#setw -g mode-keys v
# Modificando el status-bar
set -g status-left-length 40
set -g status-left "#[fg=red]Session: #S #[fg=green]#I #[fg=black]#P"
set -g status-right "#[fg=black]%d %b %R"
# Color de la status bar completa
set -g status-bg white
set -g status-utf8 on
## Ventanas en el centro
set -g status-justify centre
# Para tener los 256 colores
set -g default-terminal "screen-256color"

How many primes less than or equal to n exists?

In hackealo.co there was another problem and Ruby's solution was easier than I expected
How many primes less than or equal to 7293 exits?

#!/usr/bin/env ruby
require 'mathn'
s=0
Prime.each(7293) do |prime|
     s+=1
end
puts 'resultado:'+s.to_s

source


Ruby online
Prime numbers in Ruby

The sum of smaller prime numbers equal to "n"

In hackealo.co aggain there was another problem and the Ruby solution was easier than I expected.
What is the sum of all primes less than or equal to 5936?
#!/usr/bin/env ruby
require 'mathn'
s=0
Prime.each(5936) do |prime|
     s+=prime
end
puts 'result:'+s.to_s
fuente
Prime Numbers in Ruby