Tips using linux
Upload & download files using SCP
local sever -> remote server
1scp abc@111.222.333.444:/home/abc/* /home/me/remote sever -> local server
1scp /home/me/ abc@111.222.333.444:/home/abc/*in case of using other port as SSH
1scp -P 2222 abc@1.2.3.4:/home/abc/* /home/me/
List all files
including folders
1find .excluding folders
1find . -type f
다른 계정으로 bash 명령 실행하기
계정 이름, host 주소만 필요할 경우
1ssh [id]@[host] [command]password도 필요한 경우
1sshpass -p [password] ssh [id]@[host] [command]
bash error handling
error 발생시 스크립트 종료하기
1[bash_command] || [error_handling_function]example
12345678function error_exit{echo "$1" 1>&2exit 1}# [bash_command] || error_exit [error_message]cd $some_directory || error_exit "Cannot change directory! Aborting."
Using bash script on python
simple way without a return value
12import os, sysos.system("ls -al")the way with a return value (printed value)
123456import subprocessdef bashcmd (cmd, isPrint=True):result = subprocess.check_output(cmd, shell=True)print result.decode("utf-8")bashcmd ('ls -al')in case of python < 2.7
import subprocess
if "check_output" not in dir( subprocess ): # duck punch it in!
def f(*popenargs, **kwargs):
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd)
return output
subprocess.check_output = f
def bashcmd (cmd, isPrint=True):
result = subprocess.check_output(cmd, shell=True)
print result.decode("utf-8")
bashcmd ('ls -al')
Useful Links
file upload & download between Linux machines using scp