-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


Ubuntu Server . 2

, 09 2017 . 15:11 +
ubuntu server
1.
image
, . .

, :
  • IP + DHCP
    . . Ubuntu server.
    IP

    1. (logical name) :
    sudo lshw -C network
     logical name: enp3s0

    ifconfig ( ipconfig windows) loop 127.0.0.1. . ubuntu . enp3s0 , . eth0 ens35. .

    2.
    sudo nano /etc/network/interfaces
    (nano )
    :
    	# This file describes the network interfaces available on your system
    	# and how to activate them. For more information, see interfaces(5).
    
    	source /etc/network/interfaces.d/*
    
    	# The loopback network interface
    	auto lo
    	iface lo inet loopback
    
    	# My local network.
    	allow-hotplug enp3s0
    	iface enp3s0 inet static
    	address 172.16.55.1
    	netmask 255.255.255.0
    	gateway 172.16.55.1
    

    cntrl-x
    y

    3. :
    service networking restart


    DHCP

    1. DHCP
    sudo nano /etc/default/isc-dhcp-server
    INTERFACES=""
    DHCP . :
    INTERFACES="enp3s0"

    sudo nano /etc/dhcp/dhcpd.conf

    subnet 172.16.55.0 netmask 255.255.255.0{
     range 172.16.55.2 172.16.55.100;
    }
    DHCP 2 100.

    2. gethostbyname(),
    sudo nano /etc/hosts
    IP. IP, .
    127.0.0.1	localhost
    172.16.55.1	ubuntu

    /etc/hostname (ubuntu).
    sudo cat /etc/hostname
    .

    3. :
    service networking restart

    SSH
    , , IP SSH. Windows PuTTY. , PuTTY, .
    Ubuntu Windows.
    d:\"Program Files"\PuTTY\pscp.exe ubuntu@172.16.55.1:/home/ubuntu/tool/* "F:/WORK/SERVER/tool/"


    d:\"Program Files"\PuTTY\pscp.exe "F:/WORK/SERVER/tool/*" ubuntu@172.16.55.1:/home/ubuntu/tool/

  • FTDI
    FTDI . , , README.pdf , . D3XX Programmers Guide , Linux. FTDI, . , . , , .
    , pscp PuTTY, . d3xx-linux-i686-0.5.0.tar.bz2.
    tar -xvf d3xx-linux-i686-0.5.0.tar.bz2

    , FTDI :
    cd linux-i686
    sudo rm -f /usr/lib/libftd3xx.so
    sudo cp -f libftd3xx.so /usr/lib
    sudo cp -f libftd3xx.so.0.5.0 /usr/lib
    sudo cp -f 51-ftd3xx.rules /etc/udev/rules.d
    sudo udevadm control --reload-rules
    

  • codeBlocks , , - FTDI, Windows. Windows, h linux D3XX (c D2XX FT232RL ).
    makefile. , :
    STATLIB=libftd3xx.a Linux
    CFLAGS=$(DEPENDENCIES) -Wall -Wextra -std=c++11 c++11
    makefile main.cpp
    CC=g++
    UNAME := $(shell uname)
    ifeq ($(UNAME), Darwin)
    	DEPENDENCIES := -lpthread -ldl -lobjc -framework IOKit -framework CoreFoundation
    else
    	DEPENDENCIES := -lpthread -ldl -lrt
    endif
    CFLAGS=$(DEPENDENCIES) -Wall -Wextra -std=c++11
    STATLIB=libftd3xx.a
    APP = prgr
    
    all: $(APP)
    
    $(APP): main.o 
    	$(CC) -o $(APP) main.o $(STATLIB) $(CFLAGS)
    
    main.o: main.cpp
    	$(CC) -c -o main.o main.cpp $(CFLAGS)
    
    clean:
    	rm -f *.o ; rm $(APP)
    

    , main.c ftd3xx.h makefile libftd3xx.a .
    make

  • daemon
    Linux Windows. , . systemd , , PID=1, . Ubuntu, . , IP, , .
    ps -el
    PID. PID , . , PID kill PID ( ).
    , , , . , , , PID ( ).
    . , . . , , , .
    daemon
    void SetPidFile(char* Filename)
    {
        FILE* f;
        f = fopen(Filename, "w+");
        if (f)
        {
            fprintf(f, "%u", getpid());
            fclose(f);
        }
    }
    
    
    int main( int argc, char** argv )
    {
        char toolfile[32];
        char folder[32];
        intptr_t ret;
        FILE* logfile;
    
        if( argc!=3 )
        {
            printf( "Write address of the program and name: /home/ubuntu/tool/ tool\n");
            return -1;
        }
        pid_t pid, sid;
    
        pid = fork();
        if (pid < 0)
        {
            sleep(1);
            return -1;
        }
        //We got a good pid, Close the Parent Process
        if (pid > 0) {
            return 0;
        }
        //Create a new Signature Id for our child
        sid = setsid();
        if (sid < 0)
        {
            return -1;
        }
        //Change File Mask
        umask(0);
        //Save PID
    
        memcpy( toolfile, argv[0], strlen(argv[0]) );
        memcpy( toolfile + strlen(argv[0]), ".log", 4 );
        memset( toolfile + strlen(argv[0]) + 4, 0, 1 );
        printf( "Daemon:: log to:%s\n", toolfile );
        logfile = fopen( toolfile, "w" );
    
        fprintf( logfile, "Daemon:: started with 0=%s 1=%s 2=%s\n", argv[0], argv[1], argv[2] );
    
        memset( toolfile, 0, 32 );
        memcpy( toolfile, argv[0], strlen(argv[0]) );
        memcpy( toolfile + strlen(argv[0]), ".pid", 4 );
        memset( toolfile + strlen(argv[0]) + 4, 0, 1 );
        SetPidFile( toolfile );
        fprintf( logfile, "Daemon:: PID=%u saved in the %s\n", getpid(), toolfile );
    
        memset( folder, 0, 32 );
        memcpy( folder, argv[1], strlen(argv[1]) );
        fflush ( logfile );
    
        memset( toolfile, 0, 32 );
        memcpy( toolfile, folder, strlen(argv[1]) );
        memset( toolfile + strlen(argv[1]), '/', 1 );
        memcpy( toolfile + strlen(argv[1]) + 1, argv[2], strlen(argv[2]) );
    
        //Change Directory
        //If we cant find the directory we exit with failure.
        if ((chdir(folder)) < 0)
        {
            fprintf( logfile, "Daemon:: Program folder was not found:%s\n", folder );
            fclose( logfile );
            return -1;
        }
    
        //Close Standard File Descriptors
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
    
        fprintf( logfile, "Daemon:: Program started\n" );
        fflush ( logfile );
        ret = execl( toolfile, folder, NULL );
        if( ret==-1 )
        {
            fprintf( logfile, "Daemon:: execl error: %s. File=%s Folder=%s\n", strerror(errno), toolfile, folder );
            fclose( logfile );
            return -1;
        }
    
        fprintf( logfile, "Daemon:: closed\n" );
        fclose( logfile );
        return 0;
    }
    

    .

    , /home/ubuntu/daemon/, :
    daemon
    prgr
    prgr.strt , ( )
    prgr.stop , ( )

    :
    sudo chmod 755 ./tool
    sudo chmod 755 ./daemon


    systemd
    sudo nano /etc/systemd/system/mydaemon.service
    .
    mydaemon.service
    [Unit]
    Description=my service
    After=network.target
    After=isc-dhcp-server.service
    
    [Service]
    Type=forking
    PIDFile=/home/ubuntu/daemon/daemon.pid
    ExecStartPre=/bin/sh /home/ubuntu/daemon/prgr.strt
    ExecStart=/home/ubuntu/daemon/daemon /home/ubuntu/daemon/prgr prgr
    ExecStop=/bin/sh /home/ubuntu/daemon/prgr.stop
    Restart=always
    TimeoutSec=5
    
    [Install]
    WantedBy=multi-user.target
    

    :
    Description -
    After
    Type=forking systemd , .
    PIDFile PID
    ExecStartPre
    ExecStart
    ExecStop
    WantedBy ,

    .

    systemctl daemon-reload
    systemctl status mydaemon

    , :
    systemctl start mydaemon

    , .
    systemctl enable mydaemon


, . , , . , 3 , .
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/335042/

:  

: [1] []
 

:
: 

: ( )

:

  URL