Bash in theory and in practice - part two

854 views

Published on

bash syntax and example

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
854
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
32
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Bash in theory and in practice - part two

  1. 1. bashin theory and in practice 19.06.2013 03.07.2013 @pvb265 #imolug
  2. 2. abstract 19.06.2013 03.07.2013 @pvb265 #imolug
  3. 3. abstract 19.06.2013 03.07.2013 @pvb265 #imolug shell bash kernel o.s. history
  4. 4. abstract 19.06.2013 03.07.2013 @pvb265 #imolug shell bash kernel o.s. Login Process history passwd shadow shells configurationfiles
  5. 5. abstract 19.06.2013 03.07.2013 @pvb265 #imolug shell bash kernel o.s. Login Process history passwd shadow shells configurationfiles Main Features POSIX completion cmdhistory
  6. 6. AgendaGeneral Info Underwater Overview Syntax Example 19.06.2013 03.07.2013 @pvb265 #imolug 19.06 03.07
  7. 7. 19.06.2013 03.07.2013 @pvb265 #imolug bash Syntax
  8. 8. 19.06.2013 03.07.2013 @pvb265 #imolug bash Syntaxσύνταξις "arrangement" from σύν syn, "together", and τάξις táxis, "an ordering"
  9. 9. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax
  10. 10. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax comment #à #
  11. 11. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax comment # singole quote ' X ' ' ' '? ' à # any trasformations
  12. 12. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax comment # singole quote ' X ' ' ' '? ' à # any trasformations double quote “ X ”“ 2 any trasformations buts $ ~ ! * @
  13. 13. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax comment # singole quote ' X ' ' ' '? ' à # any trasformations double quote “ X ”“ 2 any trasformations buts $ ~ ! * @ ANSI-C quoting | b backspace f form feed n newline r carriage return t horizontal tab v vertical tab backslash ' single quote " double quote nnn the octal value nnn xHH the hexadecimal value HH
  14. 14. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax variable vector NAME=”test” echo $NAME NAME=(test1 test2 test3) echo $TEST[0]
  15. 15. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ...
  16. 16. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... MacBook-Pro-di-valerio:~ valeriobalbi$ echo "the shell I'm using is $SHELL and his value is in " '$SHELL' the shell I'm using is /bin/bash and his value is in $SHELL
  17. 17. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until test-command; do commands; done while test-command; do commands; done for name in lists; do commands; done for (( exp1; exp2; exp3 )); do commands; done
  18. 18. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until test-command; do commands; done while test-command; do commands; done for name in lists; do commands; done for (( exp1; exp2; exp3 )); do commands; done
  19. 19. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until test-command; do commands; done while test-command; do commands; done for name in lists; do commands; done for (( exp1; exp2; exp3 )); do commands; done MacBook-Pro-di-valerio:~ valeriobalbi$ export i=2 MacBook-Pro-di-valerio:~ valeriobalbi$ until [ $i -lt 0 ]; do echo $i; export i=$(($i-1)); done 2 1 0
  20. 20. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until test-command; do commands; done while test-command; do commands; done for name in lists; do commands; done for (( exp1; exp2; exp3 )); do commands; done MacBook-Pro-di-valerio:~ valeriobalbi$ export i=2 MacBook-Pro-di-valerio:~ valeriobalbi$ while [ $i -ge 0 ]; do echo $i; export i=$(($i-1)); done 2 1 0
  21. 21. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until test-command; do commands; done while test-command; do commands; done for name in lists; do commands; done for (( exp1; exp2; exp3 )); do commands; done MacBook-Pro-di-valerio:~ valeriobalbi$ while true; do date; sleep 2; done Mar 2 Lug 2013 00:58:07 CEST Mar 2 Lug 2013 00:58:09 CEST Mar 2 Lug 2013 00:58:11 CEST ^C
  22. 22. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until test-command; do commands; done while test-command; do commands; done for name in lists; do commands; done for (( exp1; exp2; exp3 )); do commands; done MacBook-Pro-di-valerio:~ valeriobalbi$ for i in 2 1 0; do echo $i; done 2 1 0
  23. 23. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until test-command; do commands; done while test-command; do commands; done for name in lists; do commands; done for (( exp1; exp2; exp3 )); do commands; done MacBook-Pro-di-valerio:~ valeriobalbi$ for i in $(ls); do echo $i; done . .. file_one file_two
  24. 24. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until test-command; do commands; done while test-command; do commands; done for name in lists; do commands; done for (( exp1; exp2; exp3 )); do commands; done MacBook-Pro-di-valerio:~ valeriobalbi$ for (( i=0 ; $i<3; i++ )); do echo $i; done 0 1 2
  25. 25. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until while for command if test-command; then commands; ficonditional if test-command; then commands; else commands; fi if test-command; then commands; elif commands; else commands; fi if test-command; then commands; (elif commands;)* else commands; fi case name in value) commands;; … value) commands;; esac; select name in words; do commands; done
  26. 26. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until while for command if test-command; then commands; ficonditional if test-command; then commands; else commands; fi if test-command; then commands; elif commands; else commands; fi if test-command; then commands; (elif commands;)* else commands; fi case name in (value) commands;; )+ esac commands; select name in words; do commands; done MacBook-Pro-di-valerio:~ valeriobalbi$ export i=start; case $i in > start) > echo "start command placeholder";; > stop) > echo "stopping command placeholder";; > esac Start command placeholder
  27. 27. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until while for command if test-command; then commands; ficonditional if test-command; then commands; else commands; fi if test-command; then commands; elif commands; else commands; fi if test-command; then commands; (elif commands;)* else commands; fi case name in (value) commands;; )+ esac commands; select name in words; do commands; done MacBook-Pro-di-valerio:~ valeriobalbi$ export i=”one two”; select j in $i; do echo $j; done 1) one 2) two #? 1 one
  28. 28. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until while for command if case select commandconditional arithmetics logical complex (( expression )) [[ expression ]]
  29. 29. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until while for command if case select commandconditional arithmetics logical complex MacBook-Pro-di-valerio:~ valeriobalbi$ export i=10; ((i--)); echo $i 9 (( expression )) [[ expression ]]
  30. 30. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax command verb parameter1 parameter2 parameter3 ... looping until while for command if case select commandconditional arithmetics logical complex MacBook-Pro-di-valerio:~ valeriobalbi$ if [[ -a /etc/passwd || -b /etc/passwd ]]; then ls -l /etc/passwd; fi -rw-r--r-- 1 root wheel 5148 18 Nov 2011 /etc/passwd MacBook-Pro-di-valerio:~ valeriobalbi$ if [[ -a /etc/passwd && -b /etc/passwd ]]; then ls -l /etc/passwd; fi MacBook-Pro-di-valerio:~ valeriobalbi$ (( expression )) [[ expression ]]
  31. 31. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax parentheses ( list ) curly brace new environment, space needed { list; } same environment, operational token
  32. 32. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax parentheses ( list ) curly brace new environment, space needed { list; } same environment, operational token function name ( ) { commands; } redirections name ( ) { commands; } redirections undef name FUNCNEST != 0 local definition variable
  33. 33. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax parentheses ( list ) curly brace MacBook-Pro-di-valerio:~ valeriobalbi$ foo(){ # do something # if not false call foo foo } MacBook-Pro-di-valerio:~ valeriobalbi$ foo new environment, space needed { list; } same environment, operational token function name ( ) { commands; } redirections name ( ) { commands; } redirections undef name FUNCNEST != 0 local definition variable infinite loop
  34. 34. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax parentheses ( list ) curly brace MacBook-Pro-di-valerio:~ valeriobalbi$ export FUNCNEST=5 MacBook-Pro-di-valerio:~ valeriobalbi$ foo(){ # do something # if not false call foo foo } MacBook-Pro-di-valerio:~ valeriobalbi$ foo MacBook-Pro-di-valerio:~ valeriobalbi$ undef foo MacBook-Pro-di-valerio:~ valeriobalbi$ foo -bash: foo: command not found new environment, space needed { list; } same environment, operational token function name ( ) { commands; } redirections name ( ) { commands; } redirections undef name FUNCNEST != 0 local definition variable
  35. 35. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax parentheses ( list ) curly brace new environment, space needed { list; } same environment, operational token built-in function function definition . filename break n cd .. cd - cd directory eval exec command exit n export pwd readonly name return n shift n test umask mode unset name bash specificsh specific
  36. 36. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax parentheses ( list ) curly brace new environment, space needed { list; } same environment, operational token built-in function function definition . filename break n cd .. cd - cd directory eval exec command exit n export pwd readonly name return n shift n test umask mode unset name alias name=value command name echo help local name=value let printf format values read n source filename ulimit unalias bash specificsh specific
  37. 37. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax positional special
  38. 38. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax positional special useradd -b /home/pvb265 pvb265
  39. 39. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax positional useradd -b /home/pvb265 pvb265 ${0} ${1} ${2} ${3} special
  40. 40. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax positional useradd -b /home/pvb265 pvb265 special $* equivalent ${1}${2}${3}.... $@ equivalent ${1} ${2} ${3} $# parameter number $? return code $$ PID ${0} ${1} ${2} ${3}
  41. 41. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax $CDPATH $HOME $IFS $PATH $PS1 $PS2 bash specificsh specific
  42. 42. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax $CDPATH $HOME $IFS $PATH $PS1 $PS2 $BASH* $ENV $HISTSIZE $HISTFILESIZE LANG SHELL PPID UID GID bash specificsh specific
  43. 43. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax
  44. 44. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax process1
  45. 45. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax process1 stdin
  46. 46. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax process1 stdin stdout stderr
  47. 47. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax process1 stdin stdout stderr 0 1 2
  48. 48. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax cat /etc/hosts > /tmp/output.txt process1
  49. 49. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax cat /etc/hosts > /tmp/output.txt cat /etc/passwd >> /tmp/output.txt process1
  50. 50. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax cat /etc/hosts > /tmp/output.txt cat /etc/passwd >> /tmp/output.txt cat /etc/passwd 2>&1 > /tmp/output.txt process1
  51. 51. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax cat /etc/hosts > /tmp/output.txt cat /etc/passwd >> /tmp/output.txt cat /etc/passwd 2>&1 > /tmp/output.txt > /tmp/output.txt process1
  52. 52. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax process1 process2 pipeline ||
  53. 53. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax Script is a collection of commands #!/bin/sh # RETVAL=0 prog="sssd" # Source function library. . /etc/init.d/functions SSSD=/usr/sbin/sssd LOCK_FILE=/var/lock/subsys/sssd PID_FILE=/var/run/sssd.pid start() { [ -x $SSSD ] || exit 5 echo -n $"Starting $prog: " daemon $SSSD -f -D && success || failure RETVAL=$? echo [ "$RETVAL" = 0 ] && touch $LOCK_FILE return $RETVAL }
  54. 54. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax shabang #! #!/bin/bash
  55. 55. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax shabang #! #!/bin/bash code syntax tree
  56. 56. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax shabang #! #!/bin/bash code syntax tree executable code execution
  57. 57. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax shabang #! #!/bin/bash code syntax tree execution shabang tell O.S. which interpreter use to execute the code For each #! line immagine the fork of a new interpreter
  58. 58. 19.06.2013 03.07.2013 @pvb265 #imolug bash Basics Commands Functions Parameters Variables Redirections Scripting Syntax shabang #! #!/bin/bash code syntax tree execution a script file can be executed - bash scriptfilename.sh - scriptfilename.sh
  59. 59. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example
  60. 60. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example [root@doc ~]# runlevel N 3 [root@doc ~]# cd /etc/rc+TAB+TAB rc rc0.d/ rc1.d/ rc2.d/ rc3.d/ rc4.d/ rc5.d/ rc6.d/ rc.d/ rc.local rc.sysinit [root@doc ~]# cd /etc/rc3.d/ [root@doc rc3.d]# ls -l totale 0 lrwxrwxrwx. 1 root root 15 22 giu 23:52 K01numad -> ../init.d/numad lrwxrwxrwx. 1 root root 16 22 giu 23:57 K01smartd -> ../init.d/smartd lrwxrwxrwx. 1 root root 17 22 giu 21:21 K02oddjobd -> ../init.d/oddjobd lrwxrwxrwx. 1 root root 16 22 giu 23:58 K10psacct -> ../init.d/psacct lrwxrwxrwx. 1 root root 19 22 giu 23:57 K10saslauthd -> ../init.d/saslauthd … lrwxrwxrwx. 1 root root 14 22 giu 23:57 S55sshd -> ../init.d/sshd lrwxrwxrwx. 1 root root 16 23 giu 19:42 S64mysqld -> ../init.d/mysqld lrwxrwxrwx. 1 root root 17 22 giu 21:21 S80postfix -> ../init.d/postfix ... lrwxrwxrwx. 1 root root 11 22 giu 23:48 S99local -> ../rc.local
  61. 61. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example [root@doc rc3.d]# cd ../init.d/ [root@doc init.d]# ls -l totale 320 -rwxr-xr-x. 1 root root 1288 23 feb 05:39 abrt-ccpp … -rwxr-xr-x. 1 root root 1698 22 feb 06:38 sandbox -rwxr-xr-x. 1 root root 2056 20 nov 2012 saslauthd -rwxr-xr-x. 1 root root 647 9 gen 12:13 single -rwxr-xr-x. 1 root root 3002 21 feb 23:26 smartd -rwxr-xr-x. 1 root root 4534 22 feb 04:51 sshd -rwxr-xr-x. 1 root root 2647 29 apr 09:50 sssd -rwxr-xr-x. 1 root root 1228 22 giu 2012 sysstat -rwxr-xr-x. 1 root root 2294 22 feb 06:22 udev-post -rwxr-xr-x. 1 root root 1608 22 feb 05:09 winbind -rwxr-xr-x. 1 root root 4799 22 feb 00:23 ypbind [root@doc init.d]# file sshd sshd: Bourne-Again shell script text executable
  62. 62. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example #!/bin/bash # # sshd Start up the OpenSSH server daemon # # chkconfig: 2345 55 25 # description: SSH is a protocol for secure remote shell access. # This service starts up the OpenSSH server daemon. # # processname: sshd # config: /etc/ssh/ssh_host_key # config: /etc/ssh/ssh_host_key.pub # config: /etc/ssh/ssh_random_seed # config: /etc/ssh/sshd_config # pidfile: /var/run/sshd.pid ### BEGIN INIT INFO # Provides: sshd # Required-Start: $local_fs $network $syslog # Required-Stop: $local_fs $syslog # Should-Start: $syslog # Should-Stop: $network $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6
  63. 63. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example #!/bin/bash # # sshd Start up the OpenSSH server daemon # # chkconfig: 2345 55 25 # description: SSH is a protocol for secure remote shell access. # This service starts up the OpenSSH server daemon. # # processname: sshd # config: /etc/ssh/ssh_host_key # config: /etc/ssh/ssh_host_key.pub # config: /etc/ssh/ssh_random_seed # config: /etc/ssh/sshd_config # pidfile: /var/run/sshd.pid ### BEGIN INIT INFO # Provides: sshd # Required-Start: $local_fs $network $syslog # Required-Stop: $local_fs $syslog # Should-Start: $syslog # Should-Stop: $network $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6
  64. 64. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example # Short-Description: Start up the OpenSSH server daemon # Description: SSH is a protocol for secure remote shell access. # This service starts up the OpenSSH server daemon. ### END INIT INFO # source function library . /etc/rc.d/init.d/functions # pull in sysconfig settings [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd RETVAL=0 prog="sshd" lockfile=/var/lock/subsys/$prog # Some functions to make the below more readable KEYGEN=/usr/bin/ssh-keygen SSHD=/usr/sbin/sshd RSA1_KEY=/etc/ssh/ssh_host_key RSA_KEY=/etc/ssh/ssh_host_rsa_key DSA_KEY=/etc/ssh/ssh_host_dsa_key PID_FILE=/var/run/sshd.pid runlevel=$(set -- $(runlevel); eval "echo $$#" )
  65. 65. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example # Short-Description: Start up the OpenSSH server daemon # Description: SSH is a protocol for secure remote shell access. # This service starts up the OpenSSH server daemon. ### END INIT INFO # source function library . /etc/rc.d/init.d/functions # pull in sysconfig settings [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd RETVAL=0 prog="sshd" lockfile=/var/lock/subsys/$prog # Some functions to make the below more readable KEYGEN=/usr/bin/ssh-keygen SSHD=/usr/sbin/sshd RSA1_KEY=/etc/ssh/ssh_host_key RSA_KEY=/etc/ssh/ssh_host_rsa_key DSA_KEY=/etc/ssh/ssh_host_dsa_key PID_FILE=/var/run/sshd.pid runlevel=$(set -- $(runlevel); eval "echo $$#" )
  66. 66. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example # Short-Description: Start up the OpenSSH server daemon # Description: SSH is a protocol for secure remote shell access. # This service starts up the OpenSSH server daemon. ### END INIT INFO # source function library . /etc/rc.d/init.d/functions # pull in sysconfig settings [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd RETVAL=0 prog="sshd" lockfile=/var/lock/subsys/$prog # Some functions to make the below more readable KEYGEN=/usr/bin/ssh-keygen SSHD=/usr/sbin/sshd RSA1_KEY=/etc/ssh/ssh_host_key RSA_KEY=/etc/ssh/ssh_host_rsa_key DSA_KEY=/etc/ssh/ssh_host_dsa_key PID_FILE=/var/run/sshd.pid runlevel=$(set -- $(runlevel); eval "echo $$#" )
  67. 67. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example # Short-Description: Start up the OpenSSH server daemon # Description: SSH is a protocol for secure remote shell access. # This service starts up the OpenSSH server daemon. ### END INIT INFO # source function library . /etc/rc.d/init.d/functions # pull in sysconfig settings [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd RETVAL=0 prog="sshd" lockfile=/var/lock/subsys/$prog # Some functions to make the below more readable KEYGEN=/usr/bin/ssh-keygen SSHD=/usr/sbin/sshd RSA1_KEY=/etc/ssh/ssh_host_key RSA_KEY=/etc/ssh/ssh_host_rsa_key DSA_KEY=/etc/ssh/ssh_host_dsa_key PID_FILE=/var/run/sshd.pid runlevel=$(set -- $(runlevel); eval "echo $$#" )
  68. 68. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example # Short-Description: Start up the OpenSSH server daemon # Description: SSH is a protocol for secure remote shell access. # This service starts up the OpenSSH server daemon. ### END INIT INFO # source function library . /etc/rc.d/init.d/functions # pull in sysconfig settings [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd RETVAL=0 prog="sshd" lockfile=/var/lock/subsys/$prog # Some functions to make the below more readable KEYGEN=/usr/bin/ssh-keygen SSHD=/usr/sbin/sshd RSA1_KEY=/etc/ssh/ssh_host_key RSA_KEY=/etc/ssh/ssh_host_rsa_key DSA_KEY=/etc/ssh/ssh_host_dsa_key PID_FILE=/var/run/sshd.pid runlevel=$(set -- $(runlevel); eval "echo $$#" )
  69. 69. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example # Short-Description: Start up the OpenSSH server daemon # Description: SSH is a protocol for secure remote shell access. # This service starts up the OpenSSH server daemon. ### END INIT INFO # source function library . /etc/rc.d/init.d/functions # pull in sysconfig settings [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd RETVAL=0 prog="sshd" lockfile=/var/lock/subsys/$prog # Some functions to make the below more readable KEYGEN=/usr/bin/ssh-keygen SSHD=/usr/sbin/sshd RSA1_KEY=/etc/ssh/ssh_host_key RSA_KEY=/etc/ssh/ssh_host_rsa_key DSA_KEY=/etc/ssh/ssh_host_dsa_key PID_FILE=/var/run/sshd.pid runlevel=$(set -- $(runlevel); eval "echo $$#" )
  70. 70. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example # Short-Description: Start up the OpenSSH server daemon # Description: SSH is a protocol for secure remote shell access. # This service starts up the OpenSSH server daemon. ### END INIT INFO # source function library . /etc/rc.d/init.d/functions # pull in sysconfig settings [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd RETVAL=0 prog="sshd" lockfile=/var/lock/subsys/$prog # Some functions to make the below more readable KEYGEN=/usr/bin/ssh-keygen SSHD=/usr/sbin/sshd RSA1_KEY=/etc/ssh/ssh_host_key RSA_KEY=/etc/ssh/ssh_host_rsa_key DSA_KEY=/etc/ssh/ssh_host_dsa_key PID_FILE=/var/run/sshd.pid runlevel=$(set -- $(runlevel); eval "echo $$#" )
  71. 71. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example fips_enabled() { if [ -r /proc/sys/crypto/fips_enabled ]; then cat /proc/sys/crypto/fips_enabled else echo 0 fi } do_rsa1_keygen() { if [ ! -s $RSA1_KEY -a `fips_enabled` -eq 0 ]; then echo -n $"Generating SSH1 RSA host key: " rm -f $RSA1_KEY if test ! -f $RSA1_KEY && $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then chmod 600 $RSA1_KEY chmod 644 $RSA1_KEY.pub if [ -x /sbin/restorecon ]; then /sbin/restorecon $RSA1_KEY.pub fi success $"RSA1 key generation" echo else failure $"RSA1 key generation" echo exit 1 fi fi }
  72. 72. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example fips_enabled() { if [ -r /proc/sys/crypto/fips_enabled ]; then cat /proc/sys/crypto/fips_enabled else echo 0 fi } do_rsa1_keygen() { if [ ! -s $RSA1_KEY -a `fips_enabled` -eq 0 ]; then echo -n $"Generating SSH1 RSA host key: " rm -f $RSA1_KEY if test ! -f $RSA1_KEY && $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then chmod 600 $RSA1_KEY chmod 644 $RSA1_KEY.pub if [ -x /sbin/restorecon ]; then /sbin/restorecon $RSA1_KEY.pub fi success $"RSA1 key generation" echo else failure $"RSA1 key generation" echo exit 1 fi fi }
  73. 73. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example fips_enabled() { if [ -r /proc/sys/crypto/fips_enabled ]; then cat /proc/sys/crypto/fips_enabled else echo 0 fi } do_rsa1_keygen() { if [ ! -s $RSA1_KEY -a `fips_enabled` -eq 0 ]; then echo -n $"Generating SSH1 RSA host key: " rm -f $RSA1_KEY if test ! -f $RSA1_KEY && $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then chmod 600 $RSA1_KEY chmod 644 $RSA1_KEY.pub if [ -x /sbin/restorecon ]; then /sbin/restorecon $RSA1_KEY.pub fi success $"RSA1 key generation" echo else failure $"RSA1 key generation" echo exit 1 fi fi }
  74. 74. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example start() { [ -x $SSHD ] || exit 5 [ -f /etc/ssh/sshd_config ] || exit 6 # Create keys if necessary if [ "x${AUTOCREATE_SERVER_KEYS}" != xNO ]; then do_rsa1_keygen do_rsa_keygen do_dsa_keygen fi echo -n $"Starting $prog: " $SSHD $OPTIONS && success || failure RETVAL=$? [ $RETVAL -eq 0 ] && touch $lockfile echo return $RETVAL }
  75. 75. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example start() { [ -x $SSHD ] || exit 5 [ -f /etc/ssh/sshd_config ] || exit 6 # Create keys if necessary if [ "x${AUTOCREATE_SERVER_KEYS}" != xNO ]; then do_rsa1_keygen do_rsa_keygen do_dsa_keygen fi echo -n $"Starting $prog: " $SSHD $OPTIONS && success || failure RETVAL=$? [ $RETVAL -eq 0 ] && touch $lockfile echo return $RETVAL }
  76. 76. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example start() { [ -x $SSHD ] || exit 5 [ -f /etc/ssh/sshd_config ] || exit 6 # Create keys if necessary if [ "x${AUTOCREATE_SERVER_KEYS}" != xNO ]; then do_rsa1_keygen do_rsa_keygen do_dsa_keygen fi echo -n $"Starting $prog: " $SSHD $OPTIONS && success || failure RETVAL=$? [ $RETVAL -eq 0 ] && touch $lockfile echo return $RETVAL }
  77. 77. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example case "$1" in start) rh_status_q && exit 0 start ;; stop) if ! rh_status_q; then rm -f $lockfile exit 0 fi stop ;; *) echo $"Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}" RETVAL=2 esac exit $RETVAL
  78. 78. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example case "$1" in start) rh_status_q && exit 0 start ;; stop) if ! rh_status_q; then rm -f $lockfile exit 0 fi stop ;; *) echo $"Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}" RETVAL=2 esac exit $RETVAL
  79. 79. 19.06.2013 03.07.2013 @pvb265 #imolug bash Example Questions?

×