elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


  Mostrar Mensajes
Páginas: 1 2 3 [4]
31  Programación / Scripting / Re: [BASH] Borrar línea justo después del segundo ":". Análisis de log. en: 30 Enero 2013, 22:58 pm
Código
  1. $ cat /etc/passwd | cut -f1,2 -d':'
  2. $ cat /etc/passwd | awk -F': ' '{print $1":"$2;}'
  3. # Y si lo que necesitas son mas de un caracter de delimitador cambia a -F' : '
32  Programación / Scripting / Re: Script bash reiniciar ruter en: 20 Diciembre 2012, 20:26 pm
Copy-paste, pero se que me sirvio en otra oportunidad.

Código
  1. #!/usr/bin/expect
  2. set timeout 20
  3. set ip [lindex $argv 0]
  4. set port [lindex $argv 1]
  5. set user [lindex $argv 2]
  6. set password [lindex $argv 3]
  7.  
  8. spawn telnet $ip $port
  9. expect "'^]'."
  10. sleep .1;
  11. send "\r";
  12. expect
  13. {
  14.  "login:"
  15.  {
  16.        send "$user\r"
  17.        expect "Password:"
  18.        send "$password\r";
  19.        interact
  20.  
  21.  }
  22.  
  23.  "host: Connection refused"
  24.  {
  25.    send_user "ERROR:EXITING!"
  26.    exit
  27.  }
  28.  
  29. }

Código:
http://stackoverflow.com/questions/7789710/expect-script-to-automate-telnet-login
33  Programación / Scripting / Re: Crear Monitor de recursos linux en: 5 Diciembre 2012, 20:18 pm
Update: ahora acepta varios PID's y tiene un poco mas de color...espero no tener que volver a hacer esto...

Código
  1. #!/bin/sh
  2. PROCESS_LIST_FILE=`mktemp`
  3. PARSED_PROCESS_LIST_FILE=`mktemp`
  4. GRAPH_SIZE=10
  5. DOTS='****************************************************************************************************'
  6.  
  7. trap control_c SIGINT
  8.  
  9. control_c(){
  10.    rm -f $PROCESS_LIST_FILE
  11.    rm -f $PARSED_PROCESS_LIST_FILE
  12.    echo 'Bye!'
  13.    stty echo
  14.    tput cvvis
  15.    exit 5
  16. }
  17.  
  18. f_watch_watcher(){
  19.    tput clear
  20.    tput civis
  21.    stty -echo
  22.    while :; do
  23.        i=0
  24.        for pid in $PID_LIST; do
  25.            if [[ -d /proc/$pid ]]; then
  26.                tput cup $i 0
  27.                CURRENT_VALUE=`ps h -p $pid -o $PS_STRING`
  28.                echo -n "$DOTS" "$CURRENT_VALUE"
  29.                tput cup $i 0
  30.                ((CURRENT_VALUE++))
  31.                case $CURRENT_VALUE in
  32.                    [0-9]|[1-2][0-9]|3[1-3])color='\e[0;32m';;
  33.                    3[4-9]|[4-5][0-9]|6[0-6])color='\e[0;33m';;
  34.                    6[7-9]|[7-9][0-9]|100)color='\e[0;31m';;
  35.                esac
  36.                echo -ne $color
  37.                NOTDOT=`seq -s "+" $CURRENT_VALUE | sed 's/[0-9]//g'`
  38.                echo -en $NOTDOT'\e[0m'
  39.            else
  40.                tput cup $i 0
  41.                echo -n 'PID dead: '$pid
  42.            fi
  43.            ((i++))
  44.         done
  45.    done
  46.    exit 4
  47. }
  48.  
  49. f_what_watch(){
  50.    select optionC in "CPU" "Memory" "CPU Time" "State"; do
  51.        case "$optionC" in
  52.            "CPU")
  53.                PS_STRING='c'
  54.                break
  55.            ;;
  56.            *)
  57.                echo -e '501 Not implemented\nGo away!'
  58.                exit 5
  59.            ;;
  60.        esac
  61.    done
  62. }
  63.  
  64. f_get_pidlist(){
  65.    # TODO: Add warning if PID was selected before OR
  66.    #       Remove PID from list
  67.    select optionB in `cat $PARSED_PROCESS_LIST_FILE` "Done"; do
  68.        case $optionB in
  69.            "Done")
  70.                # Catch empty PID_LIST
  71.                [[ x"$PID_LIST" == 'x' ]] && echo 'You did not select any PID...' && control_c
  72.                break # Exits select loop
  73.            ;;
  74.            *)
  75.                # Catch first PID
  76.                if [[ x"$PID_LIST" == 'x' ]]; then
  77.                    PID_LIST=$optionB
  78.                else
  79.                    PID_LIST="$PID_LIST"' '"$optionB"
  80.                fi
  81.            ;;
  82.        esac
  83.    done
  84. }
  85.  
  86. f_select_pid(){
  87.    cat $PROCESS_LIST_FILE | cut -f1 -d' ' | tr ' ' '-' > $PARSED_PROCESS_LIST_FILE
  88. }
  89.  
  90. f_select_filter(){
  91.    select optionA in "PID - Process ID" "USER - User name" "COMM - Command"; do
  92.        case $optionA in
  93.            "PID - Process ID")
  94.                f_select_pid # Return $PARSED_PROCESS_LIST_FILE
  95.                break # This is for the select loop
  96.            ;;
  97.            "USER - User name"|"COMM - Command")
  98.                echo -e '501 Not implemented\nGo away! '$optionA
  99.                exit 3
  100.            ;;
  101.            *)
  102.                echo 'Error!'
  103.                exit 3
  104.            ;;
  105.        esac
  106.    done
  107. }
  108.  
  109. f_get_psfile(){
  110.    ps h -o pid,user,comm \
  111.      -N --ppid 2 -p 1,2 \
  112.      | column -t | tr -s ' ' | cut -f1,2,3 -d ' '> $PROCESS_LIST_FILE
  113. }
  114.  
  115. f_get_psfile    # Return $PROCESS_LIST_FILE
  116. f_select_filter # Return $PARSED_PROCESS_LIST_FILE
  117. f_get_pidlist   # Return $PID_LIST
  118. f_what_watch    # Return $PS_STRING
  119. f_watch_watcher # Shows nice graphs
  120.  
  121. control_c
  122.  
34  Sistemas Operativos / GNU/Linux / Re: Ayuda comandos en: 4 Diciembre 2012, 21:24 pm
¿Algo como esto?

Código:
$ cat /etc/passwd | cut -f1,3,4 -d':'
root:0:0
bin:1:1
daemon:2:2
adm:3:4
lp:4:7
sync:5:0
shutdown:6:0
halt:7:0
mail:8:12
...
35  Programación / Scripting / Re: Crear Monitor de recursos linux en: 29 Noviembre 2012, 06:02 am
Sonaba entretenido y dado que no es mi tarea me tome la libertad de hacerlo como se me ocurrió...y hasta acá llego :D

Quizás algún día descubra un buen gráfico para la medición.

Código
  1. #!/bin/sh
  2. PROCESS_LIST_FILE=`mktemp`
  3. PARSED_PROCESS_LIST_FILE=`mktemp`
  4. GRAPH_SIZE=10
  5.  
  6. trap control_c SIGINT
  7.  
  8. control_c(){
  9.    rm -vf $PROCESS_LIST_FILE
  10.    rm -vf $PARSED_PROCESS_LIST_FILE
  11.    echo 'Bye!'
  12.    exit 5
  13. }
  14.  
  15. f_watch_watcher(){
  16.    while [[ -d /proc/$optionB ]]; do
  17.        CURRENT_VALUE=`ps h -p $optionB -o $PS_STRING`
  18.        let "CURRENT_VALUE %= $GRAPH_SIZE"
  19.        CURRENT_VALUE=$((CURRENT_VALUE + 2))
  20.        clear
  21.        seq -s "+" $CURRENT_VALUE | sed 's/[0-9]//g'
  22.        sleep 1
  23.        clear
  24.    done
  25.    exit 4
  26. }
  27.  
  28. f_get_watch(){
  29.    select optionC in "CPU" "Memory" "CPU Time" "State"; do
  30.        case "$optionC" in
  31.            "CPU")PS_STRING='c';f_watch_watcher;;
  32.            *)echo -e '501 Not implemented\nGo away! '$optionC; exit 3;;
  33.        esac
  34.    done
  35. }
  36.  
  37. f_show_parsed(){
  38.    select optionB in `cat $PARSED_PROCESS_LIST_FILE` "Done"; do
  39.        case "$optionB" in
  40.            "Done")exit;;
  41.            *) f_get_watch; exit 2 ;;
  42.        esac
  43.    done
  44. }
  45.  
  46. f_select_pid(){
  47.    cat $PROCESS_LIST_FILE | cut -f1 -d' ' | tr ' ' '-' > $PARSED_PROCESS_LIST_FILE
  48.    f_show_parsed
  49. }
  50.  
  51. ps fh -o pid,user,comm \
  52.      -N --ppid 2 -p 1,2 \
  53.      | awk '$3 !~ /^[|\\]/ { print $1" "$2" "$3 }' > $PROCESS_LIST_FILE
  54.  
  55. select optionA in "PID - Process ID" "USER - User name" "COMM - Command"; do
  56.    case "$optionA" in
  57.        "PID - Process ID")f_select_pid;;
  58.        *)echo -e '501 Not implemented\nGo away! '$optionA; exit 1;;
  59.    esac
  60. done
  61.  
  62. control_c
  63.  
36  Sistemas Operativos / GNU/Linux / Re: Realizar wget correctamente en determinadas URLs en: 28 Noviembre 2012, 04:39 am
La segunda URL que das como ejemplo (dw.com.com/redir...) es solo una re-dirección hacia el archivo original.

Código:
sendai@limbo ~ $ wget -S 'http://dw.com.com/redir?edId=3&siteId=4&oId=3...8071%26psid%3D10019223%26fileName%3Davast_free_antivirus_setup.exe'
--2012-11-27 21:25:06--  http://dw.com.com/redir?edId=3&siteId=4&oId=3000-2239_4-10019223&ontId=2239_4&spi...D12808071%26psid%3D10019223%26fileName%3Davast_free_antivirus_setup.exe
Resolviendo dw.com.com... 216.239.120.50
Conectando con dw.com.com[216.239.120.50]:80... conectado.
Petición HTTP enviada, esperando respuesta...
  HTTP/1.1 302 Found
  Date: Wed, 28 Nov 2012 03:28:15 GMT
  Server: Apache/2.0
  Pragma: no-cache
  Cache-control: no-cache, must-revalidate, no-transform
  Vary: *
  Expires: Fri, 23 Jan 1970 12:12:12 GMT
  Location: http://software-files-a.cnet.com/s/software/12/80/80/71/avast_free_antivirus_setup.exe?token=1354108731_17...71&psid=10019223&fileName=avast_free_antivirus_setup.exe
  Content-Length: 0
  P3P: CP="CAO DSP COR CURa ADMa DEVa PSAa PSDa IVAi IVDi CONi OUR OTRi IND PHY ONL UNI FIN COM NAV INT DEM STA"
  Keep-Alive: timeout=363, max=785
  Connection: Keep-Alive
  Content-Type: text/plain
Localización: http://software-files-a.cnet.com/s/software/12/80/80/71/avast_free_antivirus_setup.exe?token=1354108731_17d6...d=10019223&fileName=avast_free_antivirus_setup.exe [siguiendo]
--2012-11-27 21:25:07--  http://software-files-a.cnet.com/s/software/12/80/80/71/avast_free_antivirus_setup.exe?token=1354108731_17d6ad31f187..019223&fileName=avast_free_antivirus_setup.exe
Resolviendo software-files-a.cnet.com... 23.62.63.18, 23.62.63.24
Conectando con software-files-a.cnet.com[23.62.63.18]:80... conectado.
Petición HTTP enviada, esperando respuesta...
  HTTP/1.1 200 OK
  Server: Apache
  ETag: "94e28010255d126fe7bfe4e55c06492c:1351718223"
  Last-Modified: Wed, 31 Oct 2012 21:17:02 GMT
  Accept-Ranges: bytes
  Content-Length: 97495576
  Content-Type: application/octet-stream
  Date: Wed, 28 Nov 2012 03:28:16 GMT
  Connection: keep-alive
Longitud: 97495576 (93M) [application/octet-stream]

La url (software-files-a.cnet...)  es la url real (y probablemente temporaria) del archivo.

Imagino los browsers normales usaran esta segunda url para darle nombre al archivo.

Solo se me ocurre que hagas lo mismo con mas wget y bash :D
37  Programación / Scripting / Re: como abrir otra consola y mandarle instrucciones desde bash? en: 27 Noviembre 2012, 20:47 pm
Primero tenes que saber como se llama el proceso de tu emulador de terminal

Código:
sendai@goliath ~ $ pstree -p | grep $BASHPID
        |-urxvt(28596)---bash(28597)-+-grep(29178)

Averiguar el full path del bin

Código:
sendai@goliath ~ $ type urxvt
urxvt is /usr/bin/urxvt

Buscar en el man cual es el parametro que usa para acceptar comandos.

Código:
sendai@goliath ~ $ man urxvt | grep -im1 command
       urxvt [options] [-e command [ args ]]

Y finalmente crear un bash.

Código:
#!/bin/bash
/usr/bin/urxvt -e alsamixer

Puede que tengas que investigar peculiaridades de cada terminal.
38  Programación / Scripting / Re: [BASH] usar return en bash en: 16 Noviembre 2012, 04:38 am
Por defecto, en bash, todas las variables declaradas fuera o dentro de una función son globales.

Si bien podes hacerla local anteponiendo "local" a la declaración de la variable.
Páginas: 1 2 3 [4]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines