Tag Archives: script

Ubuntu: get last stable kernel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env ruby
require 'net/http'
require 'nokogiri'
require 'fileutils'
require 'securerandom'
require 'progressbar'
require 'micro-optparse'

VERSION='2.1'

options = Parser.new do |p|
  p.banner = "This is a script for getting last kernel version from kernel.ubuntu.com/~kernel-ppa/mainline, for usage see below"
  p.version = "script version #{VERSION}"
  p.option :arch, 'architecture type "amd64" or "i386", default "amd64"', :default => 'amd64', :value_in_set => ['amd64', 'i386']
  p.option :type, 'kernel type "generic" or "lowlatency", default "generic"', :default => 'generic', :value_in_set => ['generic', 'lowlatency']
  p.option :show, 'only show last stable kernel version end exit', :default => false, :optional => true
  p.option :install, 'install downloaded kernel', :default => false, :optional => true
  p.option :clear, 'remove folder with kernel deb packages from /tmp', :default => false, :optional => true
end.process!

HOST = 'kernel.ubuntu.com'
MAINLINE = '/~kernel-ppa/mainline/'
$arch = options[:arch] || 'amd64'
$type = options[:type] || 'generic'
$versions = []
$files = []

def get_all_versions
  $versions = []
  source = Net::HTTP.get( HOST, MAINLINE )
  page = Nokogiri::HTML( source )
  page.css('a').each do |a|
    $versions << a.text if !a.text.include? '-rc'
  end
end

def get_last_version
  get_all_versions if $files.empty?
  $versions[-1]
end

def get_all_files
  $files = []
  source = Net::HTTP.get( HOST, "#{MAINLINE}#{get_last_version}" )
  page = Nokogiri::HTML( source )
  page.css('a').each do |a|
    $files << a.text if( ( a.text.include? $arch and a.text.include? $type ) or a.text.include? '_all' )
  end
end


def generate_tmp_folder
  path = "/tmp/#{SecureRandom.hex}"
  FileUtils.mkdir_p(path) unless File.exists?(path)
  return path
end

def download_file(path, file)
  counter = 0
  file_path = "#{MAINLINE}#{get_last_version}#{file}"
  Net::HTTP.start( HOST ) do |http|
    response = http.request_head( URI.escape( file_path ) )
    ProgressBar
    pbar = ProgressBar.new( "progress", response['content-length'].to_i )
    puts file
    pbar.file_transfer_mode
    File.open( "#{path}/#{file}", 'w' ) do |f|
      http.get( file_path ) do |str|
        f.write str
        counter += str.length
        pbar.set(counter)
      end
    end
    pbar.finish
  end
end

if __FILE__ == $0

  if options[:show]
    puts "Last stable version: #{get_last_version.sub('/', '')}"
    exit 0
  end

  get_all_files
  path = generate_tmp_folder

  $files.each do |file|
    download_file(path, file)
  end

  if options[:install]
    puts "\nInstalling kernel\n"
    output = %x[ sudo dpkg -i #{path}/linux-*.deb ]
    puts output
    puts "\nDon't forget reboot your PC/server\n"
  end

  if options[:clear]
    puts "\nremoving #{path}\n"
    %x[ rm -rf #{path} ]
  end

  if !options[:clear] and !options[:install]
    puts "\nrun \ bash -c 'sudo dpkg -i #{path}/linux-*.deb\' if you'd like to install downloaded kernel!\n"
  end
end

Script version 2.1, new version could be found in github: https://github.com/alter/get_last_ubuntu_kernel.git

VPN restarter script

put in crontab job:
*/10 * * * * root /root/check_vpn.sh

create script:
cat > /root/check_vpn.sh

1
2
3
4
5
6
7
8
9
#!/bin/bash
VPN_HOST=10.254.254.1
/bin/ping -c1 $VPN_HOST &gt; /dev/null;
if [ $? -eq 0 ]; then
  exit;
else
  /bin/killall -9 openvpn;
  /etc/init.d/openvpn start;
fi

autossh

Autossh is a great script which made for people who don't want to make «own wheel».

example script (from sources of autossh):

#!/bin/sh
#
# Example script to start up tunnel with autossh.
#
# This script will tunnel 2200 from the remote host
# to 22 on the local host. On remote host do:
#     ssh -p 2200 localhost
#
# $Id: autossh.host,v 1.6 2004/01/24 05:53:09 harding Exp $
#

ID=username
HOST=hostname.your.net

if [ "X$SSH_AUTH_SOCK" = "X" ]; then
eval `ssh-agent -s`
ssh-add $HOME/.ssh/id_rsa
fi

AUTOSSH_POLL=600
AUTOSSH_PORT=20000
AUTOSSH_GATETIME=30
AUTOSSH_LOGFILE=$HOST.log
AUTOSSH_DEBUG=yes
AUTOSSH_PATH=/usr/local/bin/ssh
export AUTOSSH_POLL AUTOSSH_LOGFILE AUTOSSH_DEBUG AUTOSSH_PATH AUTOSSH_GATETIME AUTOSSH_PORT

autossh -2 -fN -M 20000 -R 2200:localhost:22 ${ID}@${HOST}

Gentoo: utorrent autostart

wget http://download.utorrent.com/linux/utorrent-server-3.0-25053.tar.gz
tar -zxf utorrent-server-3.0-25053.tar.gz
mv utorrent-server-v3_0/* /home/user/.utorrent/
cat > /etc/init/utorrent.conf
description «utorrent startup script»

start on (local-filesystems and started dbus and stopped udevtrigger)
stop on runlevel [016]

script
cd /home/user/.utorrent
exec su user -c «/home/user/.utorrent/utserver»
end script

ln -s /lib/init/upstart-job /etc/init.d/utorrent

/etc/init.d/utorrent start
update-rc.d utorrent defaults

Fedora: runscript (example: openvpn client)

/etc/init.d/opevpn-home-client

#!/bin/bash
#
# Run-level Startup script for the OpenVPN client
#
# chkconfig: 345 91 19
# description: Startup/Shutdown OpenVPN client 
OVPN_HOME="/home/alter/ovpn/"
case "$1" in
    start)
        echo -n "Starting OpenVPN home client: "
        cd $OVPN_HOME && /usr/sbin/openvpn ovpn.conf
        echo "OK"
        ;;
    stop)
        echo -n "Shutdown OpenVPN home client: "
        killall openvpn
        echo "OK"
        ;;
    reload|restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: $0 start|stop|restart"
        exit 1
esac
exit 0
chmod +x /etc/init.d/opevpn-home-client
chkconfig --add openvpn-home-client 

Script for extracting archives

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
if [ -f $1 ] ; then
 case $1 in
  *.tar.bz2) tar xvjf $1 ;;
  *.tar.gz) tar xvzf $1 ;;
  *.bz2) bunzip2 $1 ;;
  *.rar) unrar x $1 ;;
  *.gz) gunzip $1 ;;
  *.tar) tar xvf $1 ;;
  *.tbz2) tar xvjf $1 ;;
  *.tgz) tar xvzf $1 ;;
  *.zip) unzip $1 ;;
  *.Z) uncompress $1 ;;
  *.7z) 7z x $1 ;;
  *) echo "'$1' cannot be extracted via script" ;;
 esac
else
 echo "'$1' is not a valid file"
fi