This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
amt1_ssh_operator.py
executable file
·54 lines (45 loc) · 1.76 KB
/
amt1_ssh_operator.py
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
from datetime import timedelta, datetime
import airflow
from airflow import DAG
from airflow.operators.python import PythonOperator, PythonVirtualenvOperator
from airflow.contrib.operators.ssh_operator import SSHOperator
from airflow.contrib.hooks.ssh_hook import SSHHook
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'email': ['airflow@example.com'],
'email_on_failure': False,
'email_on_retry': False,
'start_date': datetime.now() - timedelta(minutes=20),
'retries': 0,
'retry_delay': timedelta(minutes=5),
}
dag = DAG(dag_id='amt1_ssh_operator',
default_args=default_args,
# schedule_interval='0,10,20,30,40,50 * * * *',
dagrun_timeout=timedelta(seconds=120))
def create_the_file():
# ssh = SSHHook(remote_host = '172.29.6.4', username='kthomson', password='***', port = 22)
ssh = SSHHook(ssh_conn_id="sas1pcrpkthomson")
print(ssh)
ssh_client = None
try:
ssh_client = ssh.get_conn()
ssh_client.load_system_host_keys()
# ssh_client.exec_command('cd ~/airflow; touch test.txt; ls -la')
# stdin, stdout, stderr = ssh_client.exec_command('cd /Users/kthomson/; touch test.txt; ls -la')
# stdin, stdout, stderr = ssh_client.exec_command('dir')
# stdin, stdout, stderr = ssh_client.exec_command('sas test5.sas')
stdin, stdout, stderr = ssh_client.exec_command('cd Documents/My SAS Files/9.4/ && sas ccd_nonfiscal_state_test.sas')
out = stdout.read().decode().strip()
error = stderr.read().decode().strip()
print(out)
print(error)
finally:
if ssh_client:
ssh_client.close()
call_ssh_task = PythonOperator(
task_id='call_ssh_task',
python_callable=create_the_file,
dag=dag
)