C에서 Shell 명령어 사용 (Ubuntu 12.04, Eclipse) :: Horizontal Grays S2

이전 포스팅에서 Bluetooth Serial Port를 생성하고 연결하기 위해 몇가지 쉘 명령어를 이용해야만 했다.


기본적으로 내가 하고 있는 Linux Client PC는 Monitor도 없이 Stand Alone 으로 동작해야 해서 


전원을 켜면 제작한 프로그램이 실행되고 알아서 블루투스 연결하고 와이파이 연결하고 해야한다.


와이파이야 자동으로 연결되니 별 문제 없다면 블루투스는 프로그램 시작 전에 쉘 명령어를 입력해야 해서


C에서 입력 하는 방법을 검색 ㅋ (왠지 프로그래밍을 구글이 하는 것 같다. ^^;)


- 물론 윈도우 컴퓨터 등에서 원격접속으로 이래저래 실행 할 수도 있다. -


http://www.digipine.com/programming/25045


위 링크를 참고해서 다음과 같이 프로그램을 실행해보았다.


//Shell command

//Connect Bluetooth Seria Port

int ret = system("sudo su");

if(ret == -1) printf("sudo su Failed\n");

else printf("sudo su OK\n");


ret = system("password");

if(ret == -1) printf("Password input Failed\n");

else printf("Password input OK\n");

 즉 나는 장치를 컨트롤 해야하므로 Root 권한을 획득해야 한다.

그래서 루트권한 획득 sudo su 명령을 입력



그랬더니 다음과 같은 Error T.T

sudo: no tty present and no askpass program specified

sudo: no tty present and no askpass program specified

Sorry, try again.

sudo: no tty present and no askpass program specified

sudo: no tty present and no askpass program specified

Sorry, try again.

sudo: no tty present and no askpass program specified

sudo: no tty present and no askpass program specified

Sorry, try again.


http://stackoverflow.com/questions/21659637/sudo-no-tty-present-and-no-askpass-program-specified-netbeans


위 링크를 보니 문제는 sudo 명령 자체가 터미널에 붙여서 사용하는 것인데 쉘 명령으로 실행하니  sudo 명령을 attach 할 수 없는데 있던 것 같다.

상기 링크에서는 askpass를 이용하라는 데 난 좀 더 무식하게 해결하기로 했다.


방법은 sudo configuration을 수정하는 것

(이 방법은 추천하고 싶지 않다. sudo 명령을 비번없이 실행할 수 있기에 위험하다.)


위와 같이 sudo select-editor를 실행하면 Editor를 선택할 수 있다. nano가 편하니 2번 선택

(한번 선택하면 앞으로 계속 nano로 선택된다.)


그리고 visudo 실행한다.


# This file MUST be edited with the 'visudo' command as root.

#

# Please consider adding local content in /etc/sudoers.d/ instead of

# directly modifying this file.

#

# See the man page for details on how to write a sudoers file.

#

Defaults        env_reset

Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:$


# Host alias specification


# User alias specification


# Cmnd alias specification


# User privilege specification

root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges

%admin ALL=(ALL) ALL


# Allow members of group sudo to execute any command

#%sudo  ALL=(ALL:ALL) ALL  

%sudo ALL=NOPASSWD: ALL

# See sudoers(5) for more information on "#include" directives:


#includedir /etc/sudoers.d



아래쪽 %sudo 항목에서 

%sudo  ALL=(ALL:ALL) ALL  -> %sudo ALL=NOPASSWD: ALL 이렇게 바꿔줌


그리고 ctrl + o 로 저장 ctrl + x 로 나오면 앞으로 sudo 명령을 입력해도 비번을 묻지 않는다.


* 원래는 Ubuntu 에서 sudo configuration은 /etc/sudoers 에서 관리하는데 위험하기 때문에  visudo (/etc/sudoers.tmp)로 관리한다.

* visudo의 자세한 사용법은 검색 ㅋ (위험하니까 책임회피 ㅋ)

* 사실 한번 잘못써서 터미널에서 sudo를 사용못하게 되어서 복구모드로 진입해서 겨우 해결 (식겁했다는 T.T) http://www.psychocats.net/ubuntu/fixsudo  <= 이 사이트가 도움이 되었슴




여튼 포스팅이 삼천포로 가고 있다. ㅋㅋㅋ

다시 원래의 주제로 돌아와서 C에서 Shell 명령이 잘 되는지 다시 해보기로 한다.


결과는 다음과 같았다.


혹시 몰라 터미널을 실행하는 명령어를 입력했더니 Terminal 창이 하나 딱 뜬다.

그리고 기다린다. T.T

Terminal에서 exit 입력해서 끝내고 rfcomm connect 명령을 실행한다. 그리고 성공한다.

그런데 문제는 위와같이 실행중이 되어 버린다는 것이다.


사실 터미널에서 rfcomm connect를 수행할 때도 딱 저 메시지가 뜨고 터미널 작업은 새로운 터미널을 띄우고 했는데 T.T

여튼 어쩔수 없이 Ctrl+C로 연결을 끊고 넘어갔슴..

연결을 끊었으니 통신은 당연 안됨 T.T


다른 방법을 강구해보자!!





+ Recent posts