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. 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. 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
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 ]]
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
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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
}